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.
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.
14 use rustc::{declare_lint, lint_array};
16 use syntax::codemap::Span;
18 /// **What it does:** it lints if an exported function, method, trait method with default impl,
19 /// or trait method impl is not `#[inline]`.
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.
29 /// **Known problems:** None.
33 /// pub fn foo() {} // missing #[inline]
35 /// #[inline] pub fn bar() {} // ok
36 /// #[inline(always)] pub fn baz() {} // ok
40 /// fn def_bar() {} // missing #[inline]
45 /// fn priv() {} // ok
48 /// impl Bar for Baz {
49 /// fn bar() {} // ok - Baz is not exported
52 /// pub struct PubBaz;
54 /// fn priv() {} // ok
55 /// pub not_ptriv() {} // missing #[inline]
58 /// impl Bar for PubBaz {
59 /// fn bar() {} // missing #[inline]
60 /// fn def_bar() {} // missing #[inline]
63 declare_clippy_lint! {
64 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
66 "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
69 pub struct MissingInline;
71 fn check_missing_inline_attrs(cx: &LateContext,
72 attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
73 let has_inline = attrs
75 .any(|a| a.name() == "inline" );
78 MISSING_INLINE_IN_PUBLIC_ITEMS,
80 &format!("missing `#[inline]` for {}", desc),
85 fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
86 use rustc::session::config::CrateType;
88 cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| {
90 CrateType::CrateTypeExecutable => true,
96 impl LintPass for MissingInline {
97 fn get_lints(&self) -> LintArray {
98 lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
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) {
108 if !cx.access_levels.is_exported(it.id) {
112 hir::ItemKind::Fn(..) => {
113 let desc = "a function";
114 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
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);
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,
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(..) => {},
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) {
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) {
166 let desc = match impl_item.node {
167 hir::ImplItemKind::Method(..) => "a method",
168 hir::ImplItemKind::Const(..) |
169 hir::ImplItemKind::Type(_) => return,
172 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
173 let trait_def_id = match cx.tcx.associated_item(def_id).container {
174 TraitContainer(cid) => Some(cid),
175 ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
178 if let Some(trait_def_id) = trait_def_id {
179 if let Some(n) = cx.tcx.hir.as_local_node_id(trait_def_id) {
180 if !cx.access_levels.is_exported(n) {
181 // If a trait is being implemented for an item, and the
182 // trait is not exported, we don't need #[inline]
188 check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);