Skip to content

Commit

Permalink
Rollup merge of rust-lang#121792 - GuillaumeGomez:improve-suggestion,…
Browse files Browse the repository at this point in the history
… r=michaelwoerister

Improve renaming suggestion when item starts with underscore

Fixes rust-lang#121776.

It goes from:

```terminal
error[E0433]: failed to resolve: use of undeclared type `Foo`
 --> src/foo.rs:6:13
  |
6 |     let _ = Foo::Bar;
  |             ^^^ use of undeclared type `Foo`
  |
help: an enum with a similar name exists, consider changing it
  |
1 | enum Foo {
  |      ~~~
```

to:

```terminal
error[E0433]: failed to resolve: use of undeclared type `Foo`
 --> foo.rs:6:13
  |
6 |     let _ = Foo::Bar;
  |             ^^^ use of undeclared type `Foo`
  |
help: an enum with a similar name exists, consider renaming `_Foo` into `Foo`
  |
1 | enum Foo {
  |      ~~~

error: aborting due to 1 previous error
```
  • Loading branch information
GuillaumeGomez authored Feb 29, 2024
2 parents 7147112 + 451fd98 commit 8a74df9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,9 +1585,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
{
// When the suggested binding change would be from `x` to `_x`, suggest changing the
// original binding definition instead. (#60164)
(span, snippet, ", consider changing it")
let post = format!(", consider renaming `{}` into `{snippet}`", suggestion.candidate);
(span, snippet, post)
} else {
(span, suggestion.candidate.to_string(), "")
(span, suggestion.candidate.to_string(), String::new())
};
let msg = match suggestion.target {
SuggestionTarget::SimilarlyNamed => format!(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/silenced-binding-typo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0425]: cannot find value `x` in this scope
LL | let _y = x;
| ^
|
help: a local variable with a similar name exists, consider changing it
help: a local variable with a similar name exists, consider renaming `_x` into `x`
|
LL | let x = 42;
| ~
Expand Down

0 comments on commit 8a74df9

Please sign in to comment.