Skip to content

Commit 059317d

Browse files
toolchain: avoid broad substring replacement in get_toolchain_binary
1 parent ea7f582 commit 059317d

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/toolchain.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
1111
let linker_file_name =
1212
linker.file_name().unwrap().to_str().expect("linker filename should be valid UTF-8");
1313

14-
if linker_file_name == "ld.lld" {
15-
if tool != "ld" {
16-
linker.set_file_name(tool)
17-
}
14+
let tool_file_name = if linker_file_name == "ld.lld" {
15+
if tool == "ld" { "ld.lld".to_owned() } else { tool.to_owned() }
16+
} else if let Some(prefix) = linker_file_name.strip_suffix("gcc") {
17+
format!("{prefix}{tool}")
18+
} else if let Some(prefix) = linker_file_name.strip_suffix("clang") {
19+
format!("{prefix}{tool}")
20+
} else if let Some(prefix) = linker_file_name.strip_suffix("cc") {
21+
format!("{prefix}{tool}")
22+
} else if let Some(prefix) = linker_file_name.strip_suffix("ld") {
23+
format!("{prefix}{tool}")
1824
} else {
19-
let tool_file_name = linker_file_name
20-
.replace("ld", tool)
21-
.replace("gcc", tool)
22-
.replace("clang", tool)
23-
.replace("cc", tool);
25+
linker_file_name.to_owned()
26+
};
2427

25-
linker.set_file_name(tool_file_name)
28+
if tool_file_name != linker_file_name {
29+
linker.set_file_name(tool_file_name);
2630
}
2731

2832
linker

0 commit comments

Comments
 (0)