Use strip_{prefix|suffix} instead of {starts|ends}_with+indexing

This commit is contained in:
Yotam Ofek 2025-03-16 15:54:04 +00:00
parent 8b87fefd76
commit a3e4dff183
7 changed files with 20 additions and 21 deletions

View file

@ -1632,8 +1632,8 @@ fn get_mut_span_in_struct_field<'tcx>(
/// If possible, suggest replacing `ref` with `ref mut`.
fn suggest_ref_mut(tcx: TyCtxt<'_>, span: Span) -> Option<Span> {
let pattern_str = tcx.sess.source_map().span_to_snippet(span).ok()?;
if pattern_str.starts_with("ref")
&& pattern_str["ref".len()..].starts_with(rustc_lexer::is_whitespace)
if let Some(rest) = pattern_str.strip_prefix("ref")
&& rest.starts_with(rustc_lexer::is_whitespace)
{
let span = span.with_lo(span.lo() + BytePos(4)).shrink_to_lo();
Some(span)

View file

@ -1538,8 +1538,13 @@ fn print_native_static_libs(
}
let stem = path.file_stem().unwrap().to_str().unwrap();
// Convert library file-stem into a cc -l argument.
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
let lib = &stem[prefix..];
let lib = if let Some(lib) = stem.strip_prefix("lib")
&& !sess.target.is_like_windows
{
lib
} else {
stem
};
let path = parent.unwrap_or_else(|| Path::new(""));
if sess.target.is_like_msvc {
// When producing a dll, the MSVC linker may not actually emit a

View file

@ -457,8 +457,7 @@ pub enum Compilation {
fn handle_explain(early_dcx: &EarlyDiagCtxt, registry: Registry, code: &str, color: ColorConfig) {
// Allow "E0123" or "0123" form.
let upper_cased_code = code.to_ascii_uppercase();
let start = if upper_cased_code.starts_with('E') { 1 } else { 0 };
if let Ok(code) = upper_cased_code[start..].parse::<u32>()
if let Ok(code) = upper_cased_code.strip_prefix('E').unwrap_or(&upper_cased_code).parse::<u32>()
&& let Ok(description) = registry.try_find_description(ErrCode::from_u32(code))
{
let mut is_in_code_block = false;

View file

@ -394,8 +394,8 @@ impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
symbol,
suffix,
span,
}) if symbol.as_str().starts_with('-') => {
let symbol = Symbol::intern(&symbol.as_str()[1..]);
}) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
let symbol = Symbol::intern(symbol);
let integer = TokenKind::lit(token::Integer, symbol, suffix);
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
let b = tokenstream::TokenTree::token_alone(integer, span);
@ -406,8 +406,8 @@ impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
symbol,
suffix,
span,
}) if symbol.as_str().starts_with('-') => {
let symbol = Symbol::intern(&symbol.as_str()[1..]);
}) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
let symbol = Symbol::intern(symbol);
let float = TokenKind::lit(token::Float, symbol, suffix);
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
let b = tokenstream::TokenTree::token_alone(float, span);

View file

@ -901,12 +901,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.iter()
.map(|(item, _)| format!("{} = Type", item.name))
.collect();
let code = if snippet.ends_with('>') {
let code = if let Some(snippet) = snippet.strip_suffix('>') {
// The user wrote `Trait<'a>` or similar and we don't have a type we can
// suggest, but at least we can clue them to the correct syntax
// `Trait<'a, Item = Type>` while accounting for the `<'a>` in the
// suggestion.
format!("{}, {}>", &snippet[..snippet.len() - 1], types.join(", "))
format!("{}, {}>", snippet, types.join(", "))
} else if in_expr_or_pat {
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
// least we can clue them to the correct syntax `Iterator::<Item = Type>`.

View file

@ -142,8 +142,8 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
if let Some((name, term)) = associated_ty {
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
// That should be extracted into a helper function.
if constraint.ends_with('>') {
constraint = format!("{}, {} = {}>", &constraint[..constraint.len() - 1], name, term);
if let Some(stripped) = constraint.strip_suffix('>') {
constraint = format!("{stripped}, {name} = {term}>");
} else {
constraint.push_str(&format!("<{name} = {term}>"));
}

View file

@ -390,13 +390,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if let Some((name, term)) = associated_ty {
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
// That should be extracted into a helper function.
if constraint.ends_with('>') {
constraint = format!(
"{}, {} = {}>",
&constraint[..constraint.len() - 1],
name,
term
);
if let Some(stripped) = constraint.strip_suffix('>') {
constraint = format!("{stripped}, {name} = {term}>");
} else {
constraint.push_str(&format!("<{name} = {term}>"));
}