Skip to content

Commit 4bd5f7d

Browse files
committed
Typo suggestion to change bindings with leading underscore
When encountering a binding that isn't found but has a typo suggestion for a binding with a leading underscore, suggest changing the binding definition instead of the use place. Fix #60164.
1 parent cc705b8 commit 4bd5f7d

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14621462
}
14631463
}
14641464

1465+
#[tracing::instrument(level = "info", skip(self, err))]
14651466
pub(crate) fn add_typo_suggestion(
14661467
&self,
14671468
err: &mut Diagnostic,
@@ -1511,17 +1512,30 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15111512
),
15121513
);
15131514
}
1515+
1516+
let (span, sugg, post) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
1517+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
1518+
&& let Some(span) = suggestion.span
1519+
&& suggestion.candidate.as_str().starts_with("_")
1520+
&& snippet == suggestion.candidate.as_str()[1..]
1521+
{
1522+
// When the suggested binding change would be from `x` to `_x`, suggest changing the
1523+
// original binding definition instead. (#60164)
1524+
(span, snippet, ", consider changing it")
1525+
} else {
1526+
(span, suggestion.candidate.to_string(), "")
1527+
};
15141528
let msg = match suggestion.target {
15151529
SuggestionTarget::SimilarlyNamed => format!(
1516-
"{} {} with a similar name exists",
1530+
"{} {} with a similar name exists{post}",
15171531
suggestion.res.article(),
15181532
suggestion.res.descr()
15191533
),
15201534
SuggestionTarget::SingleItem => {
15211535
format!("maybe you meant this {}", suggestion.res.descr())
15221536
}
15231537
};
1524-
err.span_suggestion(span, msg, suggestion.candidate, Applicability::MaybeIncorrect);
1538+
err.span_suggestion(span, msg, sugg, Applicability::MaybeIncorrect);
15251539
true
15261540
}
15271541

Diff for: tests/ui/suggestions/silenced-binding-typo.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// run-rustfix
2+
fn main() {
3+
let x = 42; //~ HELP
4+
let _y = x; //~ ERROR
5+
}

Diff for: tests/ui/suggestions/silenced-binding-typo.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// run-rustfix
2+
fn main() {
3+
let _x = 42; //~ HELP
4+
let _y = x; //~ ERROR
5+
}

Diff for: tests/ui/suggestions/silenced-binding-typo.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0425]: cannot find value `x` in this scope
2+
--> $DIR/silenced-binding-typo.rs:4:14
3+
|
4+
LL | let _y = x;
5+
| ^
6+
|
7+
help: a local variable with a similar name exists, consider changing it
8+
|
9+
LL | let x = 42;
10+
| ~
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)