Skip to content

Confusing compiler suggestion with type/variable with underscore prefix #125650

Closed
@Patiga

Description

@Patiga

Code

fn a(_p: i32) {
    let _ = p;
}

Current output

error[E0425]: cannot find value `p` in this scope
 --> <source>:2:13
  |
2 |     let _ = p;
  |             ^
  |
help: a local variable with a similar name exists, consider renaming `_p` into `p`
  |
1 | fn a(p: i32) {
  |      ~

Desired output

error[E0425]: cannot find value `p` in this scope
 --> <source>:2:13
  |
2 |     let _ = p;
  |             ^ help: a local variable with a similar name exists: `_p`
  |
help: The leading underscore in `_p` marks it as unused, consider changing its name to `p`
  |
1 | fn a(_p: i32) {
  |      --

Rationale and extra context

Compiler explorer link: https://godbolt.org/z/heYWW481h

This kind of diagnostic happens when you have a variable or type name with an underscore prefix.
When you try to access the variable/type without the underscore, the compiler tries to get the programmer to change the name of the variable/type, instead of the usage of it.

The compiler output was improved in 1.78 with #121776/#121792 by @GuillaumeGomez.

I personally find the current output still confusing. It shows the suggested improvement of changing the type/variable name, without showing the original code. As the compiler tells me about an error in the usage, I expect the other code it shows me to be unmodified.

You also have to read very closely that it wants you to change the variable, and not the usage. As the original error is in the line with the usage, I expected Rust to tell me how to correct that line. To be fair, I was on rustc version 1.77, but the error messages I am talking about here are from 1.78, for which I technically don't have first-hand-experience.

Other cases

No response

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions