Skip to content

Commit 9de7fff

Browse files
committed
Suggest use "{}", self.x instead of {self.x} when resolve x as field of self
Signed-off-by: xizheyin <[email protected]>
1 parent fe0663c commit 9de7fff

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,30 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
751751
match candidate {
752752
AssocSuggestion::Field(field_span) => {
753753
if self_is_available {
754-
err.span_suggestion_verbose(
755-
span.shrink_to_lo(),
756-
"you might have meant to use the available field",
757-
format!("{pre}self."),
758-
Applicability::MachineApplicable,
759-
);
754+
let source_map = self.r.tcx.sess.source_map();
755+
// check if the field is used in a format string, such as `"{x}"`
756+
let field_is_format_named_arg =
757+
source_map.span_to_source(span, |s, start, _| {
758+
if let Some(expanded_expr) = s.get(start - 1..start)
759+
&& expanded_expr.starts_with("{")
760+
{
761+
Ok(true)
762+
} else {
763+
Ok(false)
764+
}
765+
});
766+
if let Ok(true) = field_is_format_named_arg {
767+
err.help(
768+
format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
769+
);
770+
} else {
771+
err.span_suggestion_verbose(
772+
span.shrink_to_lo(),
773+
"you might have meant to use the available field",
774+
format!("{pre}self."),
775+
Applicability::MachineApplicable,
776+
);
777+
}
760778
} else {
761779
err.span_label(field_span, "a field by that name exists in `Self`");
762780
}

tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,15 @@ error[E0425]: cannot find value `x` in this scope
1414
LL | let _ = format!("{x}");
1515
| ^
1616
|
17-
help: you might have meant to use the available field
18-
|
19-
LL | let _ = format!("{self.x}");
20-
| +++++
17+
= help: you might have meant to use the available field in a format string: `"{}", self.x`
2118

2219
error[E0425]: cannot find value `x` in this scope
2320
--> $DIR/sugg-field-in-format-string-issue-141136.rs:8:27
2421
|
2522
LL | let _ = format!("{x }");
2623
| ^^
2724
|
28-
help: you might have meant to use the available field
29-
|
30-
LL | let _ = format!("{self.x }");
31-
| +++++
25+
= help: you might have meant to use the available field in a format string: `"{}", self.x`
3226

3327
error[E0425]: cannot find value `x` in this scope
3428
--> $DIR/sugg-field-in-format-string-issue-141136.rs:10:31

tests/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,9 @@ error[E0425]: cannot find value `config` in this scope
2020
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20
2121
|
2222
LL | println!("{config}");
23-
| ^^^^^^
24-
|
25-
help: you might have meant to use the available field
26-
|
27-
LL | println!("{self.config}");
28-
| +++++
29-
help: a local variable with a similar name exists
30-
|
31-
LL - println!("{config}");
32-
LL + println!("{cofig}");
23+
| ^^^^^^ help: a local variable with a similar name exists: `cofig`
3324
|
25+
= help: you might have meant to use the available field in a format string: `"{}", self.config`
3426

3527
error[E0425]: cannot find value `bah` in this scope
3628
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:33:9

0 commit comments

Comments
 (0)