Skip to content

Commit e75214e

Browse files
committed
Auto merge of #14730 - weihanglo:env, r=ehuss
refactor(env): remove unnecessary clones ### What does this PR try to resolve? This was found during the review of #14701. Not every environment variable access requires an owned type. To be precise, most of our usages don't. ### How should we test and review this PR? Refactor only. Should not have any change in behavior. One drawback is that the fn signatures deviate from `std::env::var{_os}`.
2 parents 28274d0 + ce95009 commit e75214e

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

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

+9-14
Original file line numberDiff line numberDiff line change
@@ -837,31 +837,26 @@ impl LocalFingerprint {
837837
};
838838
for (key, previous) in info.env.iter() {
839839
let current = if key == CARGO_ENV {
840-
Some(
841-
cargo_exe
842-
.to_str()
843-
.ok_or_else(|| {
844-
format_err!(
845-
"cargo exe path {} must be valid UTF-8",
846-
cargo_exe.display()
847-
)
848-
})?
849-
.to_string(),
850-
)
840+
Some(cargo_exe.to_str().ok_or_else(|| {
841+
format_err!(
842+
"cargo exe path {} must be valid UTF-8",
843+
cargo_exe.display()
844+
)
845+
})?)
851846
} else {
852847
if let Some(value) = gctx.env_config()?.get(key) {
853-
value.to_str().and_then(|s| Some(s.to_string()))
848+
value.to_str()
854849
} else {
855850
gctx.get_env(key).ok()
856851
}
857852
};
858-
if current == *previous {
853+
if current == previous.as_deref() {
859854
continue;
860855
}
861856
return Ok(Some(StaleItem::ChangedEnv {
862857
var: key.clone(),
863858
previous: previous.clone(),
864-
current,
859+
current: current.map(Into::into),
865860
}));
866861
}
867862
if *checksum {

src/cargo/sources/git/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ where
669669
debug_assert!(res.is_err());
670670
let mut attempts = vec![String::from("git")];
671671
if let Ok(s) = gctx.get_env("USER").or_else(|_| gctx.get_env("USERNAME")) {
672-
attempts.push(s);
672+
attempts.push(s.to_string());
673673
}
674674
if let Some(ref s) = cred_helper.username {
675675
attempts.push(s.clone());

src/cargo/util/context/environment.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn make_case_insensitive_and_normalized_env(
1515
.filter_map(|k| k.to_str())
1616
.map(|k| (k.to_uppercase(), k.to_owned()))
1717
.collect();
18+
1819
let normalized_env = env
1920
.iter()
2021
// Only keep entries where both the key and value are valid UTF-8,
@@ -112,12 +113,12 @@ impl Env {
112113
///
113114
/// This can be used similarly to [`std::env::var_os`].
114115
/// On Windows, we check for case mismatch since environment keys are case-insensitive.
115-
pub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<OsString> {
116+
pub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<&OsStr> {
116117
match self.env.get(key.as_ref()) {
117-
Some(s) => Some(s.clone()),
118+
Some(s) => Some(s),
118119
None => {
119120
if cfg!(windows) {
120-
self.get_env_case_insensitive(key).cloned()
121+
self.get_env_case_insensitive(key)
121122
} else {
122123
None
123124
}
@@ -129,14 +130,14 @@ impl Env {
129130
///
130131
/// This can be used similarly to `std::env::var`.
131132
/// On Windows, we check for case mismatch since environment keys are case-insensitive.
132-
pub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<String> {
133+
pub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<&str> {
133134
let key = key.as_ref();
134135
let s = self
135136
.get_env_os(key)
136137
.ok_or_else(|| anyhow!("{key:?} could not be found in the environment snapshot"))?;
137138

138139
match s.to_str() {
139-
Some(s) => Ok(s.to_owned()),
140+
Some(s) => Ok(s),
140141
None => bail!("environment variable value is not valid unicode: {s:?}"),
141142
}
142143
}
@@ -146,10 +147,10 @@ impl Env {
146147
/// This is relevant on Windows, where environment variables are case-insensitive.
147148
/// Note that this only works on keys that are valid UTF-8 and it uses Unicode uppercase,
148149
/// which may differ from the OS's notion of uppercase.
149-
fn get_env_case_insensitive(&self, key: impl AsRef<OsStr>) -> Option<&OsString> {
150+
fn get_env_case_insensitive(&self, key: impl AsRef<OsStr>) -> Option<&OsStr> {
150151
let upper_case_key = key.as_ref().to_str()?.to_uppercase();
151152
let env_key: &OsStr = self.case_insensitive_env.get(&upper_case_key)?.as_ref();
152-
self.env.get(env_key)
153+
self.env.get(env_key).map(|v| v.as_ref())
153154
}
154155

155156
/// Get the value of environment variable `key` as a `&str`.

src/cargo/util/context/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -815,15 +815,15 @@ impl GlobalContext {
815815
/// [`GlobalContext`].
816816
///
817817
/// This can be used similarly to [`std::env::var`].
818-
pub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<String> {
818+
pub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<&str> {
819819
self.env.get_env(key)
820820
}
821821

822822
/// Get the value of environment variable `key` through the snapshot in
823823
/// [`GlobalContext`].
824824
///
825825
/// This can be used similarly to [`std::env::var_os`].
826-
pub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<OsString> {
826+
pub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<&OsStr> {
827827
self.env.get_env_os(key)
828828
}
829829

0 commit comments

Comments
 (0)