1 use crate::utils::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;
9 /// **What it does:** it lints if an exported function, method, trait method with default impl,
10 /// or trait method impl is not `#[inline]`.
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.
20 /// **Known problems:** None.
24 /// pub fn foo() {} // missing #[inline]
26 /// #[inline] pub fn bar() {} // ok
27 /// #[inline(always)] pub fn baz() {} // ok
31 /// fn def_bar() {} // missing #[inline]
36 /// fn private() {} // ok
39 /// impl Bar for Baz {
40 /// fn bar() {} // ok - Baz is not exported
43 /// pub struct PubBaz;
45 /// fn private() {} // ok
46 /// pub fn not_ptrivate() {} // missing #[inline]
49 /// impl Bar for PubBaz {
50 /// fn bar() {} // missing #[inline]
51 /// fn def_bar() {} // missing #[inline]
54 pub MISSING_INLINE_IN_PUBLIC_ITEMS,
56 "detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
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)));
64 MISSING_INLINE_IN_PUBLIC_ITEMS,
66 &format!("missing `#[inline]` for {}", desc),
71 fn is_executable(cx: &LateContext<'_, '_>) -> bool {
72 use rustc_session::config::CrateType;
74 cx.tcx.sess.crate_types().iter().any(|t: &CrateType| match t {
75 CrateType::Executable => true,
80 declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
82 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
83 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item<'_>) {
84 if rustc_middle::lint::in_external_macro(cx.sess(), it.span) || is_executable(cx) {
88 if !cx.access_levels.is_exported(it.hir_id) {
92 hir::ItemKind::Fn(..) => {
93 let desc = "a function";
94 check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
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);
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);
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(..) => {},
132 fn check_impl_item(&mut self, cx: &LateContext<'a, '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) {
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) {
143 let desc = match impl_item.kind {
144 hir::ImplItemKind::Fn(..) => "a method",
145 hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(_) | hir::ImplItemKind::OpaqueTy(_) => return,
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),
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]
162 check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);