Skip to content

Commit

Permalink
chore: Apply auto fixable clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
VorpalBlade committed Sep 6, 2024
1 parent e611cf4 commit 004471a
Show file tree
Hide file tree
Showing 55 changed files with 218 additions and 182 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,19 @@ zstd = "0.13.2"

[workspace.lints.rust]
rust-2018-idioms = "warn"
unused_qualifications = "warn"

[workspace.lints.clippy]
assigning_clones = "warn"
doc_markdown = "warn"
format_push_string = "warn"
redundant_clone = "warn"
needless_pass_by_value = "warn"
unnecessary_box_returns = "warn"
redundant_closure_for_method_calls = "warn"
semicolon_if_nothing_returned = "warn"
undocumented_unsafe_blocks = "warn"
uninlined_format_args = "warn"
unnecessary_safety_doc = "warn"
unwrap_used = "warn"
wildcard_imports = "warn"
Expand Down
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
allow-unwrap-in-tests = true
avoid-breaking-exported-api = false
check-private-items = true
10 changes: 5 additions & 5 deletions crates/konfigkoll/src/fs_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) fn scan_fs(
let mut fs_instructions_sys = vec![];
let files = if backend.prefer_files_from_archive() {
tracing::debug!("Using files from archives");
let all = package_map.keys().cloned().collect::<Vec<_>>();
let all = package_map.keys().copied().collect::<Vec<_>>();
let mut files = backend.files_from_archives(&all, package_map, interner)?;
// For all the failures, attempt to resolve them with the traditional backend
let missing: AHashSet<PackageRef> = files
Expand All @@ -60,7 +60,7 @@ pub(crate) fn scan_fs(
}
})
.flatten()
.cloned()
.copied()
.collect();
let mut extra_files = vec![];
if !missing.is_empty() {
Expand All @@ -70,7 +70,7 @@ pub(crate) fn scan_fs(
extra time)"
);
let traditional_files = backend.files(interner)?;
for file in traditional_files.into_iter() {
for file in traditional_files {
if let Some(pkg_ref) = file.package {
if missing.contains(&pkg_ref) {
extra_files.push(file);
Expand Down Expand Up @@ -99,14 +99,14 @@ pub(crate) fn scan_fs(
.for_each(|entry| {
file_map.insert(entry.path.clone(), entry);
});
extra_files.into_iter().for_each(|entry| {
for entry in extra_files {
let old = file_map.insert(entry.path.clone(), entry);
if let Some(old) = old {
if old.properties.is_dir() == Some(false) {
tracing::warn!("Duplicate file entry for {}", old.path.display());
}
}
});
}
file_map.into_iter().map(|(_, v)| v).collect_vec()
} else {
let mut files = backend.files(interner).wrap_err_with(|| {
Expand Down
10 changes: 5 additions & 5 deletions crates/konfigkoll/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod _musl {
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> color_eyre::eyre::Result<()> {
async fn main() -> eyre::Result<()> {
color_eyre::install()?;
// Set up logging with tracing
let filter = tracing_subscriber::EnvFilter::builder()
Expand All @@ -73,7 +73,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
return Ok(());
}

let mut script_engine = konfigkoll_script::ScriptEngine::new_with_files(&config_path)?;
let mut script_engine = ScriptEngine::new_with_files(&config_path)?;

match cli.command {
Commands::Init {} | Commands::Save { .. } | Commands::Apply {} | Commands::Diff { .. } => {}
Expand Down Expand Up @@ -232,8 +232,8 @@ async fn main() -> color_eyre::eyre::Result<()> {
tracing::info!("Got file system scan results");

// Compare expected to system
let mut script_fs = konfigkoll_core::state::FsEntries::default();
let mut sys_fs = konfigkoll_core::state::FsEntries::default();
let mut script_fs = FsEntries::default();
let mut sys_fs = FsEntries::default();
let fs_actions = std::mem::take(&mut script_engine.state_mut().commands_mut().fs_actions);
script_fs.apply_instructions(fs_actions.into_iter(), true);
sys_fs.apply_instructions(fs_instructions_sys.into_iter(), false);
Expand Down Expand Up @@ -354,7 +354,7 @@ fn cmd_save_changes(
.write(true)
.truncate(true)
.open(&output_path)
.wrap_err_with(|| format!("Failed to open output file {}", output_path))?,
.wrap_err_with(|| format!("Failed to open output file {output_path}"))?,
);
output.write_all("// This file is generated by konfigkoll\n".as_bytes())?;
output.write_all(
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll/src/pkgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) fn load_packages(
(backend, backend_pkgs)
})
.collect();
for (backend, backend_pkgs) in backend_maps.into_iter() {
for (backend, backend_pkgs) in backend_maps {
let (backend_pkgs_map, pkg_instructions) = backend_pkgs?;
package_maps.insert(backend.as_backend_enum(), backend_pkgs_map);
pkgs_sys.extend(pkg_instructions.into_iter());
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_core/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl InProcessApplicator {
.get(instr.path.as_std_path())
.ok_or_else(|| eyre::eyre!("Failed to find owner for {}", instr.path))?
.ok_or_else(|| eyre::eyre!("No owner for {}", instr.path))?;
let package = package.to_str(&self.interner);
let package = package.as_str(&self.interner);
// Get original contents:
let queries = [OriginalFileQuery {
package: package.into(),
Expand Down
11 changes: 6 additions & 5 deletions crates/konfigkoll_core/src/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub trait Choices: Copy + PartialEq + Eq {
fn options() -> &'static [(char, &'static str, Self)];

/// Get the default choice (if any)
#[must_use]
fn default() -> Option<Self> {
None
}
Expand All @@ -43,6 +44,7 @@ pub struct MultiOptionConfirm<T: Choices> {

impl<T: Choices + 'static> MultiOptionConfirm<T> {
/// Create a builder for this type
#[must_use]
pub fn builder() -> MultiOptionConfirmBuilder<T> {
MultiOptionConfirmBuilder::new()
}
Expand Down Expand Up @@ -93,9 +95,9 @@ impl<T: Choices> MultiOptionConfirm<T> {
let mut term = Term::stdout();
let ch = inner_prompt(&mut term, &self.prompt, self.default)?;
let lower_case: AHashSet<_> = ch.to_lowercase().collect();
let found = AHashSet::from_iter(self.options.keys().cloned())
let found = AHashSet::from_iter(self.options.keys().copied())
.intersection(&lower_case)
.cloned()
.copied()
.collect_vec();
if found.len() == 1 {
return Ok(self.options[&ch]);
Expand Down Expand Up @@ -211,10 +213,9 @@ impl<T: Choices + 'static> MultiOptionConfirmBuilder<T> {
self
}

#[must_use]
pub fn build(&self) -> MultiOptionConfirm<T> {
if T::options().len() < 2 {
panic!("At least two options are required");
}
assert!(T::options().len() >= 2, "At least two options are required");
let mut default_char = None;
let default = T::default();
let options: AHashMap<char, T> = T::options()
Expand Down
4 changes: 2 additions & 2 deletions crates/konfigkoll_core/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ pub fn convert_packages_to_pkg_instructions(
continue;
}
let identifier = if package.ids.is_empty() {
package.name.to_str(interner).into()
package.name.as_str(interner).into()
} else {
package.ids[0].to_str(interner).into()
package.ids[0].as_str(interner).into()
};
results.insert(
PkgIdent {
Expand Down
20 changes: 11 additions & 9 deletions crates/konfigkoll_core/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn show_fs_instr_diff(
println!(
"{}: Would change mode: {} -> {}",
instr.path,
style(format!("{:o}", old_mode)).red(),
style(format!("{old_mode:o}")).red(),
style(format!("{:o}", mode.as_raw())).green()
);
}
Expand All @@ -90,13 +90,14 @@ pub fn show_fs_instr_diff(
.unwrap_or(0);
// Resolve to old user
let old_user = nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(old_uid))?
.map(|u| u.name)
.unwrap_or_else(|| "<user missing in passwd?>".to_string());
.map_or_else(|| "<user missing in passwd?>".to_string(), |u| u.name);
// Resolve new owner to new UID
let new_uid = nix::unistd::User::from_name(owner.as_str())?
.map(|u| u.uid.as_raw())
.map(|uid| format!("{}", uid))
.unwrap_or_else(|| "<uid missing in passwd?>".to_string());
.map_or_else(
|| "<uid missing in passwd?>".to_string(),
|uid| format!("{uid}"),
);
// Show diff
println!(
"{}: Would change owner: {} ({}) -> {} ({})",
Expand All @@ -114,13 +115,14 @@ pub fn show_fs_instr_diff(
.unwrap_or(0);
// Resolve to old group
let old_group = nix::unistd::Group::from_gid(nix::unistd::Gid::from_raw(old_gid))?
.map(|g| g.name)
.unwrap_or_else(|| "<group missing in group?>".to_string());
.map_or_else(|| "<group missing in group?>".to_string(), |g| g.name);
// Resolve new group to new GID
let new_gid = nix::unistd::Group::from_name(group.as_str())?
.map(|g| g.gid.as_raw())
.map(|gid| format!("{}", gid))
.unwrap_or_else(|| "<gid missing in group?>".to_string());
.map_or_else(
|| "<gid missing in group?>".to_string(),
|gid| format!("{gid}"),
);
// Show diff
println!(
"{}: Would change group: {} ({}) -> {} ({})",
Expand Down
10 changes: 5 additions & 5 deletions crates/konfigkoll_core/src/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ pub fn save_fs_changes<'instruction>(
format_compact!(" // {comment}")
}
(Some(pkg), None) => {
format_compact!(" // [{}]", pkg.to_str(interner))
format_compact!(" // [{}]", pkg.as_str(interner))
}
(Some(pkg), Some(ref comment)) => {
format_compact!(" // [{}] {comment}", pkg.to_str(interner))
format_compact!(" // [{}] {comment}", pkg.as_str(interner))
}
};
let prefix = format!(" {}cmds", prefix);
let prefix = format!(" {prefix}cmds");
match instruction.op {
konfigkoll_types::FsOp::Remove => {
writeln!(output, "{prefix}.rm(\"{}\")?;{}", instruction.path, comment)?;
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn save_packages<'instructions>(
output: &mut dyn std::io::Write,
instructions: impl Iterator<Item = (&'instructions PkgIdent, PkgInstruction)>,
) -> eyre::Result<()> {
let prefix = format!(" {}cmds", prefix);
let prefix = format!(" {prefix}cmds");
let instructions = instructions
.into_iter()
.sorted_unstable_by(|(ak, av), (bk, bv)| {
Expand All @@ -142,7 +142,7 @@ pub fn save_packages<'instructions>(
.then_with(|| ak.identifier.cmp(&bk.identifier))
});

for (pkg_ident, pkg_instruction) in instructions.into_iter() {
for (pkg_ident, pkg_instruction) in instructions {
let comment = match &pkg_instruction.comment {
Some(comment) => format_compact!(" // {}", comment),
None => CompactString::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ enum FsEntry {
/// A file
File(FileContents),
/// A symlink
Symlink { target: camino::Utf8PathBuf },
Symlink { target: Utf8PathBuf },
/// Create a FIFO
Fifo,
/// Create a block device
Expand Down
2 changes: 2 additions & 0 deletions crates/konfigkoll_hwinfo/src/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl PciDevice {
}

/// Get the vendor, device and possibly subsystem names
#[must_use]
pub fn vendor_names<'db>(&self, db: &'db PciIdDb) -> PciVendorLookup<&'db str> {
// Resolve vendor
let vendor = db.vendors.get(&self.vendor);
Expand All @@ -121,6 +122,7 @@ impl PciDevice {
}

/// Get the class, subclass and program interface names
#[must_use]
pub fn class_strings<'db>(&self, db: &'db PciIdDb) -> PciClassLookup<&'db str> {
// Split up class 0xccsspp
let class = (self.class >> 16) as u8;
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_script/src/plugins/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl Commands {
#[rune::module(::command)]
/// Commands describe the changes to apply to the system
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<Commands>()?;
m.function_meta(Commands::ignore_path__meta)?;
m.function_meta(Commands::add_pkg__meta)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/konfigkoll_script/src/plugins/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rune::Module;
use std::fmt::Display;

/// Result alias using `KError`
pub type KResult<T, E = KError> = core::result::Result<T, E>;
pub type KResult<T, E = KError> = Result<T, E>;

/// An opqaue error type that can be be printed (but does little else)
///
Expand Down Expand Up @@ -89,7 +89,7 @@ impl KError {
#[rune::module(::error)]
/// Generic error handling type(s) used by konfigkoll
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<KError>()?;
m.function_meta(KError::string_debug)?;
m.function_meta(KError::string_display)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_script/src/plugins/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn config_path() -> String {
/// This is generally not needed when working with konfigkoll, but can be useful
/// for interacting with external commands via the `process` module.
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<File>()?;
m.function_meta(File::debug)?;
m.function_meta(File::open)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_script/src/plugins/package_managers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl OriginalFilesError {
#[rune::module(::package_managers)]
/// Interface to the package manager(s) in the system
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<PackageManager>()?;
m.function_meta(PackageManager::original_file_contents__meta)?;
m.ty::<PackageManagers>()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_script/src/plugins/passwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ fn set_nologin_path(path: &str) {
#[rune::module(::passwd)]
/// Utilities for patching file contents conveniently.
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<Passwd>()?;
m.ty::<User>()?;
m.ty::<Group>()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_script/src/plugins/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl TryFrom<&Action> for konfigkoll_utils::line_edit::Action {
#[rune::module(::patch)]
/// Utilities for patching file contents conveniently.
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<LineEditor>()?;
m.function_meta(LineEditor::new)?;
m.function_meta(LineEditor::apply)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_script/src/plugins/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use tracing::instrument;
/// pipelines.
#[rune::module(::process)]
pub fn module(_stdio: bool) -> Result<Module, ContextError> {
let mut module = Module::from_meta(self::module_meta)?;
let mut module = Module::from_meta(module_meta)?;
module.ty::<Command>()?;
module.ty::<Child>()?;
module.ty::<ExitStatus>()?;
Expand Down
4 changes: 2 additions & 2 deletions crates/konfigkoll_script/src/plugins/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ impl Properties {
#[rune::function]
pub fn dump(&self) {
for (key, value) in self.properties.iter().sorted_by(|a, b| a.0.cmp(b.0)) {
println!("{} = {:?}", key, value);
println!("{key} = {value:?}");
}
}
}

#[rune::module(::properties)]
/// User defined persistent (between phases) properties
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<Properties>()?;
m.function_meta(Properties::get)?;
m.function_meta(Properties::set)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/konfigkoll_script/src/plugins/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Regex {
#[rune::module(::regex)]
/// A wrapper for the rust regex crate
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<Regex>()?;
m.function_meta(Regex::new)?;
m.function_meta(Regex::is_match)?;
Expand Down
3 changes: 2 additions & 1 deletion crates/konfigkoll_script/src/plugins/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Settings {
*guard
}

/// Get enabled package backends
pub fn enabled_pkg_backends(&self) -> impl Iterator<Item = paketkoll_types::backend::Backend> {
let guard = self.enabled_pkg_backends.lock();
let v: Vec<_> = guard.iter().cloned().collect();
Expand Down Expand Up @@ -246,7 +247,7 @@ impl Settings {
#[rune::module(::settings)]
/// Settings of how konfigkoll should behave.
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
let mut m = Module::from_meta(module_meta)?;
m.ty::<Settings>()?;
m.function_meta(Settings::set_file_backend)?;
m.function_meta(Settings::enable_pkg_backend)?;
Expand Down
Loading

0 comments on commit 004471a

Please sign in to comment.