Skip to content

Suggest to bind self.x to x when field x may be in format string #141633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +769 to +771
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use maybe to imply that maybe x is in the format string, and add a comment here to clarify.

.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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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
|
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
Expand All @@ -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`.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading