Skip to content

Commit 91fbe9f

Browse files
committed
refactor: Migrate some cases to expect/reason
Thought of this when reviewing new cases for `std::env` in rust-lang#16131.
1 parent b8ba7ae commit 91fbe9f

File tree

15 files changed

+81
-62
lines changed

15 files changed

+81
-62
lines changed

build.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ fn main() {
88
commit_info();
99
compress_man();
1010
windows_manifest();
11-
// ALLOWED: Accessing environment during build time shouldn't be prohibited.
12-
#[allow(clippy::disallowed_methods)]
11+
#[expect(
12+
clippy::disallowed_methods,
13+
reason = "not `cargo`, not needing to load from config"
14+
)]
1315
let target = std::env::var("TARGET").unwrap();
1416
println!("cargo:rustc-env=RUST_HOST_TARGET={target}");
1517
}
1618

1719
fn compress_man() {
18-
// ALLOWED: Accessing environment during build time shouldn't be prohibited.
19-
#[allow(clippy::disallowed_methods)]
20+
#[expect(
21+
clippy::disallowed_methods,
22+
reason = "not `cargo`, not needing to load from config"
23+
)]
2024
let out_path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("man.tgz");
2125
let dst = fs::File::create(out_path).unwrap();
2226
let encoder = GzBuilder::new()
@@ -114,8 +118,10 @@ fn commit_info_from_rustc_source_tarball() -> Option<CommitInfo> {
114118
fn commit_info() {
115119
// Var set by bootstrap whenever omit-git-hash is enabled in rust-lang/rust's config.toml.
116120
println!("cargo:rerun-if-env-changed=CFG_OMIT_GIT_HASH");
117-
// ALLOWED: Accessing environment during build time shouldn't be prohibited.
118-
#[allow(clippy::disallowed_methods)]
121+
#[expect(
122+
clippy::disallowed_methods,
123+
reason = "not `cargo`, not needing to load from config"
124+
)]
119125
if std::env::var_os("CFG_OMIT_GIT_HASH").is_some() {
120126
return;
121127
}
@@ -129,7 +135,10 @@ fn commit_info() {
129135
println!("cargo:rustc-env=CARGO_COMMIT_DATE={}", git.date);
130136
}
131137

