Skip to content
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

Typo suggestion to change bindings with leading underscore #116961

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 15 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,17 +1511,30 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
),
);
}

let (span, sugg, post) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
&& let Some(span) = suggestion.span
&& let Some(candidate) = suggestion.candidate.as_str().strip_prefix("_")
&& snippet == candidate
{
// When the suggested binding change would be from `x` to `_x`, suggest changing the
// original binding definition instead. (#60164)
(span, snippet, ", consider changing it")
} else {
(span, suggestion.candidate.to_string(), "")
};
let msg = match suggestion.target {
SuggestionTarget::SimilarlyNamed => format!(
"{} {} with a similar name exists",
"{} {} with a similar name exists{post}",
suggestion.res.article(),
suggestion.res.descr()
),
SuggestionTarget::SingleItem => {
format!("maybe you meant this {}", suggestion.res.descr())
}
};
err.span_suggestion(span, msg, suggestion.candidate, Applicability::MaybeIncorrect);
err.span_suggestion(span, msg, sugg, Applicability::MaybeIncorrect);
true
}

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/suggestions/silenced-binding-typo.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// run-rustfix
fn main() {
let x = 42; //~ HELP
let _y = x; //~ ERROR
}
5 changes: 5 additions & 0 deletions tests/ui/suggestions/silenced-binding-typo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// run-rustfix
fn main() {
let _x = 42; //~ HELP
let _y = x; //~ ERROR
}
14 changes: 14 additions & 0 deletions tests/ui/suggestions/silenced-binding-typo.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0425]: cannot find value `x` in this scope
--> $DIR/silenced-binding-typo.rs:4:14
|
LL | let _y = x;
| ^
|
help: a local variable with a similar name exists, consider changing it
|
LL | let x = 42;
| ~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.
Loading