Skip to content

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

Open

Description

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions