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