-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add missing_inline lint #2895
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
Add missing_inline lint #2895
Conversation
When turned on, the lint warns on all exported functions, methods, trait methods (default impls, impls), that are not `#[inline]`. Closes rust-lang#1503.
cc @oli-obk |
clippy_lints/src/missing_inline.rs
Outdated
// If we're building a test harness, FIXME: is this relevant? | ||
// if cx.sess().opts.test { | ||
// return; | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea whether this is needed for something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not. Just don't turn it on in tests, and most test stuff isn't pub
/// It allows the crate to require all exported methods to be `#[inline]` by default, and then opt | ||
/// out for specific methods where this might not make sense. | ||
/// | ||
/// **Known problems:** None. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even when the example is trivial there should be an example in the documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an example of a lint that does this? I based this lint on missing_docs
, which doesn't provide anything :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added some examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's more than enough examples! Thanks!
You're right the missing_docs
lint is missing an example. Every other lint does have one AFAIK. Even when it is obvious what the lint does: https://rust-lang-nursery.github.io/rust-clippy/current/index.html#assign_ops
clippy_lints/src/missing_inline.rs
Outdated
"detects missing #[inline] attribute for public callables (functions, trait methods, methods...)" | ||
} | ||
|
||
pub struct MissingInline {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just make it a pub struct MissingInline;
, saving you the Default
impl and new
method
clippy_lints/src/missing_inline.rs
Outdated
// If we're building a test harness, FIXME: is this relevant? | ||
// if cx.sess().opts.test { | ||
// return; | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not. Just don't turn it on in tests, and most test stuff isn't pub
clippy_lints/src/missing_inline.rs
Outdated
} | ||
match it.node { | ||
hir::ItemFn(..) => { | ||
// ignore main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just ignore this lint on binary crates? It's only relevant for libs, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, do you have an example of a lint that does that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope ^^ You can probably ask the session what kind of crate it is.
clippy_lints/src/missing_inline.rs
Outdated
let desc = "a function"; | ||
self.check_missing_inline_attrs(cx, &it.attrs, it.span, desc); | ||
}, | ||
hir::ItemTrait(ref _is_auto, ref _unsafe, ref _generics, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment here that we're not using https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/trait.LateLintPass.html#method.check_trait_item because we need to check if the trait is exported
clippy_lints/src/missing_inline.rs
Outdated
use rustc::ty::{TraitContainer, ImplContainer}; | ||
|
||
// If the item being implemented is not exported, then we don't need #[inline] | ||
if !cx.access_levels.is_exported(impl_item.id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this check is doing anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without it, the check fails for:
// do not need inline because Foo is not exported
impl Foo {
fn FooImpl() {} // ok
}
clippy_lints/src/missing_inline.rs
Outdated
match cx.tcx.associated_item(def_id).container { | ||
TraitContainer(cid) => { | ||
let n = cx.tcx.hir.as_local_node_id(cid); | ||
if n.is_some() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace with if let Some(n) = n
to get rid of the unwrap
inside
clippy_lints/src/missing_inline.rs
Outdated
}, | ||
} | ||
|
||
let desc = match impl_item.node { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this up in the function as an early abort (saves unnecessary CPU cycles)
See travis Also (unrelated) we should fix the |
I've addresses all changes (hopefully) except for detecting the type of crate being compiled. I can only get a |
Ah, it's under |
Thanks, i've now fixed that too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, overlooked one thing in the reviews
r=me with the nit fixed
clippy_lints/src/missing_inline.rs
Outdated
pub struct MissingInline; | ||
|
||
impl MissingInline { | ||
fn check_missing_inline_attrs(&self, cx: &LateContext, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be a function instead of a method. It does not use &self
at all.
When turned on, the
missing_inline
lint warns on all exported functions, methods,trait methods (default impls, impls), that are not
#[inline]
.Closes #1503.