Skip to content

Lint non-Rusty trait names #15638

@kpreid

Description

@kpreid

What it does

Detects prefixes and suffixes on the names of trait-like entities that are idiomatic in other languages but not in Rust, such as:

  • Prefix “I”, followed by another uppercase letter.

    In this case, the help should give a hint that trait names can often avoid conflict with type names by naming the operation, likely a verb, rather than the kind of thing that implements it, likely a noun.

  • Suffix “able”.

    In this case, removing the suffix to produce a verb should be suggested. (The trait’s methods could sometimes be used to improve the spelling of the verb without needing a complete English dictionary.) If removing the suffix would collide with an existing type name, the suggestion should not be emitted.

Advantage

  • Introduces new users to Rust-specific naming conventions.
  • Will typically result in more concise code without any loss in clarity.

Drawbacks

  • As with any naming lint, will have false positives on bindings crates that are intentionally using existing foreign names. Traits are an uncommon sort of item to map directly, though.
  • Could have false positives in cases like trait Table.
  • A perfect implementation of the suggestion would need an English stemmer and dictionary of irregular cases.
  • While I’m a big fan of naming traits with verbs whenever possible, that’s not actually part of the Rust style guide or API guide.

Example

trait Flippable {
    fn flip(self) -> Self;
}

Could be written as:

trait Flip {
    fn flip(self) -> Self;
}

The lint message could be:

warning: trait names should not have `-able` suffixes
 --> src/lib.rs:1:7
  |
1 | trait Flippable {
  |       ^^^^^^^^^^ help: consider naming the trait using a verb instead of an adjective: `trait Flip`

Comparison with existing lints

It’s not completely clear whether Clippy should have this much opinion about naming, but I think that it is worth considering given existing naming lints such as:

  • enum_variant_names
  • self_named_constructors (another other-language-conventions lint)
  • upper_case_acronyms

I do not know of any lints that overlap with this one or lint nearby cases, except that upper_case_acronyms might trigger in code with “I” prefixes where it would not otherwise.


@rustbot label +L-style

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-styleLint: Belongs in the style lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions