1 use crate::utils::span_lint;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
6 use syntax::source_map::Span;
8 /// **What it does:** it lints if an exported function, method, trait method with default impl,
9 /// or trait method impl is not `#[inline]`.
11 /// **Why is this bad?** In general, it is not. Functions can be inlined across
12 /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
13 /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
14 /// might intend for most of the methods in their public API to be able to be inlined across
15 /// crates even when LTO is disabled. For these types of crates, enabling this lint might make
16 /// sense. It allows the crate to require all exported methods to be `#[inline]` by default, and
17 /// then opt out for specific methods where this might not make sense.
19 /// **Known problems:** None.
23 /// pub fn foo() {} // missing #[inline]
25 /// #[inline] pub fn bar() {} // ok
26 /// #[inline(always)] pub fn baz() {} // ok
30 /// fn def_bar() {} // missing #[inline]
35 /// fn priv() {} // ok
38 /// impl Bar for Baz {
39 /// fn bar() {} // ok - Baz is not exported
42 /// pub struct PubBaz;
44 /// fn priv() {} // ok
45 /// pub not_ptriv() {} // missing #[inline]
48 /// impl Bar for PubBaz {
49 /// fn bar() {} // missing #[inline]
50 /// fn def_bar() {} // missing #[inline]
53 declare_clippy_lint! {
54 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
56 "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
59 pub struct MissingInline;
61 fn check_missing_inline_attrs(cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
62 let has_inline = attrs.iter().any(|a| a.name() == "inline");
66 MISSING_INLINE_IN_PUBLIC_ITEMS,
68 &format!("missing `#[inline]` for {}", desc),
73 fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
74 use rustc::session::config::CrateType;
76 cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| match t {
77 CrateType::Executable => true,
82 impl LintPass for MissingInline {
83 fn get_lints(&self) -> LintArray {
84 lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
87 fn name(&self) -> &'static str {
92 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
93 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
94 if is_executable(cx) {
98 if !cx.access_levels.is_exported(it.id) {
102 hir::ItemKind::Fn(..) => {
103 let desc = "a function";
104 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
106 hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, ref trait_items) => {
107 // note: we need to check if the trait is exported so we can't use
108 // `LateLintPass::check_trait_item` here.
109 for tit in trait_items {
110 let tit_ = cx.tcx.hir().trait_item(tit.id);
112 hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
113 hir::TraitItemKind::Method(..) => {
114 if tit.defaultness.has_value() {
115 // trait method with default body needs inline in case
116 // an impl is not provided
117 let desc = "a default trait method";
118 let item = cx.tcx.hir().expect_trait_item(tit.id.node_id);
119 check_missing_inline_attrs(cx, &item.attrs, item.span, desc);
125 hir::ItemKind::Const(..)
126 | hir::ItemKind::Enum(..)
127 | hir::ItemKind::Mod(..)
128 | hir::ItemKind::Static(..)
129 | hir::ItemKind::Struct(..)
130 | hir::ItemKind::TraitAlias(..)
131 | hir::ItemKind::GlobalAsm(..)
132 | hir::ItemKind::Ty(..)
133 | hir::ItemKind::Union(..)
134 | hir::ItemKind::Existential(..)
135 | hir::ItemKind::ExternCrate(..)
136 | hir::ItemKind::ForeignMod(..)
137 | hir::ItemKind::Impl(..)
138 | hir::ItemKind::Use(..) => {},
142 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
143 use rustc::ty::{ImplContainer, TraitContainer};
144 if is_executable(cx) {
148 // If the item being implemented is not exported, then we don't need #[inline]
149 if !cx.access_levels.is_exported(impl_item.id) {
153 let desc = match impl_item.node {
154 hir::ImplItemKind::Method(..) => "a method",
155 hir::ImplItemKind::Const(..) | hir::ImplItemKind::Type(_) | hir::ImplItemKind::Existential(_) => return,
158 let def_id = cx.tcx.hir().local_def_id(impl_item.id);
159 let trait_def_id = match cx.tcx.associated_item(def_id).container {
160 TraitContainer(cid) => Some(cid),
161 ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
164 if let Some(trait_def_id) = trait_def_id {
165 if let Some(n) = cx.tcx.hir().as_local_node_id(trait_def_id) {
166 if !cx.access_levels.is_exported(n) {
167 // If a trait is being implemented for an item, and the
168 // trait is not exported, we don't need #[inline]
174 check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);