Skip to content

Commit 5c8de1c

Browse files
committed
use strip_prefix over slicing (clippy::manual_strip)
1 parent 2225ee1 commit 5c8de1c

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,8 @@ fn generic_simd_intrinsic(
854854
));
855855
}
856856

857-
if name_str.starts_with("simd_shuffle") {
858-
let n: u64 = name_str["simd_shuffle".len()..].parse().unwrap_or_else(|_| {
857+
if let Some(stripped) = name_str.strip_prefix("simd_shuffle") {
858+
let n: u64 = stripped.parse().unwrap_or_else(|_| {
859859
span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?")
860860
});
861861

compiler/rustc_llvm/build.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ fn main() {
201201
cmd.args(&components);
202202

203203
for lib in output(&mut cmd).split_whitespace() {
204-
let name = if lib.starts_with("-l") {
205-
&lib[2..]
206-
} else if lib.starts_with('-') {
207-
&lib[1..]
204+
let name = if let Some(stripped) = lib.strip_prefix("-l") {
205+
stripped
206+
} else if let Some(stripped) = lib.strip_prefix('-') {
207+
stripped
208208
} else if Path::new(lib).exists() {
209209
// On MSVC llvm-config will print the full name to libraries, but
210210
// we're only interested in the name part
@@ -241,17 +241,17 @@ fn main() {
241241
cmd.arg(llvm_link_arg).arg("--ldflags");
242242
for lib in output(&mut cmd).split_whitespace() {
243243
if is_crossed {
244-
if lib.starts_with("-LIBPATH:") {
245-
println!("cargo:rustc-link-search=native={}", lib[9..].replace(&host, &target));
246-
} else if lib.starts_with("-L") {
247-
println!("cargo:rustc-link-search=native={}", lib[2..].replace(&host, &target));
244+
if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
245+
println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
246+
} else if let Some(stripped) = lib.strip_prefix("-L") {
247+
println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
248248
}
249-
} else if lib.starts_with("-LIBPATH:") {
250-
println!("cargo:rustc-link-search=native={}", &lib[9..]);
251-
} else if lib.starts_with("-l") {
252-
println!("cargo:rustc-link-lib={}", &lib[2..]);
253-
} else if lib.starts_with("-L") {
254-
println!("cargo:rustc-link-search=native={}", &lib[2..]);
249+
} else if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
250+
println!("cargo:rustc-link-search=native={}", stripped);
251+
} else if let Some(stripped) = lib.strip_prefix("-l") {
252+
println!("cargo:rustc-link-lib={}", stripped);
253+
} else if let Some(stripped) = lib.strip_prefix("-L") {
254+
println!("cargo:rustc-link-search=native={}", stripped);
255255
}
256256
}
257257

@@ -262,10 +262,10 @@ fn main() {
262262
let llvm_linker_flags = tracked_env_var_os("LLVM_LINKER_FLAGS");
263263
if let Some(s) = llvm_linker_flags {
264264
for lib in s.into_string().unwrap().split_whitespace() {
265-
if lib.starts_with("-l") {
266-
println!("cargo:rustc-link-lib={}", &lib[2..]);
267-
} else if lib.starts_with("-L") {
268-
println!("cargo:rustc-link-search=native={}", &lib[2..]);
265+
if let Some(stripped) = lib.strip_prefix("-l") {
266+
println!("cargo:rustc-link-lib={}", stripped);
267+
} else if let Some(stripped) = lib.strip_prefix("-L") {
268+
println!("cargo:rustc-link-search=native={}", stripped);
269269
}
270270
}
271271
}

src/bootstrap/dist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ pub fn sanitize_sh(path: &Path) -> String {
11861186
return change_drive(unc_to_lfs(&path)).unwrap_or(path);
11871187

11881188
fn unc_to_lfs(s: &str) -> &str {
1189-
if s.starts_with("//?/") { &s[4..] } else { s }
1189+
s.strip_prefix("//?/").unwrap_or(s)
11901190
}
11911191

11921192
fn change_drive(s: &str) -> Option<String> {

src/librustdoc/html/markdown.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ fn map_line(s: &str) -> Line<'_> {
139139
let trimmed = s.trim();
140140
if trimmed.starts_with("##") {
141141
Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))
142-
} else if trimmed.starts_with("# ") {
142+
} else if let Some(stripped) = trimmed.strip_prefix("# ") {
143143
// # text
144-
Line::Hidden(&trimmed[2..])
144+
Line::Hidden(&stripped)
145145
} else if trimmed == "#" {
146146
// We cannot handle '#text' because it could be #[attr].
147147
Line::Hidden("")

0 commit comments

Comments
 (0)