Skip to content

Commit 2138b7a

Browse files
authored
Merge pull request #2274 from lzutao/escape-nt-backslash
Escape Windows backslash when outputing to markdown
2 parents 93e6d7d + cac1d3a commit 2138b7a

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/cli/self_update.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use rustup::utils::Notification;
4141
use rustup::{Cfg, UpdateStatus};
4242
use rustup::{DUP_TOOLS, TOOLS};
4343
use same_file::Handle;
44+
use std::borrow::Cow;
4445
use std::env;
4546
use std::env::consts::EXE_SUFFIX;
4647
use std::fs;
@@ -65,7 +66,7 @@ pub const NEVER_SELF_UPDATE: bool = false;
6566
// argument of format! needs to be a literal.
6667

6768
macro_rules! pre_install_msg_template {
68-
($platform_msg: expr) => {
69+
($platform_msg:literal) => {
6970
concat!(
7071
r"
7172
# Welcome to Rust!
@@ -215,22 +216,21 @@ static UPDATE_ROOT: &str = "https://static.rust-lang.org/rustup";
215216

216217
/// `CARGO_HOME` suitable for display, possibly with $HOME
217218
/// substituted for the directory prefix
218-
fn canonical_cargo_home() -> Result<String> {
219+
fn canonical_cargo_home() -> Result<Cow<'static, str>> {
219220
let path = utils::cargo_home()?;
220-
let mut path_str = path.to_string_lossy().into_owned();
221221

222222
let default_cargo_home = utils::home_dir()
223223
.unwrap_or_else(|| PathBuf::from("."))
224224
.join(".cargo");
225-
if default_cargo_home == path {
225+
Ok(if default_cargo_home == path {
226226
if cfg!(unix) {
227-
path_str = String::from("$HOME/.cargo");
227+
"$HOME/.cargo".into()
228228
} else {
229-
path_str = String::from(r"%USERPROFILE%\.cargo");
229+
r"%USERPROFILE%\.cargo".into()
230230
}
231-
}
232-
233-
Ok(path_str)
231+
} else {
232+
path.to_string_lossy().into_owned().into()
233+
})
234234
}
235235

236236
/// Installing is a simple matter of copying the running binary to
@@ -327,22 +327,19 @@ pub fn install(no_prompt: bool, verbose: bool, quiet: bool, mut opts: InstallOpt
327327
}
328328

329329
let cargo_home = canonical_cargo_home()?;
330-
let msg = if !opts.no_modify_path {
331-
if cfg!(unix) {
332-
format!(post_install_msg_unix!(), cargo_home = cargo_home)
333-
} else {
334-
format!(post_install_msg_win!(), cargo_home = cargo_home)
335-
}
336-
} else if cfg!(unix) {
337-
format!(
330+
#[cfg(windows)]
331+
let cargo_home = cargo_home.replace('\\', r"\\");
332+
let msg = match (opts.no_modify_path, cfg!(unix)) {
333+
(false, true) => format!(post_install_msg_unix!(), cargo_home = cargo_home),
334+
(false, false) => format!(post_install_msg_win!(), cargo_home = cargo_home),
335+
(true, true) => format!(
338336
post_install_msg_unix_no_modify_path!(),
339337
cargo_home = cargo_home
340-
)
341-
} else {
342-
format!(
338+
),
339+
(true, false) => format!(
343340
post_install_msg_win_no_modify_path!(),
344341
cargo_home = cargo_home
345-
)
342+
),
346343
};
347344
md(&mut term, msg);
348345

0 commit comments

Comments
 (0)