Skip to content

Commit abf5c8c

Browse files
committed
Suggest to bind self.x to x when field x may be in format string
Signed-off-by: xizheyin <[email protected]>
1 parent 58fc81c commit abf5c8c

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,14 +766,15 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
766766
AssocSuggestion::Field(field_span) => {
767767
if self_is_available {
768768
let source_map = self.r.tcx.sess.source_map();
769-
// check if the field is used in a format string, such as `"{x}"`
770-
let field_is_format_named_arg = source_map
769+
// Check if the field is used in a format string, such as `{x}`.
770+
// Note that both `let y = {x}` and `"{x}"` can match this pattern.
771+
let maybe_field_is_format_named_arg = source_map
771772
.span_to_source(span, |s, start, _| {
772773
Ok(s.get(start - 1..start) == Some("{"))
773774
});
774-
if let Ok(true) = field_is_format_named_arg {
775+
if let Ok(true) = maybe_field_is_format_named_arg {
775776
err.help(
776-
format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
777+
format!("you might have meant to use the available field, try to bind it: `let {} = self.{}`", segment.ident.name, segment.ident.name),
777778
);
778779
} else {
779780
err.span_suggestion_verbose(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +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 in a format string: `"{}", self.x`
17+
= help: you might have meant to use the available field, try to bind it: `let x = self.x`
1818

1919
error[E0425]: cannot find value `x` in this scope
2020
--> $DIR/sugg-field-in-format-string-issue-141136.rs:8:27
2121
|
2222
LL | let _ = format!("{x }");
2323
| ^^
2424
|
25-
= help: you might have meant to use the available field in a format string: `"{}", self.x`
25+
= help: you might have meant to use the available field, try to bind it: `let x = self.x`
2626

2727
error[E0425]: cannot find value `x` in this scope
2828
--> $DIR/sugg-field-in-format-string-issue-141136.rs:10:31
@@ -41,15 +41,15 @@ error[E0425]: cannot find value `x` in this scope
4141
LL | println!("{x}");
4242
| ^
4343
|
44-
= help: you might have meant to use the available field in a format string: `"{}", self.x`
44+
= help: you might have meant to use the available field, try to bind it: `let x = self.x`
4545

4646
error[E0425]: cannot find value `x` in this scope
4747
--> $DIR/sugg-field-in-format-string-issue-141136.rs:12:18
4848
|
4949
LL | let _ = {x};
5050
| ^
5151
|
52-
= help: you might have meant to use the available field in a format string: `"{}", self.x`
52+
= help: you might have meant to use the available field, try to bind it: `let x = self.x`
5353

5454
error: aborting due to 6 previous errors
5555

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error[E0425]: cannot find value `config` in this scope
2222
LL | println!("{config}");
2323
| ^^^^^^ help: a local variable with a similar name exists: `cofig`
2424
|
25-
= help: you might have meant to use the available field in a format string: `"{}", self.config`
25+
= help: you might have meant to use the available field, try to bind it: `let config = self.config`
2626

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

0 commit comments

Comments
 (0)