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

Opt-out of lint `#[inline]` on generic functions #11187

Open appetrosyan opened 1 year ago

appetrosyan commented 1 year ago

Description

The lint is far from ergonomic because of the rate of false-positives.

Applying this lint is equivalent to applying LTO to just one crate, badly.

I propose making it a little easier to use by opting out of cases where it might be unapplicable as a class of functions.

One shape this could take is splitting the lint up into several.

This is a great oversimplification of the more-art-than-science of determining which functions need to be annotated as inline, but it highlights two cases where I would definitely say "it should be #[inline]" and cases where I'd say "it probably shouldn't".

Having these as separate lints would allow me to do something like deny(missing_inline_for_public_trivial_fn), and warn(missing_inline_for_constructor, missing_inline_for_generic_fn), or apply them more granularly across different modules (e.g. a module where all generics are also inline(always) and a module where items are not.

Version

No response

Additional Labels

No response

emilk commented 6 months ago

I also want a missing_inline_for_public_trivial_fn lint. Currently I have a custom Python script that reminds me to add #[inline] to these cases:

For instance:

impl AsRef<str> for Utf8 {
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

A lot of people don't realize that #[inline] is also important when implementing simple trait functions like these, so that's why I'm calling it out explicitly.