1 use clippy_utils::diagnostics::span_lint;
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;
11 /// it lints if an exported function, method, trait method with default impl,
12 /// or trait method impl is not `#[inline]`.
14 /// ### Why is this bad?
15 /// In general, it is not. Functions can be inlined across
16 /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
17 /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
18 /// might intend for most of the methods in their public API to be able to be inlined across
19 /// crates even when LTO is disabled. For these types of crates, enabling this lint might make
20 /// sense. It allows the crate to require all exported methods to be `#[inline]` by default, and
21 /// then opt out for specific methods where this might not make sense.
25 /// pub fn foo() {} // missing #[inline]
27 /// #[inline] pub fn bar() {} // ok
28 /// #[inline(always)] pub fn baz() {} // ok
32 /// fn def_bar() {} // missing #[inline]
37 /// fn private() {} // ok
40 /// impl Bar for Baz {
41 /// fn bar() {} // ok - Baz is not exported
44 /// pub struct PubBaz;
46 /// fn private() {} // ok
47 /// pub fn not_ptrivate() {} // missing #[inline]
50 /// impl Bar for PubBaz {
51 /// fn bar() {} // missing #[inline]
52 /// fn def_bar() {} // missing #[inline]
55 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
57 "detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
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));
65 MISSING_INLINE_IN_PUBLIC_ITEMS,
67 &format!("missing `#[inline]` for {}", desc),
72 fn is_executable_or_proc_macro(cx: &LateContext<'_>) -> bool {
73 use rustc_session::config::CrateType;
79 .any(|t: &CrateType| matches!(t, CrateType::Executable | CrateType::ProcMacro))
82 declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
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_or_proc_macro(cx) {
90 if !cx.access_levels.is_exported(it.def_id) {
94 hir::ItemKind::Fn(..) => {
95 let desc = "a function";
96 let attrs = cx.tcx.hir().attrs(it.hir_id());
97 check_missing_inline_attrs(cx, attrs, it.span, desc);
99 hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, _bounds, trait_items) => {
100 // note: we need to check if the trait is exported so we can't use
101 // `LateLintPass::check_trait_item` here.
102 for tit in trait_items {
103 let tit_ = cx.tcx.hir().trait_item(tit.id);
105 hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
106 hir::TraitItemKind::Fn(..) => {
107 if tit.defaultness.has_value() {
108 // trait method with default body needs inline in case
109 // an impl is not provided
110 let desc = "a default trait method";
111 let item = cx.tcx.hir().trait_item(tit.id);
112 let attrs = cx.tcx.hir().attrs(item.hir_id());
113 check_missing_inline_attrs(cx, attrs, item.span, desc);
119 hir::ItemKind::Const(..)
120 | hir::ItemKind::Enum(..)
121 | hir::ItemKind::Macro(..)
122 | hir::ItemKind::Mod(..)
123 | hir::ItemKind::Static(..)
124 | hir::ItemKind::Struct(..)
125 | hir::ItemKind::TraitAlias(..)
126 | hir::ItemKind::GlobalAsm(..)
127 | hir::ItemKind::TyAlias(..)
128 | hir::ItemKind::Union(..)
129 | hir::ItemKind::OpaqueTy(..)
130 | hir::ItemKind::ExternCrate(..)
131 | hir::ItemKind::ForeignMod { .. }
132 | hir::ItemKind::Impl { .. }
133 | hir::ItemKind::Use(..) => {},
137 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
138 use rustc_middle::ty::{ImplContainer, TraitContainer};
139 if rustc_middle::lint::in_external_macro(cx.sess(), impl_item.span) || is_executable_or_proc_macro(cx) {
143 // If the item being implemented is not exported, then we don't need #[inline]
144 if !cx.access_levels.is_exported(impl_item.def_id) {
148 let desc = match impl_item.kind {
149 hir::ImplItemKind::Fn(..) => "a method",
150 hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(_) => return,
153 let trait_def_id = match cx.tcx.associated_item(impl_item.def_id).container {
154 TraitContainer(cid) => Some(cid),
155 ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
158 if let Some(trait_def_id) = trait_def_id {
159 if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.def_id) {
160 // If a trait is being implemented for an item, and the
161 // trait is not exported, we don't need #[inline]
166 let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
167 check_missing_inline_attrs(cx, attrs, impl_item.span, desc);