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