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

Inconsistency between - (dash, hyphen, minus) and _ (underscore) in lint names #11332

Open
ivan-aksamentov opened this issue Aug 13, 2023 · 1 comment

Comments

@ivan-aksamentov
Copy link

ivan-aksamentov commented Aug 13, 2023

Description

Sometimes lint names are spelled using dashes, sometimes using underscores. But the two types of syntax are not always interchangeable.

Same problem as with crate names. Similarly unnecessary, confusing, frustrating and dangerous. Similarly not documented anywhere. New to Rust? Lints don't work? Go figure.

Scenario 1

Consider that I have a struct with many bools (let's say for a CLI args with clap crate's derive syntax) and surely Clippy reported it as:

warning: more than 3 bools in a struct
  <cut>
    = help: consider using a state machine or refactoring bools into two-variant enums
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#struct_excessive_bools
    = note: `-W clippy::struct-excessive-bools` implied by `-W clippy::pedantic`

If you have an eye of an eagle, you notice that the URL contains underscored version of the name, but without clippy:: prefix, and the "note" contains dashed version, with prefix.

I have many boolean flags in CLI, so I just want to silence this rule for this struct. I copy the lint name from the note and paste it into the code:

#[allow(clippy::struct-excessive-bools)]

That does not work, because this syntax is incorrect in Rust. The word struct is treated as keyword and the - treated as minus:

error: expected identifier, found keyword `struct`
#[allow(clippy::struct-excessive-bools)]
                ^^^^^^ expected identifier, found keyword

error: expected one of `(`, `,`, `::`, or `=`, found `-`
#[allow(clippy::struct-excessive-bools)]
                      ^ expected one of `(`, `,`, `::`, or `=`

For a newcomer to Rust, no matter how experienced the programmer is, nothing hints at this point that I need to change dashes to underscores. And even if I figured that two syntaxes exist, and I readg docs for Clippy rules sometimes, I still need to manually swap dashes to underscores every time.

Scenario 2

Another problem is that it's impossible to find the rules in the codebase: .cargo/congig.toml and CLI args can contain either syntax, so you cannot just search for one or the other, you need to search both.

Suggestion

I'd like to suggest to consider enforcing underscore syntax and deprecating and dropping the dash syntax. The earlier the better. Clocks are ticking and people are writing more and more pesky dashes, even right now as you read this.

Concrete proposals

Soon:

  • document the inconsistency in highly visible places
  • publicly deprecate usage of hyphens (dashes) in lint names
  • in errors, warnings and suggestions only emit versions with underscores (that's where people are copying them from)
  • same with config variables

In the next major version:

  • remove support for hyphen-based syntax
  • optionally provide a tool for migration

See also

Version

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-unknown-linux-gnu
release: 1.71.0
LLVM version: 16.0.5


clippy 0.1.71 (8ede3aa 2023-07-12)

Additional Labels

No response

@Alexendoo
Copy link
Member

This behaviour is from rustc, if changed there it will also be applied to clippy. Changing https://github.com/rust-lang/rust/blob/1702d0fffc490e304cf93558c621f913ca062e62/compiler/rustc_middle/src/lint.rs#L205-L243 to not replace the underscores would be a good start (see also rust-lang/rust#114030)

Removing support for hyphens on the cli seems unlikely to me, but yeah that would help to nudge towards underscores

There's an upcoming [lints] table feature in cargo that isn't stable yet, you might want to bring it up there also - rust-lang/cargo#12115

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants