From 58fc81c84bf1a5eaedf59358a9d90a7493bbf03a Mon Sep 17 00:00:00 2001 From: xizheyin Date: Tue, 27 May 2025 14:27:59 +0800 Subject: [PATCH 1/2] Add testcase for sugg-field-in-format-string-issue-141136 Signed-off-by: xizheyin --- .../sugg-field-in-format-string-issue-141136.rs | 1 + .../sugg-field-in-format-string-issue-141136.stderr | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.rs b/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.rs index d2aa61186bcd0..f29ec4bfe7dd8 100644 --- a/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.rs +++ b/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.rs @@ -9,6 +9,7 @@ impl Foo { let _ = format!("{ x}"); //~ ERROR invalid format string: expected `}`, found `x` let _ = format!("{}", x); //~ ERROR cannot find value `x` in this scope [E0425] println!("{x}"); //~ ERROR cannot find value `x` in this scope [E0425] + let _ = {x}; //~ERROR cannot find value `x` in this scope [E0425] } } diff --git a/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr b/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr index 0a84848081d5c..757ae90932e0d 100644 --- a/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr +++ b/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr @@ -43,6 +43,14 @@ LL | println!("{x}"); | = help: you might have meant to use the available field in a format string: `"{}", self.x` -error: aborting due to 5 previous errors +error[E0425]: cannot find value `x` in this scope + --> $DIR/sugg-field-in-format-string-issue-141136.rs:12:18 + | +LL | let _ = {x}; + | ^ + | + = help: you might have meant to use the available field in a format string: `"{}", self.x` + +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0425`. From abf5c8c5b998f371da864846013abd9e2dd5384d Mon Sep 17 00:00:00 2001 From: xizheyin Date: Tue, 27 May 2025 14:33:03 +0800 Subject: [PATCH 2/2] Suggest to bind `self.x` to `x` when field `x` may be in format string Signed-off-by: xizheyin --- compiler/rustc_resolve/src/late/diagnostics.rs | 9 +++++---- .../sugg-field-in-format-string-issue-141136.stderr | 8 ++++---- ...for-variable-with-name-similar-to-struct-field.stderr | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index ca25cdc9563d9..b98bf146dde9e 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -766,14 +766,15 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { AssocSuggestion::Field(field_span) => { if self_is_available { 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 + // Check if the field is used in a format string, such as `{x}`. + // Note that both `let y = {x}` and `"{x}"` can match this pattern. + let maybe_field_is_format_named_arg = source_map .span_to_source(span, |s, start, _| { Ok(s.get(start - 1..start) == Some("{")) }); - if let Ok(true) = field_is_format_named_arg { + if let Ok(true) = maybe_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), + format!("you might have meant to use the available field, try to bind it: `let {} = self.{}`", segment.ident.name, segment.ident.name), ); } else { err.span_suggestion_verbose( diff --git a/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr b/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr index 757ae90932e0d..504b06e24aa55 100644 --- a/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr +++ b/tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr @@ -14,7 +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 in a format string: `"{}", self.x` + = help: you might have meant to use the available field, try to bind it: `let x = self.x` error[E0425]: cannot find value `x` in this scope --> $DIR/sugg-field-in-format-string-issue-141136.rs:8:27 @@ -22,7 +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 in a format string: `"{}", self.x` + = help: you might have meant to use the available field, try to bind it: `let x = self.x` error[E0425]: cannot find value `x` in this scope --> $DIR/sugg-field-in-format-string-issue-141136.rs:10:31 @@ -41,7 +41,7 @@ error[E0425]: cannot find value `x` in this scope LL | println!("{x}"); | ^ | - = help: you might have meant to use the available field in a format string: `"{}", self.x` + = help: you might have meant to use the available field, try to bind it: `let x = self.x` error[E0425]: cannot find value `x` in this scope --> $DIR/sugg-field-in-format-string-issue-141136.rs:12:18 @@ -49,7 +49,7 @@ error[E0425]: cannot find value `x` in this scope LL | let _ = {x}; | ^ | - = help: you might have meant to use the available field in a format string: `"{}", self.x` + = help: you might have meant to use the available field, try to bind it: `let x = self.x` error: aborting due to 6 previous errors diff --git a/tests/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr b/tests/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr index 1ecbfee17bc70..c559340bd0a66 100644 --- a/tests/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr +++ b/tests/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr @@ -22,7 +22,7 @@ error[E0425]: cannot find value `config` in this scope LL | println!("{config}"); | ^^^^^^ 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` + = help: you might have meant to use the available field, try to bind it: `let config = 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