1 use crate::utils::span_lint;
2 use rustc::declare_lint_pass;
4 use rustc::lint::{self, LateContext, LateLintPass, LintArray, LintContext, LintPass};
5 use rustc_session::declare_tool_lint;
7 use syntax::source_map::Span;
10 /// **What it does:** it lints if an exported function, method, trait method with default impl,
11 /// or trait method impl is not `#[inline]`.
13 /// **Why is this bad?** In general, it is not. Functions can be inlined across
14 /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
15 /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
16 /// might intend for most of the methods in their public API to be able to be inlined across
17 /// crates even when LTO is disabled. For these types of crates, enabling this lint might make
18 /// sense. It allows the crate to require all exported methods to be `#[inline]` by default, and
19 /// then opt out for specific methods where this might not make sense.
21 /// **Known problems:** None.
25 /// pub fn foo() {} // missing #[inline]
27 /// #[inline] pub fn bar() {} // ok
28 /// #[inline(always)] pub fn baz() {} // ok
32 /// fn def_bar() {} // missing #[inline]
37 /// fn private() {} // ok
40 /// impl Bar for Baz {
41 /// fn bar() {} // ok - Baz is not exported
44 /// pub struct PubBaz;
46 /// fn private() {} // ok
47 /// pub fn not_ptrivate() {} // missing #[inline]
50 /// impl Bar for PubBaz {
51 /// fn bar() {} // missing #[inline]
52 /// fn def_bar() {} // missing #[inline]
55 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
57 "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
60 fn check_missing_inline_attrs(cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
61 let has_inline = attrs.iter().any(|a| a.check_name(sym!(inline)));
65 MISSING_INLINE_IN_PUBLIC_ITEMS,
67 &format!("missing `#[inline]` for {}", desc),
72 fn is_executable(cx: &LateContext<'_, '_>) -> bool {
73 use rustc::session::config::CrateType;
75 cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| match t {
76 CrateType::Executable => true,
81 declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
83 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
84 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
85 if lint::in_external_macro(cx.sess(), it.span) || is_executable(cx) {
89 if !cx.access_levels.is_exported(it.hir_id) {
93 hir::ItemKind::Fn(..) => {
94 let desc = "a function";
95 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
97 hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, ref trait_items) => {
98 // note: we need to check if the trait is exported so we can't use
99 // `LateLintPass::check_trait_item` here.
100 for tit in trait_items {
101 let tit_ = cx.tcx.hir().trait_item(tit.id);
103 hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
104 hir::TraitItemKind::Method(..) => {
105 if tit.defaultness.has_value() {
106 // trait method with default body needs inline in case
107 // an impl is not provided
108 let desc = "a default trait method";
109 let item = cx.tcx.hir().expect_trait_item(tit.id.hir_id);
110 check_missing_inline_attrs(cx, &item.attrs, item.span, desc);
116 hir::ItemKind::Const(..)
117 | hir::ItemKind::Enum(..)
118 | hir::ItemKind::Mod(..)
119 | hir::ItemKind::Static(..)
120 | hir::ItemKind::Struct(..)
121 | hir::ItemKind::TraitAlias(..)
122 | hir::ItemKind::GlobalAsm(..)
123 | hir::ItemKind::TyAlias(..)
124 | hir::ItemKind::Union(..)
125 | hir::ItemKind::OpaqueTy(..)
126 | hir::ItemKind::ExternCrate(..)
127 | hir::ItemKind::ForeignMod(..)
128 | hir::ItemKind::Impl(..)
129 | hir::ItemKind::Use(..) => {},
133 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
134 use rustc::ty::{ImplContainer, TraitContainer};
135 if lint::in_external_macro(cx.sess(), impl_item.span) || is_executable(cx) {
139 // If the item being implemented is not exported, then we don't need #[inline]
140 if !cx.access_levels.is_exported(impl_item.hir_id) {
144 let desc = match impl_item.kind {
145 hir::ImplItemKind::Method(..) => "a method",
146 hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(_) | hir::ImplItemKind::OpaqueTy(_) => return,
149 let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
150 let trait_def_id = match cx.tcx.associated_item(def_id).container {
151 TraitContainer(cid) => Some(cid),
152 ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
155 if let Some(trait_def_id) = trait_def_id {
156 if cx.tcx.hir().as_local_node_id(trait_def_id).is_some() && !cx.access_levels.is_exported(impl_item.hir_id)
158 // If a trait is being implemented for an item, and the
159 // trait is not exported, we don't need #[inline]
164 check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);