]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_inline.rs
do not apply lint to executable crate type
[rust.git] / clippy_lints / src / missing_inline.rs
1 //   Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 //   file at the top-level directory of this distribution and at
3 //   http://rust-lang.org/COPYRIGHT.
4 //
5 //   Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 //   http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 //   <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 //   option. This file may not be copied, modified, or distributed
9 //   except according to those terms.
10 //
11
12 use rustc::hir;
13 use rustc::lint::*;
14 use syntax::ast;
15 use syntax::codemap::Span;
16
17 /// **What it does:** it lints if an exported function, method, trait method with default impl,
18 /// or trait method impl is not `#[inline]`.
19 ///
20 /// **Why is this bad?** In general, it is not. Functions can be inlined across
21 /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
22 /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
23 /// might intend for most of the methods in their public API to be able to be inlined across
24 /// crates even when LTO is disabled. For these types of crates, enabling this lint might make sense.
25 /// It allows the crate to require all exported methods to be `#[inline]` by default, and then opt
26 /// out for specific methods where this might not make sense.
27 ///
28 /// **Known problems:** None.
29 ///
30 /// **Example:**
31 /// ```rust
32 /// pub fn foo() {} // missing #[inline]
33 /// fn ok() {} // ok
34 /// #[inline] pub fn bar() {} // ok
35 /// #[inline(always)] pub fn baz() {} // ok
36 ///
37 /// pub trait Bar {
38 ///   fn bar(); // ok
39 ///   fn def_bar() {} // missing #[inline]
40 /// }
41 ///
42 /// struct Baz;
43 /// impl Baz {
44 ///    fn priv() {} // ok
45 /// }
46 ///
47 /// impl Bar for Baz {
48 ///   fn bar() {} // ok - Baz is not exported
49 /// }
50 ///
51 /// pub struct PubBaz;
52 /// impl PubBaz {
53 ///    fn priv() {} // ok
54 ///    pub not_ptriv() {} // missing #[inline]
55 /// }
56 ///
57 /// impl Bar for PubBaz {
58 ///    fn bar() {} // missing #[inline]
59 ///    fn def_bar() {} // missing #[inline]
60 /// }
61 /// ```
62 declare_clippy_lint! {
63     pub MISSING_INLINE_IN_PUBLIC_ITEMS,
64     restriction,
65     "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
66 }
67
68 pub struct MissingInline;
69
70 impl MissingInline {
71     fn check_missing_inline_attrs(&self, cx: &LateContext,
72                                   attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
73         let has_inline = attrs
74             .iter()
75             .any(|a| a.name() == "inline" );
76         if !has_inline {
77             cx.span_lint(
78                 MISSING_INLINE_IN_PUBLIC_ITEMS,
79                 sp,
80                 &format!("missing `#[inline]` for {}", desc),
81             );
82         }
83     }
84 }
85
86 fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
87     use rustc::session::config::CrateType;
88
89     cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| {
90         match t {
91             CrateType::CrateTypeExecutable => true,
92             _ => false,
93         }
94     })
95 }
96
97 impl LintPass for MissingInline {
98     fn get_lints(&self) -> LintArray {
99         lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
100     }
101 }
102
103 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
104     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
105         if is_executable(cx) {
106             return;
107         }
108
109         if !cx.access_levels.is_exported(it.id) {
110             return;
111         }
112         match it.node {
113             hir::ItemFn(..) => {
114                 let desc = "a function";
115                 self.check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
116             },
117             hir::ItemTrait(ref _is_auto, ref _unsafe, ref _generics,
118                            ref _bounds, ref trait_items)  => {
119                 // note: we need to check if the trait is exported so we can't use
120                 // `LateLintPass::check_trait_item` here.
121                 for tit in trait_items {
122                     let tit_ = cx.tcx.hir.trait_item(tit.id);
123                     match tit_.node {
124                         hir::TraitItemKind::Const(..) |
125                         hir::TraitItemKind::Type(..) => {},
126                         hir::TraitItemKind::Method(..) => {
127                             if tit.defaultness.has_value() {
128                                 // trait method with default body needs inline in case
129                                 // an impl is not provided
130                                 let desc = "a default trait method";
131                                 let item = cx.tcx.hir.expect_trait_item(tit.id.node_id);
132                                 self.check_missing_inline_attrs(cx, &item.attrs,
133                                                                 item.span, desc);
134                             }
135                         },
136                     }
137                 }
138             }
139             hir::ItemConst(..) |
140             hir::ItemEnum(..) |
141             hir::ItemMod(..) |
142             hir::ItemStatic(..) |
143             hir::ItemStruct(..) |
144             hir::ItemTraitAlias(..) |
145             hir::ItemGlobalAsm(..) |
146             hir::ItemTy(..) |
147             hir::ItemUnion(..) |
148             hir::ItemExistential(..) |
149             hir::ItemExternCrate(..) |
150             hir::ItemForeignMod(..) |
151             hir::ItemImpl(..) |
152             hir::ItemUse(..) => {},
153         };
154     }
155
156     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
157         use rustc::ty::{TraitContainer, ImplContainer};
158         if is_executable(cx) {
159             return;
160         }
161
162         // If the item being implemented is not exported, then we don't need #[inline]
163         if !cx.access_levels.is_exported(impl_item.id) {
164             return;
165         }
166
167         let desc = match impl_item.node {
168             hir::ImplItemKind::Method(..) => "a method",
169             hir::ImplItemKind::Const(..) |
170             hir::ImplItemKind::Type(_) => return,
171         };
172
173         let def_id = cx.tcx.hir.local_def_id(impl_item.id);
174         match cx.tcx.associated_item(def_id).container {
175             TraitContainer(cid) => {
176                 if let Some(n) = cx.tcx.hir.as_local_node_id(cid) {
177                     if !cx.access_levels.is_exported(n) {
178                         // If a trait is being implemented for an item, and the
179                         // trait is not exported, we don't need #[inline]
180                         return;
181                     }
182                 }
183             },
184             ImplContainer(cid) => {
185                 if cx.tcx.impl_trait_ref(cid).is_some() {
186                     let trait_ref = cx.tcx.impl_trait_ref(cid).unwrap();
187                     if let Some(n) = cx.tcx.hir.as_local_node_id(trait_ref.def_id) {
188                         if !cx.access_levels.is_exported(n) {
189                             // If a trait is being implemented for an item, and the
190                             // trait is not exported, we don't need #[inline]
191                             return;
192                         }
193                     }
194                 }
195             },
196         }
197
198         self.check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);
199     }
200 }