Skip to content

Commit 7e5528f

Browse files
Rollup merge of #125795 - lucasscharenbroch:undescore-prefix-suggestion, r=compiler-errors
Improve renaming suggestion for names with leading underscores Fixes #125650 Before: ``` error[E0425]: cannot find value `p` in this scope --> test.rs:2:13 | 2 | let _ = p; | ^ | help: a local variable with a similar name exists, consider renaming `_p` into `p` | 1 | fn a(p: i32) { | ~ ``` After: ``` error[E0425]: cannot find value `p` in this scope --> test.rs:2:13 | 1 | fn a(_p: i32) { | -- `_p` defined here 2 | let _ = p; | ^ | help: the leading underscore in `_p` marks it as unused, consider renaming it to `p` | 1 | fn a(p: i32) { | ~ ``` This change doesn't exactly conform to what was proposed in the issue: 1. I've kept the suggested code instead of solely replacing it with the label 2. I've removed the "...similar name exists..." message instead of relocating to the usage span 3. You could argue that it still isn't completely clear that the change is referring to the definition (not the usage), but I'm not sure how to do this without playing down the fact that the error was caused by the usage of an undefined name.
2 parents 46a0339 + 9af9674 commit 7e5528f

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15621562
Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
15631563
Some(suggestion) => suggestion,
15641564
};
1565+
1566+
let mut did_label_def_span = false;
1567+
15651568
if let Some(def_span) = suggestion.res.opt_def_id().map(|def_id| self.def_span(def_id)) {
15661569
if span.overlaps(def_span) {
15671570
// Don't suggest typo suggestion for itself like in the following:
@@ -1595,31 +1598,38 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15951598
errors::DefinedHere::SingleItem { span, candidate_descr, candidate }
15961599
}
15971600
};
1601+
did_label_def_span = true;
15981602
err.subdiagnostic(self.tcx.dcx(), label);
15991603
}
16001604

1601-
let (span, sugg, post) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
1605+
let (span, msg, sugg) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
16021606
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
16031607
&& let Some(span) = suggestion.span
16041608
&& let Some(candidate) = suggestion.candidate.as_str().strip_prefix('_')
16051609
&& snippet == candidate
16061610
{
1611+
let candidate = suggestion.candidate;
16071612
// When the suggested binding change would be from `x` to `_x`, suggest changing the
16081613
// original binding definition instead. (#60164)
1609-
let post = format!(", consider renaming `{}` into `{snippet}`", suggestion.candidate);
1610-
(span, snippet, post)
1611-
} else {
1612-
(span, suggestion.candidate.to_ident_string(), String::new())
1613-
};
1614-
let msg = match suggestion.target {
1615-
SuggestionTarget::SimilarlyNamed => format!(
1616-
"{} {} with a similar name exists{post}",
1617-
suggestion.res.article(),
1618-
suggestion.res.descr()
1619-
),
1620-
SuggestionTarget::SingleItem => {
1621-
format!("maybe you meant this {}", suggestion.res.descr())
1614+
let msg = format!(
1615+
"the leading underscore in `{candidate}` marks it as unused, consider renaming it to `{snippet}`"
1616+
);
1617+
if !did_label_def_span {
1618+
err.span_label(span, format!("`{candidate}` defined here"));
16221619
}
1620+
(span, msg, snippet)
1621+
} else {
1622+
let msg = match suggestion.target {
1623+
SuggestionTarget::SimilarlyNamed => format!(
1624+
"{} {} with a similar name exists",
1625+
suggestion.res.article(),
1626+
suggestion.res.descr()
1627+
),
1628+
SuggestionTarget::SingleItem => {
1629+
format!("maybe you meant this {}", suggestion.res.descr())
1630+
}
1631+
};
1632+
(span, msg, suggestion.candidate.to_ident_string())
16231633
};
16241634
err.span_suggestion(span, msg, sugg, Applicability::MaybeIncorrect);
16251635
true

tests/ui/macros/expand-full-no-resolution.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | macro_rules! _a {
77
LL | format_args!(a!());
88
| ^
99
|
10-
help: a macro with a similar name exists, consider renaming `_a` into `a`
10+
help: the leading underscore in `_a` marks it as unused, consider renaming it to `a`
1111
|
1212
LL | macro_rules! a {
1313
| ~
@@ -21,7 +21,7 @@ LL | macro_rules! _a {
2121
LL | env!(a!());
2222
| ^
2323
|
24-
help: a macro with a similar name exists, consider renaming `_a` into `a`
24+
help: the leading underscore in `_a` marks it as unused, consider renaming it to `a`
2525
|
2626
LL | macro_rules! a {
2727
| ~

tests/ui/suggestions/silenced-binding-typo.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
error[E0425]: cannot find value `x` in this scope
22
--> $DIR/silenced-binding-typo.rs:4:14
33
|
4+
LL | let _x = 42;
5+
| -- `_x` defined here
46
LL | let _y = x;
57
| ^
68
|
7-
help: a local variable with a similar name exists, consider renaming `_x` into `x`
9+
help: the leading underscore in `_x` marks it as unused, consider renaming it to `x`
810
|
911
LL | let x = 42;
1012
| ~

0 commit comments

Comments
 (0)