Skip to content

Suggest use "{}", self.x instead of {self.x} when resolve x as field of self #141213

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

Merged
merged 4 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 24 additions & 6 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,12 +751,30 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
match candidate {
AssocSuggestion::Field(field_span) => {
if self_is_available {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"you might have meant to use the available field",
format!("{pre}self."),
Applicability::MachineApplicable,
);
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.span_to_source(span, |s, start, _| {
if let Some(expanded_expr) = s.get(start - 1..start)
&& expanded_expr.starts_with("{")
{
Ok(true)
} else {
Ok(false)
}
});
if let Ok(true) = 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),
);
} else {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"you might have meant to use the available field",
format!("{pre}self."),
Applicability::MachineApplicable,
);
}
} else {
err.span_label(field_span, "a field by that name exists in `Self`");
}
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

What happens on

struct Foo { x: i32 }

impl Foo {
    fn foo(&self) { _ = {x}; }
}

Does it essentially suggest fn foo(&self) { _ = "{}", self.x } (sic!)?

Copy link
Member

Choose a reason for hiding this comment

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

I've opened issue #141350 to track this regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will give this message.

error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:4:26
  |
4 |     fn foo(&self) { _ = {x}; }
  |                          ^
  |
  = help: you might have meant to use the available field in a format string: `"{}", self.x`

For further improvement, below two suggestions may be better?

The first suggestion is

help: you might have meant to use the available field if in a format string: `"{}", self.x`

and keep the origin suggestion

help: you might have meant to use the available field
  |
4 |     fn foo(&self) { _ = {self.x}; }
  |                          +++++

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the message as emitted right now (on master) is not correct. It should indeed suggest you might have meant to use the available field as we do on beta/stable.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct Foo {
x: i32
}

impl Foo {
fn foo(&self) {
let _ = format!("{x}"); //~ ERROR cannot find value `x` in this scope [E0425]
let _ = format!("{x }"); //~ ERROR cannot find value `x` in this scope [E0425]
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]
}
}

fn main(){}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error: invalid format string: expected `}`, found `x`
--> $DIR/sugg-field-in-format-string-issue-141136.rs:9:28
|
LL | let _ = format!("{ x}");
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error[E0425]: cannot find value `x` in this scope
--> $DIR/sugg-field-in-format-string-issue-141136.rs:7:27
|
LL | let _ = format!("{x}");
| ^
|
= help: you might have meant to use the available field in a format string: `"{}", 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`

error[E0425]: cannot find value `x` in this scope
--> $DIR/sugg-field-in-format-string-issue-141136.rs:10:31
|
LL | let _ = format!("{}", x);
| ^
|
help: you might have meant to use the available field
|
LL | let _ = format!("{}", self.x);
| +++++

error[E0425]: cannot find value `x` in this scope
--> $DIR/sugg-field-in-format-string-issue-141136.rs:11:20
|
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

For more information about this error, try `rustc --explain E0425`.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,9 @@ error[E0425]: cannot find value `config` in this scope
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20
|
LL | println!("{config}");
| ^^^^^^
|
help: you might have meant to use the available field
|
LL | println!("{self.config}");
| +++++
help: a local variable with a similar name exists
|
LL - println!("{config}");
LL + println!("{cofig}");
| ^^^^^^ 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`

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