From ce95009d054b64383fca5ac6fbe011bb6cf51e90 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 25 Oct 2024 02:10:47 -0400 Subject: [PATCH] refactor(env): remove unnecessary clones --- src/cargo/core/compiler/fingerprint/mod.rs | 23 +++++++++------------- src/cargo/sources/git/utils.rs | 2 +- src/cargo/util/context/environment.rs | 15 +++++++------- src/cargo/util/context/mod.rs | 4 ++-- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/cargo/core/compiler/fingerprint/mod.rs b/src/cargo/core/compiler/fingerprint/mod.rs index ac23782e4d2..5ee10c7e969 100644 --- a/src/cargo/core/compiler/fingerprint/mod.rs +++ b/src/cargo/core/compiler/fingerprint/mod.rs @@ -837,31 +837,26 @@ impl LocalFingerprint { }; for (key, previous) in info.env.iter() { let current = if key == CARGO_ENV { - Some( - cargo_exe - .to_str() - .ok_or_else(|| { - format_err!( - "cargo exe path {} must be valid UTF-8", - cargo_exe.display() - ) - })? - .to_string(), - ) + Some(cargo_exe.to_str().ok_or_else(|| { + format_err!( + "cargo exe path {} must be valid UTF-8", + cargo_exe.display() + ) + })?) } else { if let Some(value) = gctx.env_config()?.get(key) { - value.to_str().and_then(|s| Some(s.to_string())) + value.to_str() } else { gctx.get_env(key).ok() } }; - if current == *previous { + if current == previous.as_deref() { continue; } return Ok(Some(StaleItem::ChangedEnv { var: key.clone(), previous: previous.clone(), - current, + current: current.map(Into::into), })); } if *checksum { diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index f9b2f582c81..d28931f8642 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -669,7 +669,7 @@ where debug_assert!(res.is_err()); let mut attempts = vec![String::from("git")]; if let Ok(s) = gctx.get_env("USER").or_else(|_| gctx.get_env("USERNAME")) { - attempts.push(s); + attempts.push(s.to_string()); } if let Some(ref s) = cred_helper.username { attempts.push(s.clone()); diff --git a/src/cargo/util/context/environment.rs b/src/cargo/util/context/environment.rs index da9cd5e641c..4b402cac70f 100644 --- a/src/cargo/util/context/environment.rs +++ b/src/cargo/util/context/environment.rs @@ -15,6 +15,7 @@ fn make_case_insensitive_and_normalized_env( .filter_map(|k| k.to_str()) .map(|k| (k.to_uppercase(), k.to_owned())) .collect(); + let normalized_env = env .iter() // Only keep entries where both the key and value are valid UTF-8, @@ -112,12 +113,12 @@ impl Env { /// /// This can be used similarly to [`std::env::var_os`]. /// On Windows, we check for case mismatch since environment keys are case-insensitive. - pub fn get_env_os(&self, key: impl AsRef) -> Option { + pub fn get_env_os(&self, key: impl AsRef) -> Option<&OsStr> { match self.env.get(key.as_ref()) { - Some(s) => Some(s.clone()), + Some(s) => Some(s), None => { if cfg!(windows) { - self.get_env_case_insensitive(key).cloned() + self.get_env_case_insensitive(key) } else { None } @@ -129,14 +130,14 @@ impl Env { /// /// This can be used similarly to `std::env::var`. /// On Windows, we check for case mismatch since environment keys are case-insensitive. - pub fn get_env(&self, key: impl AsRef) -> CargoResult { + pub fn get_env(&self, key: impl AsRef) -> CargoResult<&str> { let key = key.as_ref(); let s = self .get_env_os(key) .ok_or_else(|| anyhow!("{key:?} could not be found in the environment snapshot"))?; match s.to_str() { - Some(s) => Ok(s.to_owned()), + Some(s) => Ok(s), None => bail!("environment variable value is not valid unicode: {s:?}"), } } @@ -146,10 +147,10 @@ impl Env { /// This is relevant on Windows, where environment variables are case-insensitive. /// Note that this only works on keys that are valid UTF-8 and it uses Unicode uppercase, /// which may differ from the OS's notion of uppercase. - fn get_env_case_insensitive(&self, key: impl AsRef) -> Option<&OsString> { + fn get_env_case_insensitive(&self, key: impl AsRef) -> Option<&OsStr> { let upper_case_key = key.as_ref().to_str()?.to_uppercase(); let env_key: &OsStr = self.case_insensitive_env.get(&upper_case_key)?.as_ref(); - self.env.get(env_key) + self.env.get(env_key).map(|v| v.as_ref()) } /// Get the value of environment variable `key` as a `&str`. diff --git a/src/cargo/util/context/mod.rs b/src/cargo/util/context/mod.rs index e16bffb3c7a..2403675db19 100644 --- a/src/cargo/util/context/mod.rs +++ b/src/cargo/util/context/mod.rs @@ -815,7 +815,7 @@ impl GlobalContext { /// [`GlobalContext`]. /// /// This can be used similarly to [`std::env::var`]. - pub fn get_env(&self, key: impl AsRef) -> CargoResult { + pub fn get_env(&self, key: impl AsRef) -> CargoResult<&str> { self.env.get_env(key) } @@ -823,7 +823,7 @@ impl GlobalContext { /// [`GlobalContext`]. /// /// This can be used similarly to [`std::env::var_os`]. - pub fn get_env_os(&self, key: impl AsRef) -> Option { + pub fn get_env_os(&self, key: impl AsRef) -> Option<&OsStr> { self.env.get_env_os(key) }