1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
11 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
12 // file at the top-level directory of this distribution and at
13 // http://rust-lang.org/COPYRIGHT.
15 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
16 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
17 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
18 // option. This file may not be copied, modified, or distributed
19 // except according to those terms.
22 use crate::rustc::hir;
23 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
24 use crate::rustc::{declare_tool_lint, lint_array};
25 use crate::syntax::ast;
26 use crate::syntax::source_map::Span;
27 use crate::utils::span_lint;
29 /// **What it does:** it lints if an exported function, method, trait method with default impl,
30 /// or trait method impl is not `#[inline]`.
32 /// **Why is this bad?** In general, it is not. Functions can be inlined across
33 /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
34 /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
35 /// might intend for most of the methods in their public API to be able to be inlined across
36 /// crates even when LTO is disabled. For these types of crates, enabling this lint might make sense.
37 /// It allows the crate to require all exported methods to be `#[inline]` by default, and then opt
38 /// out for specific methods where this might not make sense.
40 /// **Known problems:** None.
44 /// pub fn foo() {} // missing #[inline]
46 /// #[inline] pub fn bar() {} // ok
47 /// #[inline(always)] pub fn baz() {} // ok
51 /// fn def_bar() {} // missing #[inline]
56 /// fn priv() {} // ok
59 /// impl Bar for Baz {
60 /// fn bar() {} // ok - Baz is not exported
63 /// pub struct PubBaz;
65 /// fn priv() {} // ok
66 /// pub not_ptriv() {} // missing #[inline]
69 /// impl Bar for PubBaz {
70 /// fn bar() {} // missing #[inline]
71 /// fn def_bar() {} // missing #[inline]
74 declare_clippy_lint! {
75 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
77 "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
80 pub struct MissingInline;
82 fn check_missing_inline_attrs(cx: &LateContext<'_, '_>,
83 attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
84 let has_inline = attrs
86 .any(|a| a.name() == "inline" );
90 MISSING_INLINE_IN_PUBLIC_ITEMS,
92 &format!("missing `#[inline]` for {}", desc),
97 fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
98 use crate::rustc::session::config::CrateType;
100 cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| {
102 CrateType::Executable => true,
108 impl LintPass for MissingInline {
109 fn get_lints(&self) -> LintArray {
110 lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
114 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
115 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
116 if is_executable(cx) {
120 if !cx.access_levels.is_exported(it.id) {
124 hir::ItemKind::Fn(..) => {
125 let desc = "a function";
126 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
128 hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics,
129 ref _bounds, ref trait_items) => {
130 // note: we need to check if the trait is exported so we can't use
131 // `LateLintPass::check_trait_item` here.
132 for tit in trait_items {
133 let tit_ = cx.tcx.hir.trait_item(tit.id);
135 hir::TraitItemKind::Const(..) |
136 hir::TraitItemKind::Type(..) => {},
137 hir::TraitItemKind::Method(..) => {
138 if tit.defaultness.has_value() {
139 // trait method with default body needs inline in case
140 // an impl is not provided
141 let desc = "a default trait method";
142 let item = cx.tcx.hir.expect_trait_item(tit.id.node_id);
143 check_missing_inline_attrs(cx, &item.attrs,
150 hir::ItemKind::Const(..) |
151 hir::ItemKind::Enum(..) |
152 hir::ItemKind::Mod(..) |
153 hir::ItemKind::Static(..) |
154 hir::ItemKind::Struct(..) |
155 hir::ItemKind::TraitAlias(..) |
156 hir::ItemKind::GlobalAsm(..) |
157 hir::ItemKind::Ty(..) |
158 hir::ItemKind::Union(..) |
159 hir::ItemKind::Existential(..) |
160 hir::ItemKind::ExternCrate(..) |
161 hir::ItemKind::ForeignMod(..) |
162 hir::ItemKind::Impl(..) |
163 hir::ItemKind::Use(..) => {},
167 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
168 use crate::rustc::ty::{TraitContainer, ImplContainer};
169 if is_executable(cx) {
173 // If the item being implemented is not exported, then we don't need #[inline]
174 if !cx.access_levels.is_exported(impl_item.id) {
178 let desc = match impl_item.node {
179 hir::ImplItemKind::Method(..) => "a method",
180 hir::ImplItemKind::Const(..) |
181 hir::ImplItemKind::Type(_) |
182 hir::ImplItemKind::Existential(_) => return,
185 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
186 let trait_def_id = match cx.tcx.associated_item(def_id).container {
187 TraitContainer(cid) => Some(cid),
188 ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
191 if let Some(trait_def_id) = trait_def_id {
192 if let Some(n) = cx.tcx.hir.as_local_node_id(trait_def_id) {
193 if !cx.access_levels.is_exported(n) {
194 // If a trait is being implemented for an item, and the
195 // trait is not exported, we don't need #[inline]
201 check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);