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

Refactor: Use format_args_capture in some parts of rustc_parse #94955

Merged
Merged
Show file tree
Hide file tree
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_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'a> Parser<'a> {
Ok(attr::mk_attr_from_item(item, None, style, attr_sp))
} else {
let token_str = pprust::token_to_string(&this.token);
let msg = &format!("expected `#`, found `{}`", token_str);
let msg = &format!("expected `#`, found `{token_str}`");
Err(this.struct_span_err(this.token.span, msg))
}
})
Expand Down Expand Up @@ -421,7 +421,7 @@ impl<'a> Parser<'a> {
}

let found = pprust::token_to_string(&self.token);
let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
let msg = format!("expected unsuffixed literal or identifier, found `{found}`");
Err(self.struct_span_err(self.token.span, &msg))
}
}
Expand Down
28 changes: 14 additions & 14 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ impl<'a> Parser<'a> {
expect.clone()
};
(
format!("expected one of {}, found {}", expect, actual),
(self.prev_token.span.shrink_to_hi(), format!("expected one of {}", short_expect)),
format!("expected one of {expect}, found {actual}"),
(self.prev_token.span.shrink_to_hi(), format!("expected one of {short_expect}")),
)
} else if expected.is_empty() {
(
Expand All @@ -337,8 +337,8 @@ impl<'a> Parser<'a> {
)
} else {
(
format!("expected {}, found {}", expect, actual),
(self.prev_token.span.shrink_to_hi(), format!("expected {}", expect)),
format!("expected {expect}, found {actual}"),
(self.prev_token.span.shrink_to_hi(), format!("expected {expect}")),
)
};
self.last_unexpected_token_span = Some(self.token.span);
Expand Down Expand Up @@ -421,7 +421,7 @@ impl<'a> Parser<'a> {
String::new(),
Applicability::MachineApplicable,
);
err.note(&format!("the raw string started with {} `#`s", n_hashes));
err.note(&format!("the raw string started with {n_hashes} `#`s"));
true
}
_ => false,
Expand Down Expand Up @@ -1191,7 +1191,7 @@ impl<'a> Parser<'a> {
_ => None,
};
if let Some(name) = previous_item_kind_name {
err.help(&format!("{} declarations are not followed by a semicolon", name));
err.help(&format!("{name} declarations are not followed by a semicolon"));
}
}
err.emit();
Expand Down Expand Up @@ -1226,12 +1226,12 @@ impl<'a> Parser<'a> {
"expected `{}`, found {}",
token_str,
match (&self.token.kind, self.subparser_name) {
(token::Eof, Some(origin)) => format!("end of {}", origin),
(token::Eof, Some(origin)) => format!("end of {origin}"),
_ => this_token_str,
},
);
let mut err = self.struct_span_err(sp, &msg);
let label_exp = format!("expected `{}`", token_str);
let label_exp = format!("expected `{token_str}`");
match self.recover_closing_delimiter(&[t.clone()], err) {
Err(e) => err = e,
Ok(recovered) => {
Expand Down Expand Up @@ -1368,7 +1368,7 @@ impl<'a> Parser<'a> {
Applicability::MachineApplicable,
);
}
err.span_suggestion(lo.shrink_to_lo(), &format!("{}you can still access the deprecated `try!()` macro using the \"raw identifier\" syntax", prefix), "r#".to_string(), Applicability::MachineApplicable);
err.span_suggestion(lo.shrink_to_lo(), &format!("{prefix}you can still access the deprecated `try!()` macro using the \"raw identifier\" syntax"), "r#".to_string(), Applicability::MachineApplicable);
err.emit();
Ok(self.mk_expr_err(lo.to(hi)))
} else {
Expand Down Expand Up @@ -1504,7 +1504,7 @@ impl<'a> Parser<'a> {
delim.retain(|c| c != '`');
err.span_suggestion_short(
self.prev_token.span.shrink_to_hi(),
&format!("`{}` may belong here", delim),
&format!("`{delim}` may belong here"),
delim,
Applicability::MaybeIncorrect,
);
Expand Down Expand Up @@ -1698,7 +1698,7 @@ impl<'a> Parser<'a> {
(
ident,
"self: ".to_string(),
format!("{}: &{}TypeName", ident, mutab),
format!("{ident}: &{mutab}TypeName"),
"_: ".to_string(),
pat.span.shrink_to_lo(),
pat.span,
Expand Down Expand Up @@ -1826,7 +1826,7 @@ impl<'a> Parser<'a> {
let (span, msg) = match (&self.token.kind, self.subparser_name) {
(&token::Eof, Some(origin)) => {
let sp = self.sess.source_map().next_point(self.prev_token.span);
(sp, format!("expected expression, found end of {}", origin))
(sp, format!("expected expression, found end of {origin}"))
}
_ => (
self.token.span,
Expand Down Expand Up @@ -1975,8 +1975,8 @@ impl<'a> Parser<'a> {
(ty_generics, self.sess.source_map().span_to_snippet(param.span()))
{
let (span, sugg) = match &generics.params[..] {
[] => (generics.span, format!("<{}>", snippet)),
[.., generic] => (generic.span().shrink_to_hi(), format!(", {}", snippet)),
[] => (generics.span, format!("<{snippet}>")),
[.., generic] => (generic.span().shrink_to_hi(), format!(", {snippet}")),
};
err.multipart_suggestion(
"`const` parameters must be declared for the `impl`",
Expand Down
36 changes: 18 additions & 18 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl<'a> Parser<'a> {
AssocOp::NotEqual => "!=",
_ => unreachable!(),
};
self.struct_span_err(sp, &format!("invalid comparison operator `{}=`", sugg))
self.struct_span_err(sp, &format!("invalid comparison operator `{sugg}=`"))
.span_suggestion_short(
sp,
&format!("`{s}=` is not a valid comparison operator, use `{s}`", s = sugg),
Expand Down Expand Up @@ -441,10 +441,10 @@ impl<'a> Parser<'a> {

/// Error on `and` and `or` suggesting `&&` and `||` respectively.
fn error_bad_logical_op(&self, bad: &str, good: &str, english: &str) {
self.struct_span_err(self.token.span, &format!("`{}` is not a logical operator", bad))
self.struct_span_err(self.token.span, &format!("`{bad}` is not a logical operator"))
.span_suggestion_short(
self.token.span,
&format!("use `{}` to perform logical {}", good, english),
&format!("use `{good}` to perform logical {english}"),
good.to_string(),
Applicability::MachineApplicable,
)
Expand Down Expand Up @@ -766,9 +766,9 @@ impl<'a> Parser<'a> {
self.look_ahead(1, |t| t.span).to(span_after_type),
"interpreted as generic arguments",
)
.span_label(self.token.span, format!("not interpreted as {}", op_noun))
.span_label(self.token.span, format!("not interpreted as {op_noun}"))
.multipart_suggestion(
&format!("try {} the cast value", op_verb),
&format!("try {op_verb} the cast value"),
vec![
(expr.span.shrink_to_lo(), "(".to_string()),
(expr.span.shrink_to_hi(), ")".to_string()),
Expand Down Expand Up @@ -970,7 +970,7 @@ impl<'a> Parser<'a> {
fn error_unexpected_after_dot(&self) {
// FIXME Could factor this out into non_fatal_unexpected or something.
let actual = pprust::token_to_string(&self.token);
self.struct_span_err(self.token.span, &format!("unexpected token: `{}`", actual)).emit();
self.struct_span_err(self.token.span, &format!("unexpected token: `{actual}`")).emit();
}

// We need an identifier or integer, but the next token is a float.
Expand Down Expand Up @@ -1151,15 +1151,15 @@ impl<'a> Parser<'a> {
mem::replace(err, replacement_err).cancel();

err.multipart_suggestion(
&format!("if `{}` is a struct, use braces as delimiters", name),
&format!("if `{name}` is a struct, use braces as delimiters"),
vec![
(open_paren, " { ".to_string()),
(close_paren, " }".to_string()),
],
Applicability::MaybeIncorrect,
);
err.multipart_suggestion(
&format!("if `{}` is a function, use the arguments directly", name),
&format!("if `{name}` is a function, use the arguments directly"),
fields
.into_iter()
.map(|field| (field.span.until(field.expr.span), String::new()))
Expand Down Expand Up @@ -1776,9 +1776,9 @@ impl<'a> Parser<'a> {
)
.emit();
} else {
let msg = format!("invalid suffix `{}` for number literal", suf);
let msg = format!("invalid suffix `{suf}` for number literal");
self.struct_span_err(span, &msg)
.span_label(span, format!("invalid suffix `{}`", suf))
.span_label(span, format!("invalid suffix `{suf}`"))
.help("the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)")
.emit();
}
Expand All @@ -1791,9 +1791,9 @@ impl<'a> Parser<'a> {
let msg = format!("invalid width `{}` for float literal", &suf[1..]);
self.struct_span_err(span, &msg).help("valid widths are 32 and 64").emit();
} else {
let msg = format!("invalid suffix `{}` for float literal", suf);
let msg = format!("invalid suffix `{suf}` for float literal");
self.struct_span_err(span, &msg)
.span_label(span, format!("invalid suffix `{}`", suf))
.span_label(span, format!("invalid suffix `{suf}`"))
.help("valid suffixes are `f32` and `f64`")
.emit();
}
Expand All @@ -1805,7 +1805,7 @@ impl<'a> Parser<'a> {
2 => "binary",
_ => unreachable!(),
};
self.struct_span_err(span, &format!("{} float literal is not supported", descr))
self.struct_span_err(span, &format!("{descr} float literal is not supported"))
.span_label(span, "not supported")
.emit();
}
Expand All @@ -1825,7 +1825,7 @@ impl<'a> Parser<'a> {
let mut err = self
.sess
.span_diagnostic
.struct_span_warn(sp, &format!("suffixes on {} are invalid", kind));
.struct_span_warn(sp, &format!("suffixes on {kind} are invalid"));
err.note(&format!(
"`{}` is *temporarily* accepted on tuple index fields as it was \
incorrectly accepted on stable for a few releases",
Expand All @@ -1842,10 +1842,10 @@ impl<'a> Parser<'a> {
);
err
} else {
self.struct_span_err(sp, &format!("suffixes on {} are invalid", kind))
self.struct_span_err(sp, &format!("suffixes on {kind} are invalid"))
.forget_guarantee()
};
err.span_label(sp, format!("invalid suffix `{}`", suf));
err.span_label(sp, format!("invalid suffix `{suf}`"));
err.emit();
}
}
Expand Down Expand Up @@ -2211,7 +2211,7 @@ impl<'a> Parser<'a> {
let ctx = if is_ctx_else { "else" } else { "if" };
self.struct_span_err(last, "outer attributes are not allowed on `if` and `else` branches")
.span_label(branch_span, "the attributes are attached to this branch")
.span_label(ctx_span, format!("the branch belongs to this `{}`", ctx))
.span_label(ctx_span, format!("the branch belongs to this `{ctx}`"))
.span_suggestion(
span,
"remove the attributes",
Expand Down Expand Up @@ -2391,7 +2391,7 @@ impl<'a> Parser<'a> {
err.span_label(arrow_span, "while parsing the `match` arm starting here");
if stmts.len() > 1 {
err.multipart_suggestion(
&format!("surround the statement{} with a body", s),
&format!("surround the statement{s} with a body"),
vec![
(span.shrink_to_lo(), "{ ".to_string()),
(span.shrink_to_hi(), " }".to_string()),
Expand Down
Loading