Skip to content

Commit 42e9f2d

Browse files
committed
typo suggestion for a variable with a name similar to struct fields
1 parent 8fbd92d commit 42e9f2d

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
443443
);
444444
}
445445
}
446+
// Try Levenshtein algorithm.
447+
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
446448
if path.len() == 1 && self.self_type_is_available() {
447449
if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
448450
let self_is_available = self.self_value_is_available(path[0].ident.span);
@@ -452,18 +454,19 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
452454
err.span_suggestion(
453455
span,
454456
"you might have meant to use the available field",
455-
format!("self.{}", path_str),
457+
format!("self.{path_str}"),
456458
Applicability::MachineApplicable,
457459
);
458460
} else {
459461
err.span_label(span, "a field by this name exists in `Self`");
460462
}
463+
self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
461464
}
462465
AssocSuggestion::MethodWithSelf if self_is_available => {
463466
err.span_suggestion(
464467
span,
465468
"you might have meant to call the method",
466-
format!("self.{}", path_str),
469+
format!("self.{path_str}"),
467470
Applicability::MachineApplicable,
468471
);
469472
}
@@ -474,7 +477,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
474477
err.span_suggestion(
475478
span,
476479
&format!("you might have meant to {}", candidate.action()),
477-
format!("Self::{}", path_str),
480+
format!("Self::{path_str}"),
478481
Applicability::MachineApplicable,
479482
);
480483
}
@@ -493,16 +496,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
493496

494497
err.span_suggestion(
495498
call_span,
496-
&format!("try calling `{}` as a method", ident),
497-
format!("self.{}({})", path_str, args_snippet),
499+
&format!("try calling `{ident}` as a method"),
500+
format!("self.{path_str}({args_snippet})"),
498501
Applicability::MachineApplicable,
499502
);
500503
return (err, candidates);
501504
}
502505
}
503506

504-
// Try Levenshtein algorithm.
505-
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
506507
// Try context-dependent help if relaxed lookup didn't work.
507508
if let Some(res) = res {
508509
if self.smart_resolve_context_dependent_help(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Foo {
2+
config: String,
3+
}
4+
5+
impl Foo {
6+
fn new(cofig: String) -> Self {
7+
Self { config } //~ Error cannot find value `config` in this scope
8+
}
9+
10+
fn do_something(cofig: String) {
11+
println!("{config}"); //~ Error cannot find value `config` in this scope
12+
}
13+
14+
fn self_is_available(self, cofig: String) {
15+
println!("{config}"); //~ Error cannot find value `config` in this scope
16+
}
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0425]: cannot find value `config` in this scope
2+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:7:16
3+
|
4+
LL | Self { config }
5+
| ^^^^^^
6+
| |
7+
| a field by this name exists in `Self`
8+
| help: a local variable with a similar name exists: `cofig`
9+
10+
error[E0425]: cannot find value `config` in this scope
11+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:11:20
12+
|
13+
LL | println!("{config}");
14+
| ^^^^^^
15+
| |
16+
| a field by this name exists in `Self`
17+
| help: a local variable with a similar name exists: `cofig`
18+
19+
error[E0425]: cannot find value `config` in this scope
20+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20
21+
|
22+
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!("{cofig}");
32+
| ~~~~~
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)