Skip to content

Commit d30f047

Browse files
authored
Rollup merge of #141213 - xizheyin:issue-141136, r=nnethercote
Suggest use "{}", self.x instead of {self.x} when resolve x as field of `self` Fixes #141136 Changes can be seen in the second commit: 9de7fff r? compiler
2 parents 64a5a66 + 84f67a5 commit d30f047

4 files changed

+83
-16
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -765,12 +765,24 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
765765
match candidate {
766766
AssocSuggestion::Field(field_span) => {
767767
if self_is_available {
768-
err.span_suggestion_verbose(
769-
span.shrink_to_lo(),
770-
"you might have meant to use the available field",
771-
format!("{pre}self."),
772-
Applicability::MachineApplicable,
773-
);
768+
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
771+
.span_to_source(span, |s, start, _| {
772+
Ok(s.get(start - 1..start) == Some("{"))
773+
});
774+
if let Ok(true) = field_is_format_named_arg {
775+
err.help(
776+
format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
777+
);
778+
} else {
779+
err.span_suggestion_verbose(
780+
span.shrink_to_lo(),
781+
"you might have meant to use the available field",
782+
format!("{pre}self."),
783+
Applicability::MaybeIncorrect,
784+
);
785+
}
774786
} else {
775787
err.span_label(field_span, "a field by that name exists in `Self`");
776788
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct Foo {
2+
x: i32
3+
}
4+
5+
impl Foo {
6+
fn foo(&self) {
7+
let _ = format!("{x}"); //~ ERROR cannot find value `x` in this scope [E0425]
8+
let _ = format!("{x }"); //~ ERROR cannot find value `x` in this scope [E0425]
9+
let _ = format!("{ x}"); //~ ERROR invalid format string: expected `}`, found `x`
10+
let _ = format!("{}", x); //~ ERROR cannot find value `x` in this scope [E0425]
11+
println!("{x}"); //~ ERROR cannot find value `x` in this scope [E0425]
12+
}
13+
}
14+
15+
fn main(){}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error: invalid format string: expected `}`, found `x`
2+
--> $DIR/sugg-field-in-format-string-issue-141136.rs:9:28
3+
|
4+
LL | let _ = format!("{ x}");
5+
| - ^ expected `}` in format string
6+
| |
7+
| because of this opening brace
8+
|
9+
= note: if you intended to print `{`, you can escape it using `{{`
10+
11+
error[E0425]: cannot find value `x` in this scope
12+
--> $DIR/sugg-field-in-format-string-issue-141136.rs:7:27
13+
|
14+
LL | let _ = format!("{x}");
15+
| ^
16+
|
17+
= help: you might have meant to use the available field in a format string: `"{}", self.x`
18+
19+
error[E0425]: cannot find value `x` in this scope
20+
--> $DIR/sugg-field-in-format-string-issue-141136.rs:8:27
21+
|
22+
LL | let _ = format!("{x }");
23+
| ^^
24+
|
25+
= help: you might have meant to use the available field in a format string: `"{}", self.x`
26+
27+
error[E0425]: cannot find value `x` in this scope
28+
--> $DIR/sugg-field-in-format-string-issue-141136.rs:10:31
29+
|
30+
LL | let _ = format!("{}", x);
31+
| ^
32+
|
33+
help: you might have meant to use the available field
34+
|
35+
LL | let _ = format!("{}", self.x);
36+
| +++++
37+
38+
error[E0425]: cannot find value `x` in this scope
39+
--> $DIR/sugg-field-in-format-string-issue-141136.rs:11:20
40+
|
41+
LL | println!("{x}");
42+
| ^
43+
|
44+
= help: you might have meant to use the available field in a format string: `"{}", self.x`
45+
46+
error: aborting due to 5 previous errors
47+
48+
For more information about this error, try `rustc --explain E0425`.

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,9 @@ error[E0425]: cannot find value `config` in this scope
2020
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20
2121
|
2222
LL | println!("{config}");
23-
| ^^^^^^
24-
|
25-
help: you might have meant to use the available field
26-
|
27-
LL | println!("{self.config}");
28-
| +++++
29-
help: a local variable with a similar name exists
30-
|
31-
LL - println!("{config}");
32-
LL + println!("{cofig}");
23+
| ^^^^^^ help: a local variable with a similar name exists: `cofig`
3324
|
25+
= help: you might have meant to use the available field in a format string: `"{}", self.config`
3426

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

0 commit comments

Comments
 (0)