]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_inline.rs
Auto merge of #6278 - ThibsG:DerefAddrOf, r=llogiq
[rust.git] / clippy_lints / src / missing_inline.rs
1 use crate::utils::span_lint;
2 use rustc_ast::ast;
3 use rustc_hir as hir;
4 use rustc_lint::{self, LateContext, LateLintPass, LintContext};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::Span;
7 use rustc_span::sym;
8
9 declare_clippy_lint! {
10     /// **What it does:** it lints if an exported function, method, trait method with default impl,
11     /// or trait method impl is not `#[inline]`.
12     ///
13     /// **Why is this bad?** In general, it is not. Functions can be inlined across
14     /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
15     /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
16     /// might intend for most of the methods in their public API to be able to be inlined across
17     /// crates even when LTO is disabled. For these types of crates, enabling this lint might make
18     /// sense. It allows the crate to require all exported methods to be `#[inline]` by default, and
19     /// then opt out for specific methods where this might not make sense.
20     ///
21     /// **Known problems:** None.
22     ///
23     /// **Example:**
24     /// ```rust
25     /// pub fn foo() {} // missing #[inline]
26     /// fn ok() {} // ok
27     /// #[inline] pub fn bar() {} // ok
28     /// #[inline(always)] pub fn baz() {} // ok
29     ///
30     /// pub trait Bar {
31     ///   fn bar(); // ok
32     ///   fn def_bar() {} // missing #[inline]
33     /// }
34     ///
35     /// struct Baz;
36     /// impl Baz {
37     ///    fn private() {} // ok
38     /// }
39     ///
40     /// impl Bar for Baz {
41     ///   fn bar() {} // ok - Baz is not exported
42     /// }
43     ///
44     /// pub struct PubBaz;
45     /// impl PubBaz {
46     ///    fn private() {} // ok
47     ///    pub fn not_ptrivate() {} // missing #[inline]
48     /// }
49     ///
50     /// impl Bar for PubBaz {
51     ///    fn bar() {} // missing #[inline]
52     ///    fn def_bar() {} // missing #[inline]
53     /// }
54     /// ```
55     pub MISSING_INLINE_IN_PUBLIC_ITEMS,
56     restriction,
57     "detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
58 }
59
60 fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
61     let has_inline = attrs.iter().any(|a| a.has_name(sym::inline));
62     if !has_inline {
63         span_lint(
64             cx,
65             MISSING_INLINE_IN_PUBLIC_ITEMS,
66             sp,
67             &format!("missing `#[inline]` for {}", desc),
68         );
69     }
70 }
71
72 fn is_executable(cx: &LateContext<'_>) -> bool {
73     use rustc_session::config::CrateType;
74
75     cx.tcx
76         .sess
77         .crate_types()
78         .iter()
79         .any(|t: &CrateType| matches!(t, CrateType::Executable))
80 }
81
82 declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
83
84 impl<'tcx> LateLintPass<'tcx> for MissingInline {
85     fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
86         if rustc_middle::lint::in_external_macro(cx.sess(), it.span) || is_executable(cx) {
87             return;
88         }
89
90         if !cx.access_levels.is_exported(it.hir_id) {
91             return;
92         }
93         match it.kind {
94             hir::ItemKind::Fn(..) => {
95                 let desc = "a function";
96                 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
97             },
98             hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, trait_items) => {
99                 // note: we need to check if the trait is exported so we can't use
100                 // `LateLintPass::check_trait_item` here.
101                 for tit in trait_items {
102                     let tit_ = cx.tcx.hir().trait_item(tit.id);
103                     match tit_.kind {
104                         hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
105                         hir::TraitItemKind::Fn(..) => {
106                             if tit.defaultness.has_value() {
107                                 // trait method with default body needs inline in case
108                                 // an impl is not provided
109                                 let desc = "a default trait method";
110                                 let item = cx.tcx.hir().expect_trait_item(tit.id.hir_id);
111                                 check_missing_inline_attrs(cx, &item.attrs, item.span, desc);
112                             }
113                         },
114                     }
115                 }
116             },
117             hir::ItemKind::Const(..)
118             | hir::ItemKind::Enum(..)
119             | hir::ItemKind::Mod(..)
120             | hir::ItemKind::Static(..)
121             | hir::ItemKind::Struct(..)
122             | hir::ItemKind::TraitAlias(..)
123             | hir::ItemKind::GlobalAsm(..)
124             | hir::ItemKind::TyAlias(..)
125             | hir::ItemKind::Union(..)
126             | hir::ItemKind::OpaqueTy(..)
127             | hir::ItemKind::ExternCrate(..)
128             | hir::ItemKind::ForeignMod(..)
129             | hir::ItemKind::Impl { .. }
130             | hir::ItemKind::Use(..) => {},
131         };
132     }
133
134     fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
135         use rustc_middle::ty::{ImplContainer, TraitContainer};
136         if rustc_middle::lint::in_external_macro(cx.sess(), impl_item.span) || is_executable(cx) {
137             return;
138         }
139
140         // If the item being implemented is not exported, then we don't need #[inline]
141         if !cx.access_levels.is_exported(impl_item.hir_id) {
142             return;
143         }
144
145         let desc = match impl_item.kind {
146             hir::ImplItemKind::Fn(..) => "a method",
147             hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(_) => return,
148         };
149
150         let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
151         let trait_def_id = match cx.tcx.associated_item(def_id).container {
152             TraitContainer(cid) => Some(cid),
153             ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
154         };
155
156         if let Some(trait_def_id) = trait_def_id {
157             if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.hir_id) {
158                 // If a trait is being implemented for an item, and the
159                 // trait is not exported, we don't need #[inline]
160                 return;
161             }
162         }
163
164         check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);
165     }
166 }