2018-08-28 06:13:42 -05:00
|
|
|
use crate::utils::span_lint;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::source_map::Span;
|
2018-07-04 03:51:04 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** it lints if an exported function, method, trait method with default impl,
|
|
|
|
/// or trait method impl is not `#[inline]`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** In general, it is not. Functions can be inlined across
|
|
|
|
/// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
|
|
|
|
/// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
|
|
|
|
/// might intend for most of the methods in their public API to be able to be inlined across
|
|
|
|
/// crates even when LTO is disabled. For these types of crates, enabling this lint might make
|
|
|
|
/// sense. 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.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// pub fn foo() {} // missing #[inline]
|
|
|
|
/// fn ok() {} // ok
|
|
|
|
/// #[inline] pub fn bar() {} // ok
|
|
|
|
/// #[inline(always)] pub fn baz() {} // ok
|
|
|
|
///
|
|
|
|
/// pub trait Bar {
|
|
|
|
/// fn bar(); // ok
|
|
|
|
/// fn def_bar() {} // missing #[inline]
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct Baz;
|
|
|
|
/// impl Baz {
|
|
|
|
/// fn priv() {} // ok
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Bar for Baz {
|
|
|
|
/// fn bar() {} // ok - Baz is not exported
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// pub struct PubBaz;
|
|
|
|
/// impl PubBaz {
|
|
|
|
/// fn priv() {} // ok
|
|
|
|
/// pub not_ptriv() {} // missing #[inline]
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Bar for PubBaz {
|
|
|
|
/// fn bar() {} // missing #[inline]
|
|
|
|
/// fn def_bar() {} // missing #[inline]
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-07-04 03:51:04 -05:00
|
|
|
pub MISSING_INLINE_IN_PUBLIC_ITEMS,
|
|
|
|
restriction,
|
|
|
|
"detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
|
|
|
|
}
|
|
|
|
|
2018-07-04 08:32:55 -05:00
|
|
|
pub struct MissingInline;
|
2018-07-04 03:51:04 -05:00
|
|
|
|
2018-11-27 14:14:15 -06:00
|
|
|
fn check_missing_inline_attrs(cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
|
|
|
|
let has_inline = attrs.iter().any(|a| a.name() == "inline");
|
2018-07-04 18:53:40 -05:00
|
|
|
if !has_inline {
|
2018-08-28 06:13:42 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
2018-07-04 18:53:40 -05:00
|
|
|
MISSING_INLINE_IN_PUBLIC_ITEMS,
|
|
|
|
sp,
|
|
|
|
&format!("missing `#[inline]` for {}", desc),
|
|
|
|
);
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:39:52 -05:00
|
|
|
fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::session::config::CrateType;
|
2018-07-04 09:39:52 -05:00
|
|
|
|
2018-11-27 14:14:15 -06:00
|
|
|
cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| match t {
|
|
|
|
CrateType::Executable => true,
|
|
|
|
_ => false,
|
2018-07-04 09:39:52 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-04 03:51:04 -05:00
|
|
|
impl LintPass for MissingInline {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
|
|
|
|
}
|
2019-01-26 13:40:55 -06:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"MissingInline"
|
|
|
|
}
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
|
2018-07-04 09:39:52 -05:00
|
|
|
if is_executable(cx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-01 06:26:06 -06:00
|
|
|
if !cx.access_levels.is_exported(cx.tcx.hir().hir_to_node_id(it.hir_id)) {
|
2018-07-04 03:51:04 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
match it.node {
|
2018-07-16 08:07:39 -05:00
|
|
|
hir::ItemKind::Fn(..) => {
|
2018-07-04 03:51:04 -05:00
|
|
|
let desc = "a function";
|
2018-07-04 18:53:40 -05:00
|
|
|
check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
|
2018-07-04 03:51:04 -05:00
|
|
|
},
|
2018-11-27 14:14:15 -06:00
|
|
|
hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, ref trait_items) => {
|
2018-07-04 08:32:55 -05:00
|
|
|
// note: we need to check if the trait is exported so we can't use
|
|
|
|
// `LateLintPass::check_trait_item` here.
|
2018-07-04 03:51:04 -05:00
|
|
|
for tit in trait_items {
|
2018-12-07 18:56:03 -06:00
|
|
|
let tit_ = cx.tcx.hir().trait_item(tit.id);
|
2018-07-04 03:51:04 -05:00
|
|
|
match tit_.node {
|
2018-11-27 14:14:15 -06:00
|
|
|
hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
|
2018-07-04 03:51:04 -05:00
|
|
|
hir::TraitItemKind::Method(..) => {
|
|
|
|
if tit.defaultness.has_value() {
|
|
|
|
// trait method with default body needs inline in case
|
|
|
|
// an impl is not provided
|
|
|
|
let desc = "a default trait method";
|
2019-03-07 14:51:05 -06:00
|
|
|
let item = cx.tcx.hir().expect_trait_item(tit.id.hir_id);
|
2018-11-27 14:14:15 -06:00
|
|
|
check_missing_inline_attrs(cx, &item.attrs, item.span, desc);
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 14:14:15 -06:00
|
|
|
},
|
|
|
|
hir::ItemKind::Const(..)
|
|
|
|
| hir::ItemKind::Enum(..)
|
|
|
|
| hir::ItemKind::Mod(..)
|
|
|
|
| hir::ItemKind::Static(..)
|
|
|
|
| hir::ItemKind::Struct(..)
|
|
|
|
| hir::ItemKind::TraitAlias(..)
|
|
|
|
| hir::ItemKind::GlobalAsm(..)
|
|
|
|
| hir::ItemKind::Ty(..)
|
|
|
|
| hir::ItemKind::Union(..)
|
|
|
|
| hir::ItemKind::Existential(..)
|
|
|
|
| hir::ItemKind::ExternCrate(..)
|
|
|
|
| hir::ItemKind::ForeignMod(..)
|
|
|
|
| hir::ItemKind::Impl(..)
|
|
|
|
| hir::ItemKind::Use(..) => {},
|
2018-07-04 03:51:04 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::ty::{ImplContainer, TraitContainer};
|
2018-07-04 09:39:52 -05:00
|
|
|
if is_executable(cx) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-04 03:51:04 -05:00
|
|
|
|
|
|
|
// If the item being implemented is not exported, then we don't need #[inline]
|
2019-02-27 03:39:33 -06:00
|
|
|
let node_id = cx.tcx.hir().hir_to_node_id(impl_item.hir_id);
|
|
|
|
if !cx.access_levels.is_exported(node_id) {
|
2018-07-04 03:51:04 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 08:32:55 -05:00
|
|
|
let desc = match impl_item.node {
|
|
|
|
hir::ImplItemKind::Method(..) => "a method",
|
2018-11-27 14:14:15 -06:00
|
|
|
hir::ImplItemKind::Const(..) | hir::ImplItemKind::Type(_) | hir::ImplItemKind::Existential(_) => return,
|
2018-07-04 08:32:55 -05:00
|
|
|
};
|
|
|
|
|
2019-02-27 03:39:33 -06:00
|
|
|
let def_id = cx.tcx.hir().local_def_id_from_hir_id(impl_item.hir_id);
|
2018-07-08 01:03:11 -05:00
|
|
|
let trait_def_id = match cx.tcx.associated_item(def_id).container {
|
|
|
|
TraitContainer(cid) => Some(cid),
|
|
|
|
ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(trait_def_id) = trait_def_id {
|
2018-12-07 18:56:03 -06:00
|
|
|
if let Some(n) = cx.tcx.hir().as_local_node_id(trait_def_id) {
|
2018-07-08 01:03:11 -05:00
|
|
|
if !cx.access_levels.is_exported(n) {
|
|
|
|
// If a trait is being implemented for an item, and the
|
|
|
|
// trait is not exported, we don't need #[inline]
|
|
|
|
return;
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
2018-07-08 01:03:11 -05:00
|
|
|
}
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:53:40 -05:00
|
|
|
check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);
|
2018-07-04 03:51:04 -05:00
|
|
|
}
|
|
|
|
}
|