From 456fb975f2e20d6f1fd3418e6a3e8cd81c03208b Mon Sep 17 00:00:00 2001 From: Ken Micklas Date: Sat, 26 Aug 2023 20:52:07 +0100 Subject: [PATCH] Fix clippy suggestions and unused imports --- crate2nix/src/config.rs | 7 +++---- crate2nix/src/lib.rs | 5 ++--- crate2nix/src/lock.rs | 2 +- crate2nix/src/metadata.rs | 14 +++++++------- crate2nix/src/nix_build.rs | 4 ++-- crate2nix/src/render.rs | 2 +- crate2nix/src/resolve.rs | 16 ++++++++-------- crate2nix/src/sources.rs | 12 ++++++------ crate2nix/src/test.rs | 16 +++++++++------- crate2nix/tests/self_build_up_to_date.rs | 4 ++-- 10 files changed, 41 insertions(+), 41 deletions(-) diff --git a/crate2nix/src/config.rs b/crate2nix/src/config.rs index dc37acda..4b2cd65e 100644 --- a/crate2nix/src/config.rs +++ b/crate2nix/src/config.rs @@ -1,6 +1,6 @@ //! Managing the `crate2nix.json` config. -use anyhow::{bail, Context, Error}; +use anyhow::{Context, Error}; use serde::{Deserialize, Serialize}; use std::{ collections::BTreeMap, @@ -8,7 +8,6 @@ use std::{ fs::File, io::{BufReader, BufWriter}, path::Path, - str::FromStr, }; impl Config { @@ -64,8 +63,8 @@ impl Config { let max_len = self .sources - .iter() - .map(|(n, _)| n.len()) + .keys() + .map(|n| n.len()) .max() .unwrap_or_default(); for (name, source) in &self.sources { diff --git a/crate2nix/src/lib.rs b/crate2nix/src/lib.rs index 62faae1d..0623aa93 100644 --- a/crate2nix/src/lib.rs +++ b/crate2nix/src/lib.rs @@ -163,8 +163,7 @@ fn cargo_metadata(config: &GenerateConfig, cargo_toml: &Path) -> Result Result<(), Error> { let mut from_lock_file: HashMap = - extract_hashes_from_lockfile(&config, default_nix)?; + extract_hashes_from_lockfile(config, default_nix)?; for (_package_id, hash) in from_lock_file.iter_mut() { let bytes = hex::decode(&hash).map_err(|e| format_err!("while decoding '{}': {}", hash, e))?; diff --git a/crate2nix/src/lock.rs b/crate2nix/src/lock.rs index b5c7d969..939fc20f 100644 --- a/crate2nix/src/lock.rs +++ b/crate2nix/src/lock.rs @@ -16,7 +16,7 @@ impl EncodableResolve { } pub fn load_lock_string(path: &Path, config: &str) -> Result { - let resolve: toml::Value = toml::from_str(&config) + let resolve: toml::Value = toml::from_str(config) .map_err(|e| format_err!("while parsing toml from {}: {}", path.display(), e))?; let v: EncodableResolve = resolve diff --git a/crate2nix/src/metadata.rs b/crate2nix/src/metadata.rs index 7d5cfc1e..e08c17ce 100644 --- a/crate2nix/src/metadata.rs +++ b/crate2nix/src/metadata.rs @@ -112,16 +112,16 @@ impl IndexedMetadata { .map(|node| { ( id_shortener.shorten(&node.id), - id_shortener.shorten_in_node(&node), + id_shortener.shorten_in_node(node), ) }) .collect(); Ok(IndexedMetadata { - root: root.as_ref().map(|id| id_shortener.shorten(&id)), + root: root.as_ref().map(|id| id_shortener.shorten(id)), workspace_members: workspace_members .iter() - .map(|id| id_shortener.shorten(&id)) + .map(|id| id_shortener.shorten(id)) .collect(), pkgs_by_id, nodes_by_id, @@ -132,7 +132,7 @@ impl IndexedMetadata { #[cfg(test)] pub fn root_package(&self) -> Option<&Package> { let root = self.root.as_ref()?; - self.pkgs_by_id.get(&root) + self.pkgs_by_id.get(root) } } @@ -200,16 +200,16 @@ impl PackageIdShortener { } pub fn lengthen_ref<'a>(&'a self, package_id: &'a PackageId) -> &'a PackageId { - self.reverse.get(&package_id).unwrap_or(&package_id) + self.reverse.get(package_id).unwrap_or(package_id) } pub fn shorten_ref<'a>(&'a self, package_id: &'a PackageId) -> &'a PackageId { - self.substitution.get(&package_id).unwrap_or(&package_id) + self.substitution.get(package_id).unwrap_or(package_id) } pub fn shorten(&self, package_id: &PackageId) -> PackageId { self.substitution - .get(&package_id) + .get(package_id) .cloned() .unwrap_or_else(|| package_id.clone()) } diff --git a/crate2nix/src/nix_build.rs b/crate2nix/src/nix_build.rs index 81c74326..d88cfe4d 100644 --- a/crate2nix/src/nix_build.rs +++ b/crate2nix/src/nix_build.rs @@ -22,7 +22,7 @@ pub fn nix_build( &format!("Building {}", project_dir), Command::new("nix") .current_dir(&project_dir) - .args(&[ + .args([ "--show-trace", "build", "-f", @@ -42,7 +42,7 @@ pub fn nix_build( ); if result.is_err() { - dump_with_lines(&project_dir_path.join("default.nix"))?; + dump_with_lines(project_dir_path.join("default.nix"))?; } result diff --git a/crate2nix/src/render.rs b/crate2nix/src/render.rs index 694180df..8ca2f1c2 100644 --- a/crate2nix/src/render.rs +++ b/crate2nix/src/render.rs @@ -176,7 +176,7 @@ fn cfg_to_nix_expr_filter( if key.starts_with("cfg(") && key.ends_with(')') { let cfg = &key[4..key.len() - 1]; - let expr = CfgExpr::from_str(&cfg).map_err(|e| { + let expr = CfgExpr::from_str(cfg).map_err(|e| { tera::Error::msg(format!( "cfg_to_nix_expr_filter: Could not parse '{}': {}", cfg, e diff --git a/crate2nix/src/resolve.rs b/crate2nix/src/resolve.rs index e8ff2c41..aa897f65 100644 --- a/crate2nix/src/resolve.rs +++ b/crate2nix/src/resolve.rs @@ -92,7 +92,7 @@ impl CrateDerivation { // name anymore. So we need to extract it from the path. let configured_source = package_path .file_name() - .and_then(|file_name| crate2nix_json.sources.get(&*file_name).cloned()); + .and_then(|file_name| crate2nix_json.sources.get(file_name).cloned()); if !crate2nix_json.sources.is_empty() && configured_source.is_none() { eprintln!( @@ -109,7 +109,7 @@ impl CrateDerivation { let source = if let Some(configured) = configured_source { configured.into() } else { - ResolvedSource::new(&config, &package, &package_path)? + ResolvedSource::new(config, package, package_path)? }; let package_path = package_path.canonicalize().map_err(|e| { @@ -128,13 +128,13 @@ impl CrateDerivation { k == "lib" || k == "cdylib" || k == "dylib" || k == "rlib" || k == "proc-macro" }) }) - .and_then(|target| BuildTarget::new(&target, &package_path).ok()); + .and_then(|target| BuildTarget::new(target, &package_path).ok()); let build = package .targets .iter() .find(|t| t.kind.iter().any(|k| k == "custom-build")) - .and_then(|target| BuildTarget::new(&target, &package_path).ok()); + .and_then(|target| BuildTarget::new(target, &package_path).ok()); let proc_macro = package .targets @@ -146,7 +146,7 @@ impl CrateDerivation { .iter() .filter_map(|t| { if t.kind.iter().any(|k| k == "bin") { - BuildTarget::new(&t, &package_path).ok() + BuildTarget::new(t, &package_path).ok() } else { None } @@ -228,7 +228,7 @@ pub fn minimal_resolve() { crate_derivation.version, semver::Version::parse("1.2.3").unwrap() ); - assert_eq!(crate_derivation.is_root_or_workspace_member, true); + assert!(crate_derivation.is_root_or_workspace_member); let empty: Vec = vec![]; assert_eq!(crate_derivation.lib_crate_types, empty); @@ -249,7 +249,7 @@ pub fn configured_source_is_used_instead_of_local_directory() { // By simulating this layout, we ensure that we do not canonicalize paths at the "wrong" // moment. let simulated_store_path = env.temp_dir(); - std::fs::File::create(&simulated_store_path.join("Cargo.toml")).expect("File creation failed"); + std::fs::File::create(simulated_store_path.join("Cargo.toml")).expect("File creation failed"); let workspace_with_symlink = env.temp_dir(); std::os::unix::fs::symlink( &simulated_store_path, @@ -272,7 +272,7 @@ pub fn configured_source_is_used_instead_of_local_directory() { version: semver::Version::from_str("1.2.3").unwrap(), sha256: "123".to_string(), }; - crate2nix_json.upsert_source(None, source.clone()); + crate2nix_json.upsert_source(None, source); let crate_derivation = CrateDerivation::resolve(&config, &crate2nix_json, &indexed, root_package).unwrap(); diff --git a/crate2nix/src/sources.rs b/crate2nix/src/sources.rs index 243a365d..1158c253 100644 --- a/crate2nix/src/sources.rs +++ b/crate2nix/src/sources.rs @@ -22,7 +22,7 @@ pub fn crates_io_source(name: String, version: Version) -> Result Result { sha256: None, }; - eprint!("Prefetching {}: ", prefetchable.to_string()); + eprint!("Prefetching {}: ", prefetchable); let sha256 = prefetchable.prefetch()?; eprintln!("done."); @@ -87,7 +87,7 @@ impl<'a> FetchedSources<'a> { } if self.sources_nix().exists() { - let reader = std::io::BufReader::new(File::open(&self.sources_nix())?); + let reader = std::io::BufReader::new(File::open(self.sources_nix())?); let generated = reader.lines().any(|l| { l.map(|l| l.contains("@generated by crate2nix")) .unwrap_or(false) @@ -97,7 +97,7 @@ impl<'a> FetchedSources<'a> { } } - crate::render::SOURCES_NIX.write_to_file(&self.sources_nix(), &info)?; + crate::render::SOURCES_NIX.write_to_file(self.sources_nix(), &info)?; Ok(()) } @@ -110,7 +110,7 @@ impl<'a> FetchedSources<'a> { let fetched_sources_symlink = self.project_dir().join(FETCHED_SOURCES); download_and_link_out_of_tree_sources( self.project_dir(), - &self.sources_nix(), + self.sources_nix(), &fetched_sources_symlink, "fetchedSources", ) @@ -200,7 +200,7 @@ fn download_and_link_out_of_tree_sources( let caption = format!("Fetching sources via {} {}", sources_nix, nix_attr); crate::command::run( &caption, - Command::new("nix").current_dir(&project_dir).args(&[ + Command::new("nix").current_dir(&project_dir).args([ "--show-trace", "build", "-f", diff --git a/crate2nix/src/test.rs b/crate2nix/src/test.rs index e4bd86d0..8d1774b1 100644 --- a/crate2nix/src/test.rs +++ b/crate2nix/src/test.rs @@ -56,7 +56,7 @@ impl MetadataEnv { } fn mut_resolve(&mut self) -> &mut Resolve { - self.metadata.resolve.get_or_insert_with(|| empty_resolve()) + self.metadata.resolve.get_or_insert_with(empty_resolve) } pub fn add_package_and_node(&mut self, name: &str) -> PackageAndNode { @@ -103,7 +103,7 @@ impl<'a> PackageAndNode<'a> { .mut_metadata() .workspace_members .push(package_id.clone()); - self.env.mut_resolve().root = Some(package_id.clone()); + self.env.mut_resolve().root = Some(package_id); self } @@ -264,11 +264,13 @@ where T: serde::de::DeserializeOwned, { use serde_json::{from_value, to_string_pretty}; - from_value(json()).expect(&format!( - "invalid {}: {}", - std::any::type_name::(), - to_string_pretty(&json()).unwrap() - )) + from_value(json()).unwrap_or_else(|_| { + panic!( + "invalid {}: {}", + std::any::type_name::(), + to_string_pretty(&json()).unwrap() + ) + }) } /// Return value from given JSON. diff --git a/crate2nix/tests/self_build_up_to_date.rs b/crate2nix/tests/self_build_up_to_date.rs index 65ebd44c..5e7acf86 100644 --- a/crate2nix/tests/self_build_up_to_date.rs +++ b/crate2nix/tests/self_build_up_to_date.rs @@ -56,7 +56,7 @@ fn pregenerated_up_to_date() { Some(pregenerated_build) => { let cargo_nix = PathBuf::from_str(&pregenerated_build) .expect("pregeneratedBuild must be valid path"); - assert_up_to_date(&cargo_nix.parent().expect("Cargo.nix must be in directory")); + assert_up_to_date(cargo_nix.parent().expect("Cargo.nix must be in directory")); } None => println!("Skipping not pregenerated {}", test_config.name), } @@ -125,7 +125,7 @@ struct TestConfig { fn get_test_configs() -> Result, Error> { let output = Command::new("nix") - .args(&["eval", "--json", "-f", "../tests.nix", "buildTestConfigs"]) + .args(["eval", "--json", "-f", "../tests.nix", "buildTestConfigs"]) .output() .map_err(|e| format_err!("while spawning nix: {}", e))?; if !output.status.success() {