132-
#[allow(clippy::disallowed_methods)]
138+
#[expect(
139+
clippy::disallowed_methods,
140+
reason = "not `cargo`, not needing to load from config"
141+
)]
133142
fn windows_manifest() {
134143
use std::env;
135144
let target_os = env::var("CARGO_CFG_TARGET_OS");

src/bin/cargo/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(clippy::self_named_module_files)] // false positive in `commands/build.rs`
2-
31
use cargo::core::features;
42
use cargo::core::shell::Shell;
53
use cargo::util::network::http::http_handle;
@@ -96,7 +94,7 @@ where
9694
+ Send
9795
+ Sync,
9896
{
99-
#![allow(clippy::disallowed_methods)]
97+
#![expect(clippy::disallowed_methods, reason = "runs before config is loaded")]
10098

10199
if env_to_bool(std::env::var_os("CARGO_LOG_PROFILE").as_deref()) {
102100
let capture_args =

src/cargo/core/compiler/custom_build.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,7 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
624624
// If we're opting into backtraces, mention that build dependencies' backtraces can
625625
// be improved by requesting debuginfo to be built, if we're not building with
626626
// debuginfo already.
627-
//
628-
// ALLOWED: Other tools like `rustc` might read it directly
629-
// through `std::env`. We should make their behavior consistent.
630-
#[allow(clippy::disallowed_methods)]
627+
#[expect(clippy::disallowed_methods, reason = "consistency with rustc")]
631628
if let Ok(show_backtraces) = std::env::var("RUST_BACKTRACE") {
632629
if !built_with_debuginfo && show_backtraces != "0" {
633630
build_error_context.push_str(&format!(
@@ -1077,10 +1074,10 @@ impl BuildOutput {
10771074
None => return false,
10781075
Some(n) => n,
10791076
};
1080-
// ALLOWED: the process of rustc bootstrapping reads this through
1081-
// `std::env`. We should make the behavior consistent. Also, we
1082-
// don't advertise this for bypassing nightly.
1083-
#[allow(clippy::disallowed_methods)]
1077+
#[expect(
1078+
clippy::disallowed_methods,
1079+
reason = "consistency with rustc, not specified behavior"
1080+
)]
10841081
std::env::var("RUSTC_BOOTSTRAP")
10851082
.map_or(false, |var| var.split(',').any(|s| s == name))
10861083
};

src/cargo/core/features.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,15 +1534,17 @@ impl CliUnstable {
15341534

15351535
/// Returns the current release channel ("stable", "beta", "nightly", "dev").
15361536
pub fn channel() -> String {
1537-
// ALLOWED: For testing cargo itself only.
1538-
#[allow(clippy::disallowed_methods)]
1537+
#[expect(
1538+
clippy::disallowed_methods,
1539+
reason = "testing only, no reason for config support"
1540+
)]
15391541
if let Ok(override_channel) = env::var("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS") {
15401542
return override_channel;
15411543
}
1542-
// ALLOWED: the process of rustc bootstrapping reads this through
1543-
// `std::env`. We should make the behavior consistent. Also, we
1544-
// don't advertise this for bypassing nightly.
1545-
#[allow(clippy::disallowed_methods)]
1544+
#[expect(
1545+
clippy::disallowed_methods,
1546+
reason = "consistency with rustc, not specified behavior"
1547+
)]
15461548
if let Ok(staging) = env::var("RUSTC_BOOTSTRAP") {
15471549
if staging == "1" {
15481550
return "dev".to_string();
@@ -1556,8 +1558,10 @@ pub fn channel() -> String {
15561558
/// Only for testing and developing. See ["Running with gitoxide as default git backend in tests"][1].
15571559
///
15581560
/// [1]: https://doc.crates.io/contrib/tests/running.html#running-with-gitoxide-as-default-git-backend-in-tests
1559-
// ALLOWED: For testing cargo itself only.
1560-
#[allow(clippy::disallowed_methods)]
1561+
#[expect(
1562+
clippy::disallowed_methods,
1563+
reason = "testing only, no reason for config support"
1564+
)]
15611565
fn cargo_use_gitoxide_instead_of_git2() -> bool {
15621566
std::env::var_os("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2").map_or(false, |value| value == "1")
15631567
}

src/cargo/core/gc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub struct Gc<'a, 'gctx> {
255255
/// This is important to be held, since we don't want multiple cargos to
256256
/// be allowed to write to the cache at the same time, or for others to
257257
/// read while we are modifying the cache.
258-
#[allow(dead_code)] // Held for drop.
258+
#[expect(dead_code, reason = "held for `drop`")]
259259
lock: CacheLock<'gctx>,
260260
}
261261

src/cargo/core/global_cache_tracker.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,10 @@ fn to_timestamp(t: &SystemTime) -> Timestamp {
17911791
///
17921792
/// If possible, try to avoid calling this too often since accessing clocks
17931793
/// can be a little slow on some systems.
1794-
#[allow(clippy::disallowed_methods)]
1794+
#[expect(
1795+
clippy::disallowed_methods,
1796+
reason = "testing only, no reason for config support"
1797+
)]
17951798
fn now() -> Timestamp {
17961799
match std::env::var("__CARGO_TEST_LAST_USE_NOW") {
17971800
Ok(now) => now.parse().unwrap(),

src/cargo/core/shell.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,10 @@ impl TtyWidth {
533533
/// Returns the width of the terminal to use for diagnostics (which is
534534
/// relayed to rustc via `--diagnostic-width`).
535535
pub fn diagnostic_terminal_width(&self) -> Option<usize> {
536-
// ALLOWED: For testing cargo itself only.
537-
#[allow(clippy::disallowed_methods)]
536+
#[expect(
537+
clippy::disallowed_methods,
538+
reason = "testing only, no reason for config support"
539+
)]
538540
if let Ok(width) = std::env::var("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS") {
539541
return Some(width.parse().unwrap());
540542
}
@@ -616,7 +618,10 @@ fn supports_unicode(stream: &dyn IsTerminal) -> bool {
616618
}
617619

618620
fn supports_hyperlinks() -> bool {
619-
#[allow(clippy::disallowed_methods)] // We are reading the state of the system, not config
621+
#[expect(
622+
clippy::disallowed_methods,
623+
reason = "reading the state of the system, not config"
624+
)]
620625
if std::env::var_os("TERM_PROGRAM").as_deref() == Some(std::ffi::OsStr::new("iTerm.app")) {
621626
// Override `supports_hyperlinks` as we have an unknown incompatibility with iTerm2
622627
return false;
@@ -626,7 +631,10 @@ fn supports_hyperlinks() -> bool {
626631
}
627632

628633
/// Determines whether the terminal supports ANSI OSC 9;4.
629-
#[allow(clippy::disallowed_methods)] // Read environment variables to detect terminal
634+
#[expect(
635+
clippy::disallowed_methods,
636+
reason = "reading the state of the system, not config"
637+
)]
630638
fn supports_term_integration(stream: &dyn IsTerminal) -> bool {
631639
let windows_terminal = std::env::var("WT_SESSION").is_ok();
632640
let conemu = std::env::var("ConEmuANSI").ok() == Some("ON".into());

src/cargo/core/source_id.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,10 @@ mod tests {
922922
}
923923

924924
/// Check if `url` equals to the overridden crates.io URL.
925-
// ALLOWED: For testing Cargo itself only.
926-
#[allow(clippy::disallowed_methods)]
925+
#[expect(
926+
clippy::disallowed_methods,
927+
reason = "testing only, no reason for config support"
928+
)]
927929
fn is_overridden_crates_io_url(url: &str) -> bool {
928930
std::env::var("__CARGO_TEST_CRATES_IO_URL_DO_NOT_USE_THIS").map_or(false, |v| v == url)
929931
}

