mirror of
https://github.com/rust-lang/rust.git
synced 2025-10-02 10:18:25 +00:00
Suggest use "{}", self.x
instead of {self.x}
when resolve x
as field of self
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
parent
fe0663c33d
commit
9de7fff0d8
@ -751,12 +751,30 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
|
||||
match candidate {
|
||||
AssocSuggestion::Field(field_span) => {
|
||||
if self_is_available {
|
||||
err.span_suggestion_verbose(
|
||||
span.shrink_to_lo(),
|
||||
"you might have meant to use the available field",
|
||||
format!("{pre}self."),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
let source_map = self.r.tcx.sess.source_map();
|
||||
// check if the field is used in a format string, such as `"{x}"`
|
||||
let field_is_format_named_arg =
|
||||
source_map.span_to_source(span, |s, start, _| {
|
||||
if let Some(expanded_expr) = s.get(start - 1..start)
|
||||
&& expanded_expr.starts_with("{")
|
||||
{
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
});
|
||||
if let Ok(true) = field_is_format_named_arg {
|
||||
err.help(
|
||||
format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
|
||||
);
|
||||
} else {
|
||||
err.span_suggestion_verbose(
|
||||
span.shrink_to_lo(),
|
||||
"you might have meant to use the available field",
|
||||
format!("{pre}self."),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
err.span_label(field_span, "a field by that name exists in `Self`");
|
||||
}
|
||||
|
@ -14,10 +14,7 @@ error[E0425]: cannot find value `x` in this scope
|
||||
LL | let _ = format!("{x}");
|
||||
| ^
|
||||
|
|
||||
help: you might have meant to use the available field
|
||||
|
|
||||
LL | let _ = format!("{self.x}");
|
||||
| +++++
|
||||
= help: you might have meant to use the available field in a format string: `"{}", self.x`
|
||||
|
||||
error[E0425]: cannot find value `x` in this scope
|
||||
--> $DIR/sugg-field-in-format-string-issue-141136.rs:8:27
|
||||
@ -25,10 +22,7 @@ error[E0425]: cannot find value `x` in this scope
|
||||
LL | let _ = format!("{x }");
|
||||
| ^^
|
||||
|
|
||||
help: you might have meant to use the available field
|
||||
|
|
||||
LL | let _ = format!("{self.x }");
|
||||
| +++++
|
||||
= help: you might have meant to use the available field in a format string: `"{}", self.x`
|
||||
|
||||
error[E0425]: cannot find value `x` in this scope
|
||||
--> $DIR/sugg-field-in-format-string-issue-141136.rs:10:31
|
||||
|
@ -20,17 +20,9 @@ error[E0425]: cannot find value `config` in this scope
|
||||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20
|
||||
|
|
||||
LL | println!("{config}");
|
||||
| ^^^^^^
|
||||
|
|
||||
help: you might have meant to use the available field
|
||||
|
|
||||
LL | println!("{self.config}");
|
||||
| +++++
|
||||
help: a local variable with a similar name exists
|
||||
|
|
||||
LL - println!("{config}");
|
||||
LL + println!("{cofig}");
|
||||
| ^^^^^^ help: a local variable with a similar name exists: `cofig`
|
||||
|
|
||||
= help: you might have meant to use the available field in a format string: `"{}", self.config`
|
||||
|
||||
error[E0425]: cannot find value `bah` in this scope
|
||||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:33:9
|
||||
|
Loading…
x
Reference in New Issue
Block a user