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.
15 use syntax::codemap::Span;
17 /// **What it does:** it lints if an exported function, method, trait method with default impl,
18 /// or trait method impl is not `#[inline]`.
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.
28 /// **Known problems:** None.
32 /// pub fn foo() {} // missing #[inline]
34 /// #[inline] pub fn bar() {} // ok
35 /// #[inline(always)] pub fn baz() {} // ok
39 /// fn def_bar() {} // missing #[inline]
44 /// fn priv() {} // ok
47 /// impl Bar for Baz {
48 /// fn bar() {} // ok - Baz is not exported
51 /// pub struct PubBaz;
53 /// fn priv() {} // ok
54 /// pub not_ptriv() {} // missing #[inline]
57 /// impl Bar for PubBaz {
58 /// fn bar() {} // missing #[inline]
59 /// fn def_bar() {} // missing #[inline]
62 declare_clippy_lint! {
63 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
65 "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
68 pub struct MissingInline;
70 fn check_missing_inline_attrs(cx: &LateContext,
71 attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
72 let has_inline = attrs
74 .any(|a| a.name() == "inline" );
77 MISSING_INLINE_IN_PUBLIC_ITEMS,
79 &format!("missing `#[inline]` for {}", desc),
84 fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
85 use rustc::session::config::CrateType;
87 cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| {
89 CrateType::CrateTypeExecutable => true,
95 impl LintPass for MissingInline {
96 fn get_lints(&self) -> LintArray {
97 lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
101 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
102 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
103 if is_executable(cx) {
107 if !cx.access_levels.is_exported(it.id) {
111 hir::ItemKind::Fn(..) => {
112 let desc = "a function";
113 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
115 hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics,
116 ref _bounds, ref trait_items) => {
117 // note: we need to check if the trait is exported so we can't use
118 // `LateLintPass::check_trait_item` here.
119 for tit in trait_items {
120 let tit_ = cx.tcx.hir.trait_item(tit.id);
122 hir::TraitItemKind::Const(..) |
123 hir::TraitItemKind::Type(..) => {},
124 hir::TraitItemKind::Method(..) => {
125 if tit.defaultness.has_value() {
126 // trait method with default body needs inline in case
127 // an impl is not provided
128 let desc = "a default trait method";
129 let item = cx.tcx.hir.expect_trait_item(tit.id.node_id);
130 check_missing_inline_attrs(cx, &item.attrs,
137 hir::ItemKind::Const(..) |
138 hir::ItemKind::Enum(..) |
139 hir::ItemKind::Mod(..) |
140 hir::ItemKind::Static(..) |
141 hir::ItemKind::Struct(..) |
142 hir::ItemKind::TraitAlias(..) |
143 hir::ItemKind::GlobalAsm(..) |
144 hir::ItemKind::Ty(..) |
145 hir::ItemKind::Union(..) |
146 hir::ItemKind::Existential(..) |
147 hir::ItemKind::ExternCrate(..) |
148 hir::ItemKind::ForeignMod(..) |
149 hir::ItemKind::Impl(..) |
150 hir::ItemKind::Use(..) => {},
154 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
155 use rustc::ty::{TraitContainer, ImplContainer};
156 if is_executable(cx) {
160 // If the item being implemented is not exported, then we don't need #[inline]
161 if !cx.access_levels.is_exported(impl_item.id) {
165 let desc = match impl_item.node {
166 hir::ImplItemKind::Method(..) => "a method",
167 hir::ImplItemKind::Const(..) |
168 hir::ImplItemKind::Type(_) => return,
171 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
172 let trait_def_id = match cx.tcx.associated_item(def_id).container {
173 TraitContainer(cid) => Some(cid),
174 ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
177 if let Some(trait_def_id) = trait_def_id {
178 if let Some(n) = cx.tcx.hir.as_local_node_id(trait_def_id) {
179 if !cx.access_levels.is_exported(n) {
180 // If a trait is being implemented for an item, and the
181 // trait is not exported, we don't need #[inline]
187 check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);