Skip to content

Commit 012974d

Browse files
committed
use strip_prefix over starts_with and manual slicing based on pattern length (clippy::manual_strip)
1 parent 95386b6 commit 012974d

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

compiler/rustc_driver/src/args.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use std::fs;
44
use std::io;
55

66
pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
7-
if arg.starts_with('@') {
8-
let path = &arg[1..];
7+
if let Some(path) = arg.strip_prefix('@') {
98
let file = match fs::read_to_string(path) {
109
Ok(file) => file,
1110
Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {

compiler/rustc_mir/src/borrow_check/diagnostics/move_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
492492
{
493493
if let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
494494
{
495-
if pat_snippet.starts_with('&') {
496-
let pat_snippet = pat_snippet[1..].trim_start();
495+
if let Some(stripped) = pat_snippet.strip_prefix('&') {
496+
let pat_snippet = stripped.trim_start();
497497
let (suggestion, to_remove) = if pat_snippet.starts_with("mut")
498498
&& pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace)
499499
{

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,8 @@ fn suggest_ampmut<'tcx>(
631631
let lt_name = &src[1..ws_pos];
632632
let ty = &src[ws_pos..];
633633
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
634-
} else if src.starts_with('&') {
635-
let borrowed_expr = &src[1..];
636-
return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
634+
} else if let Some(stripped) = src.strip_prefix('&') {
635+
return (assignment_rhs_span, format!("&mut {}", stripped));
637636
}
638637
}
639638
}

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1418,9 +1418,9 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
14181418
if snippet.starts_with('&') && !snippet.starts_with("&'") {
14191419
introduce_suggestion
14201420
.push((param.span, format!("&{} {}", lt_name, &snippet[1..])));
1421-
} else if snippet.starts_with("&'_ ") {
1421+
} else if let Some(stripped) = snippet.strip_prefix("&'_ ") {
14221422
introduce_suggestion
1423-
.push((param.span, format!("&{} {}", lt_name, &snippet[4..])));
1423+
.push((param.span, format!("&{} {}", lt_name, stripped)));
14241424
}
14251425
}
14261426
}

compiler/rustc_session/src/search_paths.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ impl PathKind {
5656

5757
impl SearchPath {
5858
pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self {
59-
let (kind, path) = if path.starts_with("native=") {
60-
(PathKind::Native, &path["native=".len()..])
61-
} else if path.starts_with("crate=") {
62-
(PathKind::Crate, &path["crate=".len()..])
63-
} else if path.starts_with("dependency=") {
64-
(PathKind::Dependency, &path["dependency=".len()..])
65-
} else if path.starts_with("framework=") {
66-
(PathKind::Framework, &path["framework=".len()..])
67-
} else if path.starts_with("all=") {
68-
(PathKind::All, &path["all=".len()..])
59+
let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
60+
(PathKind::Native, stripped)
61+
} else if let Some(stripped) = path.strip_prefix("crate=") {
62+
(PathKind::Crate, stripped)
63+
} else if let Some(stripped) = path.strip_prefix("dependency=") {
64+
(PathKind::Dependency, stripped)
65+
} else if let Some(stripped) = path.strip_prefix("framework=") {
66+
(PathKind::Framework, stripped)
67+
} else if let Some(stripped) = path.strip_prefix("all=") {
68+
(PathKind::All, stripped)
6969
} else {
7070
(PathKind::All, path)
7171
};

compiler/rustc_typeck/src/check/demand.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
370370
{
371371
let s = s.as_ref();
372372
let old = old.as_ref();
373-
if s.starts_with(old) { Some(new.as_ref().to_owned() + &s[old.len()..]) } else { None }
373+
if let Some(stripped) = s.strip_prefix(old) {
374+
Some(new.as_ref().to_owned() + stripped)
375+
} else {
376+
None
377+
}
374378
}
375379

376380
/// This function is used to determine potential "simple" improvements or users' errors and

compiler/rustc_typeck/src/check/op.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
589589
} else {
590590
msg
591591
},
592-
if lstring.starts_with('&') {
592+
if let Some(stripped) = lstring.strip_prefix('&') {
593593
// let a = String::new();
594594
// let _ = &a + "bar";
595-
lstring[1..].to_string()
595+
stripped.to_string()
596596
} else {
597597
format!("{}.to_owned()", lstring)
598598
},
@@ -617,10 +617,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
617617
is_assign,
618618
) {
619619
(Ok(l), Ok(r), IsAssign::No) => {
620-
let to_string = if l.starts_with('&') {
620+
let to_string = if let Some(stripped) = l.strip_prefix('&') {
621621
// let a = String::new(); let b = String::new();
622622
// let _ = &a + b;
623-
l[1..].to_string()
623+
stripped.to_string()
624624
} else {
625625
format!("{}.to_owned()", l)
626626
};

compiler/rustc_typeck/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2341,8 +2341,8 @@ fn from_target_feature(
23412341
item.span(),
23422342
format!("`{}` is not valid for this target", feature),
23432343
);
2344-
if feature.starts_with('+') {
2345-
let valid = supported_target_features.contains_key(&feature[1..]);
2344+
if let Some(stripped) = feature.strip_prefix('+') {
2345+
let valid = supported_target_features.contains_key(stripped);
23462346
if valid {
23472347
err.help("consider removing the leading `+` in the feature name");
23482348
}

0 commit comments

Comments
 (0)