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.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..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,8 +41,16 @@ 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: 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, try to bind it: `let x = self.x` + +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0425`. 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