1
Fork 0

typo suggestion for a variable with a name similar to struct fields

This commit is contained in:
Takayuki Maeda 2022-05-21 12:55:37 +09:00
parent 8fbd92d0b9
commit 42e9f2daf3
3 changed files with 63 additions and 7 deletions

View file

@ -443,6 +443,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
); );
} }
} }
// Try Levenshtein algorithm.
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
if path.len() == 1 && self.self_type_is_available() { if path.len() == 1 && self.self_type_is_available() {
if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) { if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
let self_is_available = self.self_value_is_available(path[0].ident.span); let self_is_available = self.self_value_is_available(path[0].ident.span);
@ -452,18 +454,19 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_suggestion( err.span_suggestion(
span, span,
"you might have meant to use the available field", "you might have meant to use the available field",
format!("self.{}", path_str), format!("self.{path_str}"),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} else { } else {
err.span_label(span, "a field by this name exists in `Self`"); err.span_label(span, "a field by this name exists in `Self`");
} }
self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
} }
AssocSuggestion::MethodWithSelf if self_is_available => { AssocSuggestion::MethodWithSelf if self_is_available => {
err.span_suggestion( err.span_suggestion(
span, span,
"you might have meant to call the method", "you might have meant to call the method",
format!("self.{}", path_str), format!("self.{path_str}"),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
@ -474,7 +477,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_suggestion( err.span_suggestion(
span, span,
&format!("you might have meant to {}", candidate.action()), &format!("you might have meant to {}", candidate.action()),
format!("Self::{}", path_str), format!("Self::{path_str}"),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
@ -493,16 +496,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_suggestion( err.span_suggestion(
call_span, call_span,
&format!("try calling `{}` as a method", ident), &format!("try calling `{ident}` as a method"),
format!("self.{}({})", path_str, args_snippet), format!("self.{path_str}({args_snippet})"),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
return (err, candidates); return (err, candidates);
} }
} }
// Try Levenshtein algorithm.
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
// Try context-dependent help if relaxed lookup didn't work. // Try context-dependent help if relaxed lookup didn't work.
if let Some(res) = res { if let Some(res) = res {
if self.smart_resolve_context_dependent_help( if self.smart_resolve_context_dependent_help(

View file

@ -0,0 +1,19 @@
struct Foo {
config: String,
}
impl Foo {
fn new(cofig: String) -> Self {
Self { config } //~ Error cannot find value `config` in this scope
}
fn do_something(cofig: String) {
println!("{config}"); //~ Error cannot find value `config` in this scope
}
fn self_is_available(self, cofig: String) {
println!("{config}"); //~ Error cannot find value `config` in this scope
}
}
fn main() {}

View file

@ -0,0 +1,36 @@
error[E0425]: cannot find value `config` in this scope
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:7:16
|
LL | Self { config }
| ^^^^^^
| |
| a field by this name exists in `Self`
| help: a local variable with a similar name exists: `cofig`
error[E0425]: cannot find value `config` in this scope
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:11:20
|
LL | println!("{config}");
| ^^^^^^
| |
| a field by this name exists in `Self`
| help: a local variable with a similar name exists: `cofig`
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!("{cofig}");
| ~~~~~
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0425`.