1
Fork 0

use strip_prefix over starts_with and manual slicing based on pattern length (clippy::manual_strip)

This commit is contained in:
Matthias Krüger 2020-09-17 10:13:16 +02:00
parent 95386b656e
commit 012974da7a
8 changed files with 28 additions and 26 deletions

View file

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

View file

@ -492,8 +492,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
{ {
if let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span) if let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
{ {
if pat_snippet.starts_with('&') { if let Some(stripped) = pat_snippet.strip_prefix('&') {
let pat_snippet = pat_snippet[1..].trim_start(); let pat_snippet = stripped.trim_start();
let (suggestion, to_remove) = if pat_snippet.starts_with("mut") let (suggestion, to_remove) = if pat_snippet.starts_with("mut")
&& pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace) && pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace)
{ {

View file

@ -631,9 +631,8 @@ fn suggest_ampmut<'tcx>(
let lt_name = &src[1..ws_pos]; let lt_name = &src[1..ws_pos];
let ty = &src[ws_pos..]; let ty = &src[ws_pos..];
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty)); return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
} else if src.starts_with('&') { } else if let Some(stripped) = src.strip_prefix('&') {
let borrowed_expr = &src[1..]; return (assignment_rhs_span, format!("&mut {}", stripped));
return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
} }
} }
} }

View file

@ -1418,9 +1418,9 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
if snippet.starts_with('&') && !snippet.starts_with("&'") { if snippet.starts_with('&') && !snippet.starts_with("&'") {
introduce_suggestion introduce_suggestion
.push((param.span, format!("&{} {}", lt_name, &snippet[1..]))); .push((param.span, format!("&{} {}", lt_name, &snippet[1..])));
} else if snippet.starts_with("&'_ ") { } else if let Some(stripped) = snippet.strip_prefix("&'_ ") {
introduce_suggestion introduce_suggestion
.push((param.span, format!("&{} {}", lt_name, &snippet[4..]))); .push((param.span, format!("&{} {}", lt_name, stripped)));
} }
} }
} }

View file

@ -56,16 +56,16 @@ impl PathKind {
impl SearchPath { impl SearchPath {
pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self { pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self {
let (kind, path) = if path.starts_with("native=") { let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
(PathKind::Native, &path["native=".len()..]) (PathKind::Native, stripped)
} else if path.starts_with("crate=") { } else if let Some(stripped) = path.strip_prefix("crate=") {
(PathKind::Crate, &path["crate=".len()..]) (PathKind::Crate, stripped)
} else if path.starts_with("dependency=") { } else if let Some(stripped) = path.strip_prefix("dependency=") {
(PathKind::Dependency, &path["dependency=".len()..]) (PathKind::Dependency, stripped)
} else if path.starts_with("framework=") { } else if let Some(stripped) = path.strip_prefix("framework=") {
(PathKind::Framework, &path["framework=".len()..]) (PathKind::Framework, stripped)
} else if path.starts_with("all=") { } else if let Some(stripped) = path.strip_prefix("all=") {
(PathKind::All, &path["all=".len()..]) (PathKind::All, stripped)
} else { } else {
(PathKind::All, path) (PathKind::All, path)
}; };

View file

@ -370,7 +370,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{ {
let s = s.as_ref(); let s = s.as_ref();
let old = old.as_ref(); let old = old.as_ref();
if s.starts_with(old) { Some(new.as_ref().to_owned() + &s[old.len()..]) } else { None } if let Some(stripped) = s.strip_prefix(old) {
Some(new.as_ref().to_owned() + stripped)
} else {
None
}
} }
/// This function is used to determine potential "simple" improvements or users' errors and /// This function is used to determine potential "simple" improvements or users' errors and

View file

@ -589,10 +589,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else { } else {
msg msg
}, },
if lstring.starts_with('&') { if let Some(stripped) = lstring.strip_prefix('&') {
// let a = String::new(); // let a = String::new();
// let _ = &a + "bar"; // let _ = &a + "bar";
lstring[1..].to_string() stripped.to_string()
} else { } else {
format!("{}.to_owned()", lstring) format!("{}.to_owned()", lstring)
}, },
@ -617,10 +617,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
is_assign, is_assign,
) { ) {
(Ok(l), Ok(r), IsAssign::No) => { (Ok(l), Ok(r), IsAssign::No) => {
let to_string = if l.starts_with('&') { let to_string = if let Some(stripped) = l.strip_prefix('&') {
// let a = String::new(); let b = String::new(); // let a = String::new(); let b = String::new();
// let _ = &a + b; // let _ = &a + b;
l[1..].to_string() stripped.to_string()
} else { } else {
format!("{}.to_owned()", l) format!("{}.to_owned()", l)
}; };

View file

@ -2341,8 +2341,8 @@ fn from_target_feature(
item.span(), item.span(),
format!("`{}` is not valid for this target", feature), format!("`{}` is not valid for this target", feature),
); );
if feature.starts_with('+') { if let Some(stripped) = feature.strip_prefix('+') {
let valid = supported_target_features.contains_key(&feature[1..]); let valid = supported_target_features.contains_key(stripped);
if valid { if valid {
err.help("consider removing the leading `+` in the feature name"); err.help("consider removing the leading `+` in the feature name");
} }