Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 7 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 4 additions & 4 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
@@ -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>`.
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -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}>"));
}
Original file line number Diff line number Diff line change
@@ -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}>"));
}
Loading