Skip to content

Commit 9bad8ac

Browse files
committed
Auto merge of #138566 - yotamofek:pr/strip-prefix, r=nnethercote
Use `strip_{prefix|suffix}` instead of `{starts|ends}_with`+indexing Randomly scratching an itch 😁
2 parents 10bcdad + a3e4dff commit 9bad8ac

File tree

7 files changed

+20
-21
lines changed

7 files changed

+20
-21
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

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

compiler/rustc_codegen_ssa/src/back/link.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1538,8 +1538,13 @@ fn print_native_static_libs(
15381538
}
15391539
let stem = path.file_stem().unwrap().to_str().unwrap();
15401540
// Convert library file-stem into a cc -l argument.
1541-
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
1542-
let lib = &stem[prefix..];
1541+
let lib = if let Some(lib) = stem.strip_prefix("lib")
1542+
&& !sess.target.is_like_windows
1543+
{
1544+
lib
1545+
} else {
1546+
stem
1547+
};
15431548
let path = parent.unwrap_or_else(|| Path::new(""));
15441549
if sess.target.is_like_msvc {
15451550
// When producing a dll, the MSVC linker may not actually emit a

compiler/rustc_driver_impl/src/lib.rs

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

compiler/rustc_expand/src/proc_macro_server.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
394394
symbol,
395395
suffix,
396396
span,
397-
}) if symbol.as_str().starts_with('-') => {
398-
let symbol = Symbol::intern(&symbol.as_str()[1..]);
397+
}) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
398+
let symbol = Symbol::intern(symbol);
399399
let integer = TokenKind::lit(token::Integer, symbol, suffix);
400400
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
401401
let b = tokenstream::TokenTree::token_alone(integer, span);
@@ -406,8 +406,8 @@ impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
406406
symbol,
407407
suffix,
408408
span,
409-
}) if symbol.as_str().starts_with('-') => {
410-
let symbol = Symbol::intern(&symbol.as_str()[1..]);
409+
}) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
410+
let symbol = Symbol::intern(symbol);
411411
let float = TokenKind::lit(token::Float, symbol, suffix);
412412
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
413413
let b = tokenstream::TokenTree::token_alone(float, span);

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

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

compiler/rustc_middle/src/ty/diagnostics.rs

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

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
390390
if let Some((name, term)) = associated_ty {
391391
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
392392
// That should be extracted into a helper function.
393-
if constraint.ends_with('>') {
394-
constraint = format!(
395-
"{}, {} = {}>",
396-
&constraint[..constraint.len() - 1],
397-
name,
398-
term
399-
);
393+
if let Some(stripped) = constraint.strip_suffix('>') {
394+
constraint = format!("{stripped}, {name} = {term}>");
400395
} else {
401396
constraint.push_str(&format!("<{name} = {term}>"));
402397
}

0 commit comments

Comments
 (0)