2018-08-28 11:13:42 +00:00
|
|
|
use crate::utils::span_lint;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{self, LateContext, LateLintPass, LintContext};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2020-11-05 13:29:48 +00:00
|
|
|
use rustc_span::sym;
|
2018-07-04 08:51:04 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00: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 {
|
2019-08-03 19:24:50 +00:00
|
|
|
/// fn private() {} // ok
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Bar for Baz {
|
|
|
|
/// fn bar() {} // ok - Baz is not exported
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// pub struct PubBaz;
|
|
|
|
/// impl PubBaz {
|
2019-08-03 19:24:50 +00:00
|
|
|
/// fn private() {} // ok
|
|
|
|
/// pub fn not_ptrivate() {} // missing #[inline]
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Bar for PubBaz {
|
|
|
|
/// fn bar() {} // missing #[inline]
|
|
|
|
/// fn def_bar() {} // missing #[inline]
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-07-04 08:51:04 +00:00
|
|
|
pub MISSING_INLINE_IN_PUBLIC_ITEMS,
|
|
|
|
restriction,
|
2020-01-06 06:30:43 +00:00
|
|
|
"detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
|
2018-07-04 08:51:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
|
2020-11-05 13:29:48 +00:00
|
|
|
let has_inline = attrs.iter().any(|a| a.has_name(sym::inline));
|
2018-07-04 23:53:40 +00:00
|
|
|
if !has_inline {
|
2018-08-28 11:13:42 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
2018-07-04 23:53:40 +00:00
|
|
|
MISSING_INLINE_IN_PUBLIC_ITEMS,
|
|
|
|
sp,
|
|
|
|
&format!("missing `#[inline]` for {}", desc),
|
|
|
|
);
|
2018-07-04 08:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_executable(cx: &LateContext<'_>) -> bool {
|
2020-03-18 22:45:02 +00:00
|
|
|
use rustc_session::config::CrateType;
|
2018-07-04 14:39:52 +00:00
|
|
|
|
2020-07-14 12:59:59 +00:00
|
|
|
cx.tcx
|
|
|
|
.sess
|
|
|
|
.crate_types()
|
|
|
|
.iter()
|
|
|
|
.any(|t: &CrateType| matches!(t, CrateType::Executable))
|
2018-07-04 14:39:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
|
2018-07-04 08:51:04 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MissingInline {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
|
2020-03-30 09:02:14 +00:00
|
|
|
if rustc_middle::lint::in_external_macro(cx.sess(), it.span) || is_executable(cx) {
|
2018-07-04 14:39:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-30 16:47:51 +00:00
|
|
|
if !cx.access_levels.is_exported(it.hir_id()) {
|
2018-07-04 08:51:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
match it.kind {
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Fn(..) => {
|
2018-07-04 08:51:04 +00:00
|
|
|
let desc = "a function";
|
2018-07-04 23:53:40 +00:00
|
|
|
check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
|
2018-07-04 08:51:04 +00:00
|
|
|
},
|
2019-12-22 14:56:34 +00:00
|
|
|
hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, trait_items) => {
|
2018-07-04 13:32:55 +00:00
|
|
|
// note: we need to check if the trait is exported so we can't use
|
|
|
|
// `LateLintPass::check_trait_item` here.
|
2018-07-04 08:51:04 +00:00
|
|
|
for tit in trait_items {
|
2018-12-08 00:56:03 +00:00
|
|
|
let tit_ = cx.tcx.hir().trait_item(tit.id);
|
2019-09-27 15:16:06 +00:00
|
|
|
match tit_.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
|
2020-03-12 19:56:55 +00:00
|
|
|
hir::TraitItemKind::Fn(..) => {
|
2018-07-04 08:51:04 +00:00
|
|
|
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";
|
2021-01-30 11:06:04 +00:00
|
|
|
let item = cx.tcx.hir().trait_item(tit.id);
|
2018-11-27 20:14:15 +00:00
|
|
|
check_missing_inline_attrs(cx, &item.attrs, item.span, desc);
|
2018-07-04 08:51:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
|
|
|
hir::ItemKind::Const(..)
|
|
|
|
| hir::ItemKind::Enum(..)
|
|
|
|
| hir::ItemKind::Mod(..)
|
|
|
|
| hir::ItemKind::Static(..)
|
|
|
|
| hir::ItemKind::Struct(..)
|
|
|
|
| hir::ItemKind::TraitAlias(..)
|
|
|
|
| hir::ItemKind::GlobalAsm(..)
|
2019-08-05 05:30:01 +00:00
|
|
|
| hir::ItemKind::TyAlias(..)
|
2018-11-27 20:14:15 +00:00
|
|
|
| hir::ItemKind::Union(..)
|
2019-08-03 06:44:32 +00:00
|
|
|
| hir::ItemKind::OpaqueTy(..)
|
2018-11-27 20:14:15 +00:00
|
|
|
| hir::ItemKind::ExternCrate(..)
|
2020-11-11 21:40:09 +00:00
|
|
|
| hir::ItemKind::ForeignMod { .. }
|
2020-01-18 05:14:36 +00:00
|
|
|
| hir::ItemKind::Impl { .. }
|
2018-11-27 20:14:15 +00:00
|
|
|
| hir::ItemKind::Use(..) => {},
|
2018-07-04 08:51:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty::{ImplContainer, TraitContainer};
|
|
|
|
if rustc_middle::lint::in_external_macro(cx.sess(), impl_item.span) || is_executable(cx) {
|
2018-07-04 14:39:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-04 08:51:04 +00:00
|
|
|
|
|
|
|
// If the item being implemented is not exported, then we don't need #[inline]
|
2019-03-23 22:37:38 +00:00
|
|
|
if !cx.access_levels.is_exported(impl_item.hir_id) {
|
2018-07-04 08:51:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
let desc = match impl_item.kind {
|
2020-03-16 15:00:16 +00:00
|
|
|
hir::ImplItemKind::Fn(..) => "a method",
|
2020-05-10 14:05:06 +00:00
|
|
|
hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(_) => return,
|
2018-07-04 13:32:55 +00:00
|
|
|
};
|
|
|
|
|
2019-07-06 03:52:51 +00:00
|
|
|
let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
|
2018-07-08 06:03:11 +00: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 {
|
2020-04-24 09:57:34 +00:00
|
|
|
if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.hir_id) {
|
2019-03-26 09:55:03 +00:00
|
|
|
// If a trait is being implemented for an item, and the
|
|
|
|
// trait is not exported, we don't need #[inline]
|
|
|
|
return;
|
2018-07-08 06:03:11 +00:00
|
|
|
}
|
2018-07-04 08:51:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 23:53:40 +00:00
|
|
|
check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);
|
2018-07-04 08:51:04 +00:00
|
|
|
}
|
|
|
|
}
|