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

bindings_with_variant_name is unhelpful and incorrect for function arguments #88730

Closed
RDambrosio016 opened this issue Sep 7, 2021 · 3 comments · Fixed by #89314
Closed

bindings_with_variant_name is unhelpful and incorrect for function arguments #88730

RDambrosio016 opened this issue Sep 7, 2021 · 3 comments · Fixed by #89314
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RDambrosio016
Copy link
Contributor

Given the following code:

#![allow(non_camel_case_types, unused_variables, dead_code)]

enum Foo {
    c
}

fn foo(c: Foo) {}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e6829290d29151853fdd98844ba6391c

The current output is:

warning[E0170]: pattern binding `c` is named the same as one of the variants of the type `Foo`
 --> src/lib.rs:7:8
  |
7 | fn foo(c: Foo) {}
  |        ^ help: to match on the variant, qualify the path: `Foo::c`
  |
  = note: `#[warn(bindings_with_variant_name)]` on by default

Ideally the lint should not trigger on such a pattern, it does not really make sense and the suggestion is wrong altogether,

@RDambrosio016 RDambrosio016 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 7, 2021
@RDambrosio016
Copy link
Contributor Author

@rustbot label: +D-confusing +D-incorrect

@rustbot rustbot added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Sep 7, 2021
@nbdd0121
Copy link
Contributor

nbdd0121 commented Sep 7, 2021

Note that the suggestion isn't wrong in this particular case, because Foo::c can actually match any Foo. It's wrong only when Foo has 2+ variants.

@RDambrosio016
Copy link
Contributor Author

Right, the real world case of this was with the ascii crate with the AsciiChar enum, this is just a tiny test case

@camelid camelid added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. labels Sep 7, 2021
ehuss added a commit to ehuss/rust that referenced this issue Sep 30, 2021
…ariant-match, r=davidtwco

fix(lint): don't suggest refutable patterns to "fix" irrefutable bind

In function arguments and let bindings, do not suggest changing `C` to `Foo::C` unless `C` is the only variant of `Foo`, because it won't work.

The general warning is still kept, because code like this is confusing.

Fixes rust-lang#88730

p.s. `src/test/ui/lint/lint-uppercase-variables.rs` already tests the one-variant case.
Manishearth added a commit to Manishearth/rust that referenced this issue Sep 30, 2021
…ariant-match, r=davidtwco

fix(lint): don't suggest refutable patterns to "fix" irrefutable bind

In function arguments and let bindings, do not suggest changing `C` to `Foo::C` unless `C` is the only variant of `Foo`, because it won't work.

The general warning is still kept, because code like this is confusing.

Fixes rust-lang#88730

p.s. `src/test/ui/lint/lint-uppercase-variables.rs` already tests the one-variant case.
Manishearth added a commit to Manishearth/rust that referenced this issue Sep 30, 2021
…ariant-match, r=davidtwco

fix(lint): don't suggest refutable patterns to "fix" irrefutable bind

In function arguments and let bindings, do not suggest changing `C` to `Foo::C` unless `C` is the only variant of `Foo`, because it won't work.

The general warning is still kept, because code like this is confusing.

Fixes rust-lang#88730

p.s. `src/test/ui/lint/lint-uppercase-variables.rs` already tests the one-variant case.
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 1, 2021
…ariant-match, r=davidtwco

fix(lint): don't suggest refutable patterns to "fix" irrefutable bind

In function arguments and let bindings, do not suggest changing `C` to `Foo::C` unless `C` is the only variant of `Foo`, because it won't work.

The general warning is still kept, because code like this is confusing.

Fixes rust-lang#88730

p.s. `src/test/ui/lint/lint-uppercase-variables.rs` already tests the one-variant case.
@bors bors closed this as completed in 6e973f0 Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants