From 60b9351b7508df868e3c0787df6d8ffae4854910 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 8 Jan 2020 23:41:35 +0700 Subject: [PATCH 1/2] Unwrap Option early --- src/cli/self_update.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 62bbef42d1..8266d2229d 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -399,11 +399,11 @@ fn check_existence_of_rustc_or_cargo_in_path(no_prompt: bool) -> Result<()> { fn do_pre_install_sanity_checks() -> Result<()> { let rustc_manifest_path = PathBuf::from("/usr/local/lib/rustlib/manifest-rustc"); let uninstaller_path = PathBuf::from("/usr/local/lib/rustlib/uninstall.sh"); - let rustup_sh_path = utils::home_dir().map(|d| d.join(".rustup")); - let rustup_sh_version_path = rustup_sh_path.as_ref().map(|p| p.join("rustup-version")); + let rustup_sh_path = utils::home_dir().unwrap().join(".rustup"); + let rustup_sh_version_path = rustup_sh_path.join("rustup-version"); let rustc_exists = rustc_manifest_path.exists() && uninstaller_path.exists(); - let rustup_sh_exists = rustup_sh_version_path.map(|p| p.exists()) == Some(true); + let rustup_sh_exists = rustup_sh_version_path.exists(); if rustc_exists { warn!("it looks like you have an existing installation of Rust"); @@ -418,10 +418,7 @@ fn do_pre_install_sanity_checks() -> Result<()> { if rustup_sh_exists { warn!("it looks like you have existing rustup.sh metadata"); warn!("rustup cannot be installed while rustup.sh metadata exists"); - warn!( - "delete `{}` to remove rustup.sh", - rustup_sh_path.unwrap().display() - ); + warn!("delete `{}` to remove rustup.sh", rustup_sh_path.display()); warn!("or, if you already have rustup installed, you can run"); warn!("`rustup self update` and `rustup toolchain list` to upgrade"); warn!("your directory structure"); @@ -711,7 +708,7 @@ pub fn install_proxies() -> Result<()> { if tool_handles.iter().all(|h| *h != handle) { warn!("tool `{}` is already installed, remove it from `{}`, then run `rustup update` \ to have rustup manage this tool.", - tool, bin_path.to_string_lossy()); + tool, bin_path.display()); continue; } } @@ -1094,30 +1091,27 @@ fn get_add_path_methods() -> Vec { return vec![PathUpdateMethod::Windows]; } - let profile = utils::home_dir().map(|p| p.join(".profile")); + let home_dir = utils::home_dir().unwrap(); + let profile = home_dir.join(".profile"); let mut profiles = vec![profile]; if let Ok(shell) = env::var("SHELL") { if shell.contains("zsh") { - let zdotdir = env::var("ZDOTDIR") - .ok() - .map(PathBuf::from) - .or_else(utils::home_dir); - let zprofile = zdotdir.map(|p| p.join(".zprofile")); + let var = env::var_os("ZDOTDIR"); + let zdotdir = var.as_deref().map_or_else(|| home_dir.as_path(), Path::new); + let zprofile = zdotdir.join(".zprofile"); profiles.push(zprofile); } } - if let Some(bash_profile) = utils::home_dir().map(|p| p.join(".bash_profile")) { - // Only update .bash_profile if it exists because creating .bash_profile - // will cause .profile to not be read - if bash_profile.exists() { - profiles.push(Some(bash_profile)); - } + let bash_profile = home_dir.join(".bash_profile"); + // Only update .bash_profile if it exists because creating .bash_profile + // will cause .profile to not be read + if bash_profile.exists() { + profiles.push(bash_profile); } - let rcfiles = profiles.into_iter().filter_map(|f| f); - rcfiles.map(PathUpdateMethod::RcFile).collect() + profiles.into_iter().map(PathUpdateMethod::RcFile).collect() } fn shell_export_string() -> Result { From 8250cc00a9c82697c5d77940edb05b6fb5a236db Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 9 Jan 2020 12:19:43 +0700 Subject: [PATCH 2/2] Use Cow::into_owned to only clone when necessary --- src/cli/self_update.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 8266d2229d..609cfdbd8a 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -215,7 +215,7 @@ static UPDATE_ROOT: &str = "https://static.rust-lang.org/rustup"; /// substituted for the directory prefix fn canonical_cargo_home() -> Result { let path = utils::cargo_home()?; - let mut path_str = path.to_string_lossy().to_string(); + let mut path_str = path.to_string_lossy().into_owned(); let default_cargo_home = utils::home_dir() .unwrap_or_else(|| PathBuf::from(".")) @@ -1168,7 +1168,7 @@ fn do_add_to_path(methods: &[PathUpdateMethod]) -> Result<()> { let mut new_path = utils::cargo_home()? .join("bin") .to_string_lossy() - .to_string(); + .into_owned(); if old_path.contains(&new_path) { return Ok(()); } @@ -1283,7 +1283,7 @@ fn do_remove_from_path(methods: &[PathUpdateMethod]) -> Result<()> { let path_str = utils::cargo_home()? .join("bin") .to_string_lossy() - .to_string(); + .into_owned(); let idx = if let Some(i) = old_path.find(&path_str) { i } else {