src/cargo/ops/cargo_new.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ impl NewOptions {
133133
#[serde(rename_all = "kebab-case")]
134134
struct CargoNewConfig {
135135
#[deprecated = "cargo-new no longer supports adding the authors field"]
136-
#[allow(dead_code)]
136+
#[expect(dead_code, reason = "deprecated")]
137137
name: Option<String>,
138138

139139
#[deprecated = "cargo-new no longer supports adding the authors field"]
140-
#[allow(dead_code)]
140+
#[expect(dead_code, reason = "deprecated")]
141141
email: Option<String>,
142142

143143
#[serde(rename = "vcs")]

src/cargo/ops/fix/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,10 @@ to prevent this issue from happening.
678678
/// Returns `None` if `fix` is not being run (not in proxy mode). Returns
679679
/// `Some(...)` if in `fix` proxy mode
680680
pub fn fix_get_proxy_lock_addr() -> Option<String> {
681-
// ALLOWED: For the internal mechanism of `cargo fix` only.
682-
// Shouldn't be set directly by anyone.
683-
#[allow(clippy::disallowed_methods)]
681+
#[expect(
682+
clippy::disallowed_methods,
683+
reason = "internal only, no reason for config support"
684+
)]
684685
env::var(FIX_ENV_INTERNAL).ok()
685686
}
686687

@@ -1238,23 +1239,26 @@ impl FixArgs {
12381239
}
12391240

12401241
let file = file.ok_or_else(|| anyhow::anyhow!("could not find .rs file in rustc args"))?;
1241-
// ALLOWED: For the internal mechanism of `cargo fix` only.
1242-
// Shouldn't be set directly by anyone.
1243-
#[allow(clippy::disallowed_methods)]
1242+
#[expect(
1243+
clippy::disallowed_methods,
1244+
reason = "internal only, no reason for config support"
1245+
)]
12441246
let idioms = env::var(IDIOMS_ENV_INTERNAL).is_ok();
12451247

1246-
// ALLOWED: For the internal mechanism of `cargo fix` only.
1247-
// Shouldn't be set directly by anyone.
1248-
#[allow(clippy::disallowed_methods)]
1248+
#[expect(
1249+
clippy::disallowed_methods,
1250+
reason = "internal only, no reason for config support"
1251+
)]
12491252
let prepare_for_edition = env::var(EDITION_ENV_INTERNAL).ok().map(|v| {
12501253
let enabled_edition = enabled_edition.unwrap_or(Edition::Edition2015);
12511254
let mode = EditionFixMode::from_str(&v);
12521255
mode.next_edition(enabled_edition)
12531256
});
12541257

1255-
// ALLOWED: For the internal mechanism of `cargo fix` only.
1256-
// Shouldn't be set directly by anyone.
1257-
#[allow(clippy::disallowed_methods)]
1258+
#[expect(
1259+
clippy::disallowed_methods,
1260+
reason = "internal only, no reason for config support"
1261+
)]
12581262
let sysroot = env::var_os(SYSROOT_INTERNAL).map(PathBuf::from);
12591263

12601264
Ok(FixArgs {

0 commit comments

Comments
 (0)