Skip to content

fix clippy warnings #12050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cargo-platform/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])));
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cargo-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn split_rules(t: TokenStream) -> Vec<String> {
.filter(|parts| !parts.is_empty())
.map(|parts| {
parts
.into_iter()
.iter()
.map(|part| part.to_string())
.collect::<String>()
})
Expand All @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions crates/cargo-test-support/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ALLOWED: testing is exempt (`std::env::var()`)
#[allow(clippy::disallowed_methods)]
fn main() {
println!(
"cargo:rustc-env=NATIVE_ARCH={}",
Expand Down
2 changes: 2 additions & 0 deletions crates/cargo-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
38 changes: 23 additions & 15 deletions crates/cargo-util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
// 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(),
Expand Down Expand Up @@ -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<PathBuf> {
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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -480,6 +486,8 @@ fn set_not_readonly(p: &Path) -> io::Result<bool> {
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)
Expand All @@ -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;
Expand Down Expand Up @@ -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| {
Expand Down
12 changes: 10 additions & 2 deletions crates/cargo-util/src/process_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -471,7 +477,7 @@ impl ProcessBuilder {
}
writeln!(buf, "{arg}")?;
}
tmp.write_all(&mut buf)?;
tmp.write_all(&buf)?;
Ok((cmd, tmp))
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util/src/process_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion crates/cargo-util/src/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions tests/build-std/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
paths.insert(0, sysroot_bin);
Expand Down