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

Rust shouldn't warn about snake case in patterns #66362

Closed
Timmmm opened this issue Nov 13, 2019 · 8 comments · Fixed by #66660
Closed

Rust shouldn't warn about snake case in patterns #66362

Timmmm opened this issue Nov 13, 2019 · 8 comments · Fixed by #66660
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Timmmm
Copy link
Contributor

Timmmm commented Nov 13, 2019

For example:

enum Animal {
  Spider {
    iHateSnakes: bool,
  },
  ...
}

...

match pet {
  Animal::Spider { iHateSnakes } =>
  ...

This gives you a warning that iHateSnakes should be snake case in the match arm. But at that point you don't really have much choice about it. Variable name warnings should be where the variable is named, and it isn't really named there. (Maybe there's some syntax like { i_hate_snakes @ iHateSnakes } but who is going to bother with that?)

This issue has been assigned to @jumbatm via this comment.

@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Nov 13, 2019
@hellow554
Copy link
Contributor

hellow554 commented Nov 13, 2019

Please provide a working example without ... next time. It makes it easier to see what's going on here.

Let's assume this one:

enum Animal {
    Spider { iHateSnakes: bool },
}

fn main() {
    let pet = Animal::Spider { iHateSnakes: false };

    match pet {
        Animal::Spider { iHateSnakes } => dbg!(iHateSnakes),
    };
}

You could instead write

match pet {
    Animal::Spider { iHateSnakes: i_hate_snakes } => dbg!(i_hate_snakes),
};

and have full control over the names of the binding and that's why the compiler is complaining.

You are right in the way, that we should lint the original issue. I'm not sure how the the suggestion goes in terms of machine applicability and renaming of further instances of this variable.
Let's see what the future™ will bring :)

@scottmcm
Copy link
Member

Direct repro link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f1197162b57e77fff49f0e9536aa6366

Current warning:

warning: variable `iHateSnakes` should have a snake case name
  --> src/main.rs:10:26
   |
10 |         Animal::Spider { iHateSnakes } => dbg!(iHateSnakes),
   |                          ^^^^^^^^^^^ help: convert the identifier to snake case: `i_hate_snakes`
   |
   = note: `#[warn(non_snake_case)]` on by default

Seems like a diagnostics fix to change the help to

help: convert the identifier to snake case: `iHateSnakes: i_hate_snakes`

@joshtriplett
Copy link
Member

We discussed this in the lang team meeting, and we agree with the proposal here. We shouldn't warn twice over the same field name; we should only warn about the definition, and not again when you match it.

Note, in particular, that it seems like a relatively common pattern to disable these lints only in a module written by bindgen or otherwise written to match some external spec/API, but leave them enabled in the rest of your code.

So, 👍 to not linting for field puns on field names that don't match the recommended style. (We should still lint when not punning, if you define a new local name that doesn't match the recommended style.)

@Centril Centril added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Nov 21, 2019
@jumbatm
Copy link
Contributor

jumbatm commented Nov 21, 2019

Hi, I'd like to take this one. Just to make sure I'm understanding this right: We want to warn on identifier used to define the field name in the struct itself, and not on the match binding. Is that correct?

@scottmcm
Copy link
Member

@jumbatm We definitely want to warn on the struct definition. My understanding is that we don't want to warn on the binding when you didn't actually write the binding ("for field puns"). So we don't warn on Foo { iHateSnakes } but would warn on Foo { iHateSnakes: iHateSnakes } (and definitely on Foo { i_hate_snakes: iHateSnakes }).

@jumbatm
Copy link
Contributor

jumbatm commented Nov 22, 2019

That makes a lot of sense. Thanks!

@rustbot claim

@rustbot rustbot self-assigned this Nov 22, 2019
@joshtriplett
Copy link
Member

joshtriplett commented Nov 22, 2019 via email

@jumbatm
Copy link
Contributor

jumbatm commented Nov 23, 2019

@joshtriplett I've put up a PR with the failing test case. How's this look? Are there any other cases I should test?

tmandry added a commit to tmandry/rust that referenced this issue Jan 17, 2020
…in_patterns, r=centril

Don't warn about snake case for field puns.

Closes rust-lang#66362.
tmandry added a commit to tmandry/rust that referenced this issue Jan 18, 2020
…in_patterns, r=centril

Don't warn about snake case for field puns.

Closes rust-lang#66362.
@bors bors closed this as completed in c854aec Jan 18, 2020
@rust-lang rust-lang deleted a comment from orhnk Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. 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.

9 participants