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.
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.
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.
21 use crate::rustc::hir;
22 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
23 use crate::rustc::{declare_tool_lint, lint_array};
24 use crate::syntax::ast;
25 use crate::syntax::source_map::Span;
26 use crate::utils::span_lint;
28 /// **What it does:** it lints if an exported function, method, trait method with default impl,
29 /// or trait method impl is not `#[inline]`.
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.
39 /// **Known problems:** None.
43 /// pub fn foo() {} // missing #[inline]
45 /// #[inline] pub fn bar() {} // ok
46 /// #[inline(always)] pub fn baz() {} // ok
50 /// fn def_bar() {} // missing #[inline]
55 /// fn priv() {} // ok
58 /// impl Bar for Baz {
59 /// fn bar() {} // ok - Baz is not exported
62 /// pub struct PubBaz;
64 /// fn priv() {} // ok
65 /// pub not_ptriv() {} // missing #[inline]
68 /// impl Bar for PubBaz {
69 /// fn bar() {} // missing #[inline]
70 /// fn def_bar() {} // missing #[inline]
73 declare_clippy_lint! {
74 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
76 "detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
79 pub struct MissingInline;
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");
86 MISSING_INLINE_IN_PUBLIC_ITEMS,
88 &format!("missing `#[inline]` for {}", desc),
93 fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
94 use crate::rustc::session::config::CrateType;
96 cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| match t {
97 CrateType::Executable => true,
102 impl LintPass for MissingInline {
103 fn get_lints(&self) -> LintArray {
104 lint_array![MISSING_INLINE_IN_PUBLIC_ITEMS]
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) {
114 if !cx.access_levels.is_exported(it.id) {
118 hir::ItemKind::Fn(..) => {
119 let desc = "a function";
120 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
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);
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);
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(..) => {},
158 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
159 use crate::rustc::ty::{ImplContainer, TraitContainer};
160 if is_executable(cx) {
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) {
169 let desc = match impl_item.node {
170 hir::ImplItemKind::Method(..) => "a method",
171 hir::ImplItemKind::Const(..) | hir::ImplItemKind::Type(_) | hir::ImplItemKind::Existential(_) => return,
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),
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]
190 check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);