Skip to content

Commit 959819c

Browse files
committed
clippy: warn on disallowed_methods for std::env::{var,var_os}
In 11588 we want to avoid reading from environment variables, 11727 did that well. I wonder if we could leverage tools to help with this. Thankfully, clippy has a `disallowed_methods`[1] lint, helping us enforce the rule. [1]: https://rust-lang.github.io/rust-clippy/stable/index.html#disallowed_methods
1 parent d58902e commit 959819c

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

clippy.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
disallowed-methods = [
2+
{ path = "std::env::var", reason = "Use `Config::get_env` instead. See rust-lang/cargo#11588" },
3+
{ path = "std::env::var_os", reason = "Use `Config::get_env_os` instead. See rust-lang/cargo#11588" },
4+
{ path = "std::env::vars", reason = "Not recommended to use in Cargo. See rust-lang/cargo#11588" },
5+
{ path = "std::env::vars_os", reason = "Not recommended to use in Cargo. See rust-lang/cargo#11588" },
6+
]

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,14 +1658,14 @@ fn local_fingerprints_deps(
16581658
local.push(LocalFingerprint::RerunIfChanged { output, paths });
16591659
}
16601660

1661-
local.extend(
1662-
deps.rerun_if_env_changed
1663-
.iter()
1664-
.map(|var| LocalFingerprint::RerunIfEnvChanged {
1665-
var: var.clone(),
1666-
val: env::var(var).ok(),
1667-
}),
1668-
);
1661+
local.extend(deps.rerun_if_env_changed.iter().map(|var| {
1662+
#[allow(clippy::disallowed_methods)]
1663+
let val = env::var(var).ok();
1664+
LocalFingerprint::RerunIfEnvChanged {
1665+
var: var.clone(),
1666+
val,
1667+
}
1668+
}));
16691669

16701670
local
16711671
}

src/cargo/core/features.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -946,11 +946,13 @@ impl CliUnstable {
946946
self.add(flag, &mut warnings)?;
947947
}
948948

949-
if self.gitoxide.is_none()
950-
&& std::env::var_os("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2")
949+
if self.gitoxide.is_none() {
950+
#[allow(clippy::disallowed_methods)]
951+
if std::env::var_os("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2")
951952
.map_or(false, |value| value == "1")
952-
{
953-
self.gitoxide = GitoxideFeatures::safe().into();
953+
{
954+
self.gitoxide = GitoxideFeatures::safe().into();
955+
}
954956
}
955957
Ok(warnings)
956958
}

src/cargo/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Due to some of the default clippy lints being somewhat subjective and not
55
// necessarily an improvement, we prefer to not use them at this time.
66
#![allow(clippy::all)]
7+
#![warn(clippy::disallowed_methods)]
78
#![warn(clippy::self_named_module_files)]
89
#![allow(rustdoc::private_intra_doc_links)]
910

0 commit comments

Comments
 (0)