/src/tools/clippy/clippy_lints/src/missing_inline.rs

https://gitlab.com/rust-lang/rust · Rust · 170 lines · 105 code · 13 blank · 52 comment · 17 complexity · 91411d1f3f383d36ac8f86fefd7643d3 MD5 · raw file

  1. use clippy_utils::diagnostics::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. declare_clippy_lint! {
  9. /// ### What it does
  10. /// 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?
  14. /// In general, it is not. Functions can be inlined across
  15. /// crates when that's profitable as long as any form of LTO is used. When LTO is disabled,
  16. /// functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates
  17. /// might intend for most of the methods in their public API to be able to be inlined across
  18. /// crates even when LTO is disabled. For these types of crates, enabling this lint might make
  19. /// sense. It allows the crate to require all exported methods to be `#[inline]` by default, and
  20. /// then opt out for specific methods where this might not make sense.
  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_private() {} // missing #[inline]
  47. /// }
  48. ///
  49. /// impl Bar for PubBaz {
  50. /// fn bar() {} // missing #[inline]
  51. /// fn def_bar() {} // missing #[inline]
  52. /// }
  53. /// ```
  54. #[clippy::version = "pre 1.29.0"]
  55. pub MISSING_INLINE_IN_PUBLIC_ITEMS,
  56. restriction,
  57. "detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
  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.has_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. fn is_executable_or_proc_macro(cx: &LateContext<'_>) -> bool {
  71. use rustc_session::config::CrateType;
  72. cx.tcx
  73. .sess
  74. .crate_types()
  75. .iter()
  76. .any(|t: &CrateType| matches!(t, CrateType::Executable | CrateType::ProcMacro))
  77. }
  78. declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]);
  79. impl<'tcx> LateLintPass<'tcx> for MissingInline {
  80. fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
  81. if rustc_middle::lint::in_external_macro(cx.sess(), it.span) || is_executable_or_proc_macro(cx) {
  82. return;
  83. }
  84. if !cx.access_levels.is_exported(it.def_id) {
  85. return;
  86. }
  87. match it.kind {
  88. hir::ItemKind::Fn(..) => {
  89. let desc = "a function";
  90. let attrs = cx.tcx.hir().attrs(it.hir_id());
  91. check_missing_inline_attrs(cx, attrs, it.span, desc);
  92. },
  93. hir::ItemKind::Trait(ref _is_auto, ref _unsafe, _generics, _bounds, trait_items) => {
  94. // note: we need to check if the trait is exported so we can't use
  95. // `LateLintPass::check_trait_item` here.
  96. for tit in trait_items {
  97. let tit_ = cx.tcx.hir().trait_item(tit.id);
  98. match tit_.kind {
  99. hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
  100. hir::TraitItemKind::Fn(..) => {
  101. if tit.defaultness.has_value() {
  102. // trait method with default body needs inline in case
  103. // an impl is not provided
  104. let desc = "a default trait method";
  105. let item = cx.tcx.hir().trait_item(tit.id);
  106. let attrs = cx.tcx.hir().attrs(item.hir_id());
  107. check_missing_inline_attrs(cx, attrs, item.span, desc);
  108. }
  109. },
  110. }
  111. }
  112. },
  113. hir::ItemKind::Const(..)
  114. | hir::ItemKind::Enum(..)
  115. | hir::ItemKind::Macro(..)
  116. | hir::ItemKind::Mod(..)
  117. | hir::ItemKind::Static(..)
  118. | hir::ItemKind::Struct(..)
  119. | hir::ItemKind::TraitAlias(..)
  120. | hir::ItemKind::GlobalAsm(..)
  121. | hir::ItemKind::TyAlias(..)
  122. | hir::ItemKind::Union(..)
  123. | hir::ItemKind::OpaqueTy(..)
  124. | hir::ItemKind::ExternCrate(..)
  125. | hir::ItemKind::ForeignMod { .. }
  126. | hir::ItemKind::Impl { .. }
  127. | hir::ItemKind::Use(..) => {},
  128. };
  129. }
  130. fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
  131. use rustc_middle::ty::{ImplContainer, TraitContainer};
  132. if rustc_middle::lint::in_external_macro(cx.sess(), impl_item.span) || is_executable_or_proc_macro(cx) {
  133. return;
  134. }
  135. // If the item being implemented is not exported, then we don't need #[inline]
  136. if !cx.access_levels.is_exported(impl_item.def_id) {
  137. return;
  138. }
  139. let desc = match impl_item.kind {
  140. hir::ImplItemKind::Fn(..) => "a method",
  141. hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(_) => return,
  142. };
  143. let trait_def_id = match cx.tcx.associated_item(impl_item.def_id).container {
  144. TraitContainer(cid) => Some(cid),
  145. ImplContainer(cid) => cx.tcx.impl_trait_ref(cid).map(|t| t.def_id),
  146. };
  147. if let Some(trait_def_id) = trait_def_id {
  148. if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.def_id) {
  149. // If a trait is being implemented for an item, and the
  150. // trait is not exported, we don't need #[inline]
  151. return;
  152. }
  153. }
  154. let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
  155. check_missing_inline_attrs(cx, attrs, impl_item.span, desc);
  156. }
  157. }