use strip_prefix over slicing (clippy::manual_strip)

This commit is contained in:
Matthias Krüger 2020-12-11 17:32:03 +01:00
parent 2225ee1b62
commit 5c8de1cf49
4 changed files with 23 additions and 23 deletions

View File

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

View File

@ -201,10 +201,10 @@ fn main() {
cmd.args(&components); cmd.args(&components);
for lib in output(&mut cmd).split_whitespace() { for lib in output(&mut cmd).split_whitespace() {
let name = if lib.starts_with("-l") { let name = if let Some(stripped) = lib.strip_prefix("-l") {
&lib[2..] stripped
} else if lib.starts_with('-') { } else if let Some(stripped) = lib.strip_prefix('-') {
&lib[1..] stripped
} else if Path::new(lib).exists() { } else if Path::new(lib).exists() {
// On MSVC llvm-config will print the full name to libraries, but // On MSVC llvm-config will print the full name to libraries, but
// we're only interested in the name part // we're only interested in the name part
@ -241,17 +241,17 @@ fn main() {
cmd.arg(llvm_link_arg).arg("--ldflags"); cmd.arg(llvm_link_arg).arg("--ldflags");
for lib in output(&mut cmd).split_whitespace() { for lib in output(&mut cmd).split_whitespace() {
if is_crossed { if is_crossed {
if lib.starts_with("-LIBPATH:") { if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
println!("cargo:rustc-link-search=native={}", lib[9..].replace(&host, &target)); println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
} else if lib.starts_with("-L") { } else if let Some(stripped) = lib.strip_prefix("-L") {
println!("cargo:rustc-link-search=native={}", lib[2..].replace(&host, &target)); println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
} }
} else if lib.starts_with("-LIBPATH:") { } else if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
println!("cargo:rustc-link-search=native={}", &lib[9..]); println!("cargo:rustc-link-search=native={}", stripped);
} else if lib.starts_with("-l") { } else if let Some(stripped) = lib.strip_prefix("-l") {
println!("cargo:rustc-link-lib={}", &lib[2..]); println!("cargo:rustc-link-lib={}", stripped);
} else if lib.starts_with("-L") { } else if let Some(stripped) = lib.strip_prefix("-L") {
println!("cargo:rustc-link-search=native={}", &lib[2..]); println!("cargo:rustc-link-search=native={}", stripped);
} }
} }
@ -262,10 +262,10 @@ fn main() {
let llvm_linker_flags = tracked_env_var_os("LLVM_LINKER_FLAGS"); let llvm_linker_flags = tracked_env_var_os("LLVM_LINKER_FLAGS");
if let Some(s) = llvm_linker_flags { if let Some(s) = llvm_linker_flags {
for lib in s.into_string().unwrap().split_whitespace() { for lib in s.into_string().unwrap().split_whitespace() {
if lib.starts_with("-l") { if let Some(stripped) = lib.strip_prefix("-l") {
println!("cargo:rustc-link-lib={}", &lib[2..]); println!("cargo:rustc-link-lib={}", stripped);
} else if lib.starts_with("-L") { } else if let Some(stripped) = lib.strip_prefix("-L") {
println!("cargo:rustc-link-search=native={}", &lib[2..]); println!("cargo:rustc-link-search=native={}", stripped);
} }
} }
} }

View File

@ -1186,7 +1186,7 @@ pub fn sanitize_sh(path: &Path) -> String {
return change_drive(unc_to_lfs(&path)).unwrap_or(path); return change_drive(unc_to_lfs(&path)).unwrap_or(path);
fn unc_to_lfs(s: &str) -> &str { fn unc_to_lfs(s: &str) -> &str {
if s.starts_with("//?/") { &s[4..] } else { s } s.strip_prefix("//?/").unwrap_or(s)
} }
fn change_drive(s: &str) -> Option<String> { fn change_drive(s: &str) -> Option<String> {

View File

@ -139,9 +139,9 @@ fn map_line(s: &str) -> Line<'_> {
let trimmed = s.trim(); let trimmed = s.trim();
if trimmed.starts_with("##") { if trimmed.starts_with("##") {
Line::Shown(Cow::Owned(s.replacen("##", "#", 1))) Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))
} else if trimmed.starts_with("# ") { } else if let Some(stripped) = trimmed.strip_prefix("# ") {
// # text // # text
Line::Hidden(&trimmed[2..]) Line::Hidden(&stripped)
} else if trimmed == "#" { } else if trimmed == "#" {
// We cannot handle '#text' because it could be #[attr]. // We cannot handle '#text' because it could be #[attr].
Line::Hidden("") Line::Hidden("")