rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.45k stars 1.54k forks source link

[Roadmap] Lint groups #6626

Open flip1995 opened 3 years ago

flip1995 commented 3 years ago

There are more and more issues about managing lints in Clippy popping up. Lints are hard to implement with a guarantee of no/few false positives (FPs). One way to address this might be to introduce more lint groups to give users the ability to better manage lints, or improve the process of classifying lints, so that disabling lints due to FPs becomes rare. It is important to note, that Clippy lints are less conservative than rustc lints, which won't change in the future.

Steps to completion:

flip1995 commented 3 years ago

Make it possible to dump a statistics file when running Clippy (allowed lints, ...?)

https://github.com/flip1995/rust-clippy/commit/fa539c9d85a8b1ae9e88bc777a495c2cebf554bc is a first attempt at implementing this as a PoC.

Output for Clippy ``` clippy_lints: Global allow attributes: missing_docs_in_private_items: 1 must_use_candidate: 1 Inner allow attributes: enum_glob_use: 3 wildcard_imports: 2 similar_names: 1 float_cmp: 1 Outer allow attributes: too_many_lines: 26 module_name_repetitions: 16 cast_possible_truncation: 14 similar_names: 10 cast_possible_wrap: 4 cast_sign_loss: 4 integer_division: 2 match_same_arms: 2 many_single_char_names: 1 shadow_unrelated: 1 too_many_arguments: 1 unknown_clippy_lints: 1 useless_attribute: 1 wrong_self_convention: 1 needless_pass_by_value: 1 option_if_let_else: 1 clippy_driver: Outer allow attributes: too_many_lines: 1 ```
dhardy commented 2 years ago

Notes on existing groups:

I only looked at lints starting a..=d for the larger groups. Let me know if you want me to go through the rest for re-categorisation purposes.

Notes

The following lint sub-categories occur in multiple groups:

Both correctness and suspicious groups both concern probable mistakes. (Possibly I am missing the distinction, beyond that suspicious lints are less certain?) Several other categories also have "probably wrong" lints. A possible distinction is "probably a bug" and "probably incorrect but harmless" (the latter should probably be categorised elsewhere).

"Style" is an odd mix of lints. Possibly simply some lints here are mis-categorised. There is also significant divide between lints which concern redundant stuff and lints which concern complexity, with other lints suggesting "more readable" alternatives.

"Utility" lints (from multiple groups) do nothing unless specific configuration is added, therefore they should be at least "warn" level by default; these could be placed in a new group.

Special sub-categories:

I tried to take an un-biased view on these categories, though certainly do have opinions (e.g. collapsible_if's suggestions are often less readable). Ideally it would be easy to disable this type of "style" lint while keeping others such as needless_borrow.

Suggestions and questions

Is a simple list of groups enough? Would hierarchical groups or non-exclusive categories (as in Venn diagram) be better?

Is introducing more categories an issue, e.g. because people use config like #[warn(clippy::pedantic)] which would affect fewer lints than previously? Is allowing lints in multiple groups a satisfactory solution?

Some lints are strongly related, e.g. collapsible_if and collapsible_else_if or chars_last_cmp and chars_next_cmp. Ideally it would be easier to en/dis-able these as a group. Motivation for hierarchical groups?

Potentially new categories:

Possibly mis-categorised lints:

llogiq commented 2 years ago

My 2¢ on this:

dhardy commented 2 years ago

Regarding drop, I have in the past used it with (mutable) references to get around borrow-checker restrictions. None of the examples I have now which require an explicit drop trip these lints, however.

The current lint groups focus on the why something is linted. Your proposed groups as I understand them focus on the what instead.

I guess so, though sometimes the why eludes me. E.g. why are bytes_count_to_len and derivable_impls complexity lints while bool_to_int_with_if and chars_last_cmp are style lints? My best guess is that style is one of the oldest groups and a lot of its contents never got re-assigned. Should they be moved to complexity (or even a new group)?

A few of those from above are a little hard to place:

flip1995 commented 2 years ago

My best guess is that style is one of the oldest groups and a lot of its contents never got re-assigned. Should they be moved to complexity (or even a new group)?

Lint groups are just assigned by whatever the author + reviewer think is the best group. We try to find the best group, but sometimes some lints might slip through into the arguably wrong group. If you think a lint should be moved to a different group, it is as easy as changing the group in the lint definition and running cargo dev update_lints.


I don't think we will introduce non-exclusive groups in Clippy. The groups we have need to stay, as they were defined in an RFC. Additional groups that might overlap (with existing groups) will just make configuring lints confusing.

Sub-groups maybe. (I brought this up for pedantic) But now, I don't really see a practical use for this in our warn-by-default lint groups.