Skip to content

Commit 620fd83

Browse files
Rollup merge of rust-lang#141633 - xizheyin:issue-141350, r=nnethercote
Suggest to bind `self.x` to `x` when field `x` may be in format string Fixes rust-lang#141350 I added the new test in the first commit, and committed the changes in the second one. r? @fmease cc @mejrs
2 parents 2d65699 + c4f14ad commit 620fd83

4 files changed

Lines changed: 38 additions & 11 deletions

File tree

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::ty;
2626
use rustc_session::{Session, lint};
2727
use rustc_span::edit_distance::{edit_distance, find_best_match_for_name};
2828
use rustc_span::edition::Edition;
29-
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
29+
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
3030
use thin_vec::ThinVec;
3131
use tracing::debug;
3232

@@ -980,12 +980,15 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
980980
AssocSuggestion::Field(field_span) => {
981981
if self_is_available {
982982
let source_map = self.r.tcx.sess.source_map();
983-
// check if the field is used in a format string, such as `"{x}"`
984-
let field_is_format_named_arg = source_map
983+
let field_is_format_named_arg = matches!(
984+
span.desugaring_kind(),
985+
Some(DesugaringKind::FormatLiteral { .. })
986+
) && source_map
985987
.span_to_source(span, |s, start, _| {
986-
Ok(s.get(start - 1..start) == Some("{"))
987-
});
988-
if let Ok(true) = field_is_format_named_arg {
988+
Ok(s.get(start.saturating_sub(1)..start) == Some("{"))
989+
})
990+
.unwrap_or(false);
991+
if field_is_format_named_arg {
989992
err.help(
990993
format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
991994
);

tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ impl Foo {
99
let _ = format!("{ x}"); //~ ERROR invalid format string: expected `}`, found `x`
1010
let _ = format!("{}", x); //~ ERROR cannot find value `x` in this scope [E0425]
1111
println!("{x}"); //~ ERROR cannot find value `x` in this scope [E0425]
12+
let _ = {x}; //~ERROR cannot find value `x` in this scope [E0425]
1213
}
1314
}
1415

tests/ui/resolve/suggestions/sugg-field-in-format-string-issue-141136.stderr

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ error[E0425]: cannot find value `x` in this scope
1414
LL | let _ = format!("{x}");
1515
| ^
1616
|
17-
= help: you might have meant to use the available field in a format string: `"{}", self.x`
17+
help: you might have meant to use the available field
18+
|
19+
LL | let _ = format!("{self.x}");
20+
| +++++
1821

1922
error[E0425]: cannot find value `x` in this scope
2023
--> $DIR/sugg-field-in-format-string-issue-141136.rs:8:27
2124
|
2225
LL | let _ = format!("{x }");
2326
| ^^
2427
|
25-
= help: you might have meant to use the available field in a format string: `"{}", self.x`
28+
help: you might have meant to use the available field
29+
|
30+
LL | let _ = format!("{self.x }");
31+
| +++++
2632

2733
error[E0425]: cannot find value `x` in this scope
2834
--> $DIR/sugg-field-in-format-string-issue-141136.rs:10:31
@@ -41,8 +47,22 @@ error[E0425]: cannot find value `x` in this scope
4147
LL | println!("{x}");
4248
| ^
4349
|
44-
= help: you might have meant to use the available field in a format string: `"{}", self.x`
50+
help: you might have meant to use the available field
51+
|
52+
LL | println!("{self.x}");
53+
| +++++
54+
55+
error[E0425]: cannot find value `x` in this scope
56+
--> $DIR/sugg-field-in-format-string-issue-141136.rs:12:18
57+
|
58+
LL | let _ = {x};
59+
| ^
60+
|
61+
help: you might have meant to use the available field
62+
|
63+
LL | let _ = {self.x};
64+
| +++++
4565

46-
error: aborting due to 5 previous errors
66+
error: aborting due to 6 previous errors
4767

4868
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ error[E0425]: cannot find value `config` in this scope
3434
LL | println!("{config}");
3535
| ^^^^^^
3636
|
37-
= help: you might have meant to use the available field in a format string: `"{}", self.config`
37+
help: you might have meant to use the available field
38+
|
39+
LL | println!("{self.config}");
40+
| +++++
3841
help: a local variable with a similar name exists
3942
|
4043
LL - println!("{config}");

0 commit comments

Comments
 (0)