Use strip_{prefix|suffix}
instead of {starts|ends}_with
+indexing
This commit is contained in:
parent
8b87fefd76
commit
a3e4dff183
7 changed files with 20 additions and 21 deletions
|
@ -1632,8 +1632,8 @@ fn get_mut_span_in_struct_field<'tcx>(
|
||||||
/// If possible, suggest replacing `ref` with `ref mut`.
|
/// If possible, suggest replacing `ref` with `ref mut`.
|
||||||
fn suggest_ref_mut(tcx: TyCtxt<'_>, span: Span) -> Option<Span> {
|
fn suggest_ref_mut(tcx: TyCtxt<'_>, span: Span) -> Option<Span> {
|
||||||
let pattern_str = tcx.sess.source_map().span_to_snippet(span).ok()?;
|
let pattern_str = tcx.sess.source_map().span_to_snippet(span).ok()?;
|
||||||
if pattern_str.starts_with("ref")
|
if let Some(rest) = pattern_str.strip_prefix("ref")
|
||||||
&& pattern_str["ref".len()..].starts_with(rustc_lexer::is_whitespace)
|
&& rest.starts_with(rustc_lexer::is_whitespace)
|
||||||
{
|
{
|
||||||
let span = span.with_lo(span.lo() + BytePos(4)).shrink_to_lo();
|
let span = span.with_lo(span.lo() + BytePos(4)).shrink_to_lo();
|
||||||
Some(span)
|
Some(span)
|
||||||
|
|
|
@ -1538,8 +1538,13 @@ fn print_native_static_libs(
|
||||||
}
|
}
|
||||||
let stem = path.file_stem().unwrap().to_str().unwrap();
|
let stem = path.file_stem().unwrap().to_str().unwrap();
|
||||||
// Convert library file-stem into a cc -l argument.
|
// 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 = if let Some(lib) = stem.strip_prefix("lib")
|
||||||
let lib = &stem[prefix..];
|
&& !sess.target.is_like_windows
|
||||||
|
{
|
||||||
|
lib
|
||||||
|
} else {
|
||||||
|
stem
|
||||||
|
};
|
||||||
let path = parent.unwrap_or_else(|| Path::new(""));
|
let path = parent.unwrap_or_else(|| Path::new(""));
|
||||||
if sess.target.is_like_msvc {
|
if sess.target.is_like_msvc {
|
||||||
// When producing a dll, the MSVC linker may not actually emit a
|
// When producing a dll, the MSVC linker may not actually emit a
|
||||||
|
|
|
@ -457,8 +457,7 @@ pub enum Compilation {
|
||||||
fn handle_explain(early_dcx: &EarlyDiagCtxt, registry: Registry, code: &str, color: ColorConfig) {
|
fn handle_explain(early_dcx: &EarlyDiagCtxt, registry: Registry, code: &str, color: ColorConfig) {
|
||||||
// Allow "E0123" or "0123" form.
|
// Allow "E0123" or "0123" form.
|
||||||
let upper_cased_code = code.to_ascii_uppercase();
|
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.strip_prefix('E').unwrap_or(&upper_cased_code).parse::<u32>()
|
||||||
if let Ok(code) = upper_cased_code[start..].parse::<u32>()
|
|
||||||
&& let Ok(description) = registry.try_find_description(ErrCode::from_u32(code))
|
&& let Ok(description) = registry.try_find_description(ErrCode::from_u32(code))
|
||||||
{
|
{
|
||||||
let mut is_in_code_block = false;
|
let mut is_in_code_block = false;
|
||||||
|
|
|
@ -394,8 +394,8 @@ impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
|
||||||
symbol,
|
symbol,
|
||||||
suffix,
|
suffix,
|
||||||
span,
|
span,
|
||||||
}) if symbol.as_str().starts_with('-') => {
|
}) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
|
||||||
let symbol = Symbol::intern(&symbol.as_str()[1..]);
|
let symbol = Symbol::intern(symbol);
|
||||||
let integer = TokenKind::lit(token::Integer, symbol, suffix);
|
let integer = TokenKind::lit(token::Integer, symbol, suffix);
|
||||||
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
|
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
|
||||||
let b = tokenstream::TokenTree::token_alone(integer, span);
|
let b = tokenstream::TokenTree::token_alone(integer, span);
|
||||||
|
@ -406,8 +406,8 @@ impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
|
||||||
symbol,
|
symbol,
|
||||||
suffix,
|
suffix,
|
||||||
span,
|
span,
|
||||||
}) if symbol.as_str().starts_with('-') => {
|
}) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
|
||||||
let symbol = Symbol::intern(&symbol.as_str()[1..]);
|
let symbol = Symbol::intern(symbol);
|
||||||
let float = TokenKind::lit(token::Float, symbol, suffix);
|
let float = TokenKind::lit(token::Float, symbol, suffix);
|
||||||
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
|
let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
|
||||||
let b = tokenstream::TokenTree::token_alone(float, span);
|
let b = tokenstream::TokenTree::token_alone(float, span);
|
||||||
|
|
|
@ -901,12 +901,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(item, _)| format!("{} = Type", item.name))
|
.map(|(item, _)| format!("{} = Type", item.name))
|
||||||
.collect();
|
.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
|
// 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
|
// suggest, but at least we can clue them to the correct syntax
|
||||||
// `Trait<'a, Item = Type>` while accounting for the `<'a>` in the
|
// `Trait<'a, Item = Type>` while accounting for the `<'a>` in the
|
||||||
// suggestion.
|
// suggestion.
|
||||||
format!("{}, {}>", &snippet[..snippet.len() - 1], types.join(", "))
|
format!("{}, {}>", snippet, types.join(", "))
|
||||||
} else if in_expr_or_pat {
|
} else if in_expr_or_pat {
|
||||||
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
|
// 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>`.
|
// least we can clue them to the correct syntax `Iterator::<Item = Type>`.
|
||||||
|
|
|
@ -142,8 +142,8 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
|
||||||
if let Some((name, term)) = associated_ty {
|
if let Some((name, term)) = associated_ty {
|
||||||
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
|
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
|
||||||
// That should be extracted into a helper function.
|
// That should be extracted into a helper function.
|
||||||
if constraint.ends_with('>') {
|
if let Some(stripped) = constraint.strip_suffix('>') {
|
||||||
constraint = format!("{}, {} = {}>", &constraint[..constraint.len() - 1], name, term);
|
constraint = format!("{stripped}, {name} = {term}>");
|
||||||
} else {
|
} else {
|
||||||
constraint.push_str(&format!("<{name} = {term}>"));
|
constraint.push_str(&format!("<{name} = {term}>"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -390,13 +390,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
||||||
if let Some((name, term)) = associated_ty {
|
if let Some((name, term)) = associated_ty {
|
||||||
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
|
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
|
||||||
// That should be extracted into a helper function.
|
// That should be extracted into a helper function.
|
||||||
if constraint.ends_with('>') {
|
if let Some(stripped) = constraint.strip_suffix('>') {
|
||||||
constraint = format!(
|
constraint = format!("{stripped}, {name} = {term}>");
|
||||||
"{}, {} = {}>",
|
|
||||||
&constraint[..constraint.len() - 1],
|
|
||||||
name,
|
|
||||||
term
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
constraint.push_str(&format!("<{name} = {term}>"));
|
constraint.push_str(&format!("<{name} = {term}>"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue