From dcc4687e7f8f169977b59e0833664c3970f3ab74 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 27 Apr 2023 19:26:15 +0200 Subject: [PATCH] fix clippy warnings --- crates/cargo-platform/src/cfg.rs | 2 +- crates/cargo-test-macro/src/lib.rs | 6 ++-- crates/cargo-test-support/build.rs | 2 ++ crates/cargo-util/src/lib.rs | 2 ++ crates/cargo-util/src/paths.rs | 38 ++++++++++++++---------- crates/cargo-util/src/process_builder.rs | 12 ++++++-- crates/cargo-util/src/process_error.rs | 2 +- crates/cargo-util/src/sha256.rs | 2 +- tests/build-std/main.rs | 2 ++ 9 files changed, 45 insertions(+), 23 deletions(-) diff --git a/crates/cargo-platform/src/cfg.rs b/crates/cargo-platform/src/cfg.rs index c3ddb69bc88..9373b3f6202 100644 --- a/crates/cargo-platform/src/cfg.rs +++ b/crates/cargo-platform/src/cfg.rs @@ -271,7 +271,7 @@ impl<'a> Iterator for Tokenizer<'a> { Some((_, ',')) => return Some(Ok(Token::Comma)), Some((_, '=')) => return Some(Ok(Token::Equals)), Some((start, '"')) => { - while let Some((end, ch)) = self.s.next() { + for (end, ch) in &mut self.s { if ch == '"' { return Some(Ok(Token::String(&self.orig[start + 1..end]))); } diff --git a/crates/cargo-test-macro/src/lib.rs b/crates/cargo-test-macro/src/lib.rs index aa06f477de0..491aa265b9c 100644 --- a/crates/cargo-test-macro/src/lib.rs +++ b/crates/cargo-test-macro/src/lib.rs @@ -176,7 +176,7 @@ fn split_rules(t: TokenStream) -> Vec { .filter(|parts| !parts.is_empty()) .map(|parts| { parts - .into_iter() + .iter() .map(|part| part.to_string()) .collect::() }) @@ -197,10 +197,10 @@ fn version() -> &'static (u32, bool) { .output() .expect("rustc should run"); let stdout = std::str::from_utf8(&output.stdout).expect("utf8"); - let vers = stdout.split_whitespace().skip(1).next().unwrap(); + let vers = stdout.split_whitespace().nth(1).unwrap(); let is_nightly = option_env!("CARGO_TEST_DISABLE_NIGHTLY").is_none() && (vers.contains("-nightly") || vers.contains("-dev")); - let minor = vers.split('.').skip(1).next().unwrap().parse().unwrap(); + let minor = vers.split('.').nth(1).unwrap().parse().unwrap(); unsafe { VERSION = (minor, is_nightly) } }); unsafe { &VERSION } diff --git a/crates/cargo-test-support/build.rs b/crates/cargo-test-support/build.rs index 478da7d99f3..323db137a1f 100644 --- a/crates/cargo-test-support/build.rs +++ b/crates/cargo-test-support/build.rs @@ -1,3 +1,5 @@ +// ALLOWED: testing is exempt (`std::env::var()`) +#[allow(clippy::disallowed_methods)] fn main() { println!( "cargo:rustc-env=NATIVE_ARCH={}", diff --git a/crates/cargo-util/src/lib.rs b/crates/cargo-util/src/lib.rs index 0cbc920ecf3..fe6f25fce02 100644 --- a/crates/cargo-util/src/lib.rs +++ b/crates/cargo-util/src/lib.rs @@ -13,6 +13,8 @@ pub mod registry; mod sha256; /// Whether or not this running in a Continuous Integration environment. +// ALLOWED: testing is exempt +#[allow(clippy::disallowed_methods)] pub fn is_ci() -> bool { std::env::var("CI").is_ok() || std::env::var("TF_BUILD").is_ok() } diff --git a/crates/cargo-util/src/paths.rs b/crates/cargo-util/src/paths.rs index 69df7a2096c..d7b3c588cb6 100644 --- a/crates/cargo-util/src/paths.rs +++ b/crates/cargo-util/src/paths.rs @@ -65,6 +65,8 @@ pub fn dylib_path_envvar() -> &'static str { /// Note that some operating systems will have defaults if this is empty that /// will need to be dealt with. pub fn dylib_path() -> Vec { + // ALLOWED: no `Config` available here + #[allow(clippy::disallowed_methods)] match env::var_os(dylib_path_envvar()) { Some(var) => env::split_paths(&var).collect(), None => Vec::new(), @@ -112,9 +114,11 @@ pub fn normalize_path(path: &Path) -> PathBuf { /// Returns an error if it cannot be found. pub fn resolve_executable(exec: &Path) -> Result { if exec.components().count() == 1 { + // ALLOWED: no `Config` available here + #[allow(clippy::disallowed_methods)] let paths = env::var_os("PATH").ok_or_else(|| anyhow::format_err!("no PATH"))?; let candidates = env::split_paths(&paths).flat_map(|path| { - let candidate = path.join(&exec); + let candidate = path.join(exec); let with_exe = if env::consts::EXE_EXTENSION.is_empty() { None } else { @@ -368,6 +372,8 @@ pub struct PathAncestors<'a> { impl<'a> PathAncestors<'a> { fn new(path: &'a Path, stop_root_at: Option<&Path>) -> PathAncestors<'a> { + // ALLOWED: tests are exempt. + #[allow(clippy::disallowed_methods)] let stop_at = env::var("__CARGO_TEST_ROOT") .ok() .map(PathBuf::from) @@ -437,7 +443,7 @@ fn _remove_dir_all(p: &Path) -> Result<()> { remove_file(&path)?; } } - remove_dir(&p) + remove_dir(p) } /// Equivalent to [`std::fs::remove_dir`] with better error messages. @@ -480,6 +486,8 @@ fn set_not_readonly(p: &Path) -> io::Result { if !perms.readonly() { return Ok(false); } + // ALLOWED: world-writable permissions are acceptable here. + #[allow(clippy::permissions_set_readonly_false)] perms.set_readonly(false); fs::set_permissions(p, perms)?; Ok(true) @@ -505,9 +513,11 @@ fn _link_or_copy(src: &Path, dst: &Path) -> Result<()> { // unlink dst in this case. symlink_metadata(dst).is_ok() will tell us // whether dst exists *without* following symlinks, which is what we want. if fs::symlink_metadata(dst).is_ok() { - remove_file(&dst)?; + remove_file(dst)?; } + // ALLOWED: testing is exempt + #[allow(clippy::disallowed_methods)] let link_result = if src.is_dir() { #[cfg(target_os = "redox")] use std::os::redox::fs::symlink; @@ -538,19 +548,17 @@ fn _link_or_copy(src: &Path, dst: &Path) -> Result<()> { // See https://github.com/rust-lang/cargo/issues/7821 for the // gory details. fs::copy(src, dst).map(|_| ()) + } else if cfg!(target_os = "macos") { + // This is a work-around for a bug on macos. There seems to be a race condition + // with APFS when hard-linking binaries. Gatekeeper does not have signing or + // hash information stored in kernel when running the process. Therefore killing it. + // This problem does not appear when copying files as kernel has time to process it. + // Note that: fs::copy on macos is using CopyOnWrite (syscall fclonefileat) which should be + // as fast as hardlinking. + // See https://github.com/rust-lang/cargo/issues/10060 for the details + fs::copy(src, dst).map(|_| ()) } else { - if cfg!(target_os = "macos") { - // This is a work-around for a bug on macos. There seems to be a race condition - // with APFS when hard-linking binaries. Gatekeeper does not have signing or - // hash information stored in kernel when running the process. Therefore killing it. - // This problem does not appear when copying files as kernel has time to process it. - // Note that: fs::copy on macos is using CopyOnWrite (syscall fclonefileat) which should be - // as fast as hardlinking. - // See https://github.com/rust-lang/cargo/issues/10060 for the details - fs::copy(src, dst).map(|_| ()) - } else { - fs::hard_link(src, dst) - } + fs::hard_link(src, dst) }; link_result .or_else(|err| { diff --git a/crates/cargo-util/src/process_builder.rs b/crates/cargo-util/src/process_builder.rs index 76392f2564b..588f6c325b9 100644 --- a/crates/cargo-util/src/process_builder.rs +++ b/crates/cargo-util/src/process_builder.rs @@ -163,7 +163,13 @@ impl ProcessBuilder { self.env .get(var) .cloned() - .or_else(|| Some(env::var_os(var))) + .or_else(|| { + Some( + // ALLOWED: No `Config` available. + #[allow(clippy::disallowed_methods)] + env::var_os(var), + ) + }) .and_then(|s| s) } @@ -471,7 +477,7 @@ impl ProcessBuilder { } writeln!(buf, "{arg}")?; } - tmp.write_all(&mut buf)?; + tmp.write_all(&buf)?; Ok((cmd, tmp)) } @@ -541,6 +547,8 @@ impl ProcessBuilder { /// Forces the command to use `@path` argfile. /// /// You should set `__CARGO_TEST_FORCE_ARGFILE` to enable this. +// ALLOWED: testing is exempt +#[allow(clippy::disallowed_methods)] fn debug_force_argfile(retry_enabled: bool) -> bool { cfg!(debug_assertions) && env::var("__CARGO_TEST_FORCE_ARGFILE").is_ok() && retry_enabled } diff --git a/crates/cargo-util/src/process_error.rs b/crates/cargo-util/src/process_error.rs index 9b4a38cb5e3..b662a1c98a2 100644 --- a/crates/cargo-util/src/process_error.rs +++ b/crates/cargo-util/src/process_error.rs @@ -196,5 +196,5 @@ pub fn is_simple_exit_code(code: i32) -> bool { // codes are very large. This is just a rough // approximation of which codes are "normal" and which // ones are abnormal termination. - code >= 0 && code <= 127 + (0..=127).contains(&code) } diff --git a/crates/cargo-util/src/sha256.rs b/crates/cargo-util/src/sha256.rs index 8906fe93d79..bff962b3c17 100644 --- a/crates/cargo-util/src/sha256.rs +++ b/crates/cargo-util/src/sha256.rs @@ -14,7 +14,7 @@ impl Sha256 { } pub fn update(&mut self, bytes: &[u8]) -> &mut Sha256 { - let _ = self.0.update(bytes); + self.0.update(bytes); self } diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index 47a4bb671c0..6b265e1c4f4 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -217,6 +217,8 @@ fn custom_test_framework() { .join("rustlib") .join(rustc_host()) .join("bin"); + // ALLOWED: testing is exempt (`std::env::var()`) + #[allow(clippy::disallowed_methods)] let path = env::var_os("PATH").unwrap_or_default(); let mut paths = env::split_paths(&path).collect::>(); paths.insert(0, sysroot_bin);