]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_inline.rs
Merge branch 'macro-use' into HEAD
[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 rustc::{declare_lint, lint_array};
15 use syntax::ast;
16 use syntax::codemap::Span;
17
18 /// **What it does:** it lints if an exported function, method, trait method with default impl,
19 /// or trait method impl is not `#[inline]`.
20 ///
21 /// **Why is this bad?** In general, it is not. Functions can be inlined across
22 /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
23 /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
24 /// might intend for most of the methods in their public API to be able to be inlined across
25 /// crates even when LTO is disabled. For these types of crates, enabling this lint might make sense.
26 /// It allows the crate to require all exported methods to be `#[inline]` by default, and then opt
27 /// out for specific methods where this might not make sense.
28 ///
29 /// **Known problems:** None.
30 ///
31 /// **Example:**
32 /// ```rust
33 /// pub fn foo() {} // missing #[inline]
34 /// fn ok() {} // ok
35 /// #[inline] pub fn bar() {} // ok
36 /// #[inline(always)] pub fn baz() {} // ok
37 ///
38 /// pub trait Bar {
39 ///   fn bar(); // ok
40 ///   fn def_bar() {} // missing #[inline]
41 /// }
42 ///
43 /// struct Baz;
44 /// impl Baz {
45 ///    fn priv() {} // ok
46 /// }
47 ///
48 /// impl Bar for Baz {
49 ///   fn bar() {} // ok - Baz is not exported
50 /// }
51 ///
52 /// pub struct PubBaz;
53 /// impl PubBaz {
54 ///    fn priv() {} // ok
55 ///    pub not_ptriv() {} // missing #[inline]
56 /// }
57 ///
58 /// impl Bar for PubBaz {
59 ///    fn bar() {} // missing #[inline]
60 ///    fn def_bar() {} // missing #[inline]
61 /// }
62 /// ```
63 declare_clippy_lint! {
64     pub MISSING_INLINE_IN_PUBLIC_ITEMS,
65     restriction,
66     "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
67 }
68
69 pub struct MissingInline;
70
71 fn check_missing_inline_attrs(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 fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
86     use rustc::session::config::CrateType;
87
88     cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| {
89         match t {
90             CrateType::CrateTypeExecutable => true,
91             _ => false,
92         }
93     })
94 }
95
96 impl LintPass for MissingInline {
97     fn get_lints(&self) -> LintArray {
98         lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
99     }
100 }
101
102 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
103     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
104         if is_executable(cx) {
105             return;
106         }
107
108         if !cx.access_levels.is_exported(it.id) {
109             return;
110         }
111         match it.node {
112             hir::ItemKind::Fn(..) => {
113                 let desc = "a function";
114                 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
115             },
116             hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics,
117                            ref _bounds, ref trait_items)  => {
118                 // note: we need to check if the trait is exported so we can't use
119                 // `LateLintPass::check_trait_item` here.
120                 for tit in trait_items {
121                     let tit_ = cx.tcx.hir.trait_item(tit.id);
122                     match tit_.node {
123                         hir::TraitItemKind::Const(..) |
124                         hir::TraitItemKind::Type(..) => {},
125                         hir::TraitItemKind::Method(..) => {
126                             if tit.defaultness.has_value() {
127                                 // trait method with default body needs inline in case
128                                 // an impl is not provided
129                                 let desc = "a default trait method";
130                                 let item = cx.tcx.hir.expect_trait_item(tit.id.node_id);
131                                 check_missing_inline_attrs(cx, &item.attrs,
132                                                                 item.span, desc);
133                             }
134                         },
135                     }
136                 }
137             }
138             hir::ItemKind::Const(..) |
139             hir::ItemKind::Enum(..) |
140             hir::ItemKind::Mod(..) |
141             hir::ItemKind::Static(..) |
142             hir::ItemKind::Struct(..) |
143             hir::ItemKind::TraitAlias(..) |
144             hir::ItemKind::GlobalAsm(..) |
145             hir::ItemKind::Ty(..) |
146             hir::ItemKind::Union(..) |
147             hir::ItemKind::Existential(..) |
148             hir::ItemKind::ExternCrate(..) |
149             hir::ItemKind::ForeignMod(..) |
150             hir::ItemKind::Impl(..) |
151             hir::ItemKind::Use(..) => {},
152         };
153     }
154
155     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
156         use rustc::ty::{TraitContainer, ImplContainer};
157         if is_executable(cx) {
158             return;
159         }
160
161         // If the item being implemented is not exported, then we don't need #[inline]
162         if !cx.access_levels.is_exported(impl_item.id) {
163             return;
164         }
165
166         let desc = match impl_item.node {
167             hir::ImplItemKind::Method(..) => "a method",
168             hir::ImplItemKind::Const(..) |
169             hir::ImplItemKind::Type(_) |
170             hir::ImplItemKind::Existential(_) => return,
171         };
172
173         let def_id = cx.tcx.hir.local_def_id(impl_item.id);
174         let trait_def_id = match cx.tcx.associated_item(def_id).container {
175             TraitContainer(cid) => Some(cid),
176             ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
177         };
178
179         if let Some(trait_def_id) = trait_def_id {
180             if let Some(n) = cx.tcx.hir.as_local_node_id(trait_def_id) {
181                 if !cx.access_levels.is_exported(n) {
182                     // If a trait is being implemented for an item, and the
183                     // trait is not exported, we don't need #[inline]
184                     return;
185                 }
186             }
187         }
188
189         check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);
190     }
191 }