Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions clippy_lints/src/missing_inline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::diagnostics::{span_lint, span_lint_hir};
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, Attribute, find_attr};
Expand Down Expand Up @@ -64,14 +64,20 @@ declare_clippy_lint! {
"detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
}

fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[Attribute], sp: Span, desc: &'static str) {
fn check_missing_inline_attrs(
cx: &LateContext<'_>,
attrs: &[Attribute],
sp: Span,
desc: &'static str,
hir_id: Option<hir::HirId>,
) {
if !find_attr!(attrs, AttributeKind::Inline(..)) {
span_lint(
cx,
MISSING_INLINE_IN_PUBLIC_ITEMS,
sp,
format!("missing `#[inline]` for {desc}"),
);
let msg = format!("missing `#[inline]` for {desc}");
if let Some(hir_id) = hir_id {
span_lint_hir(cx, MISSING_INLINE_IN_PUBLIC_ITEMS, hir_id, sp, msg);
} else {
span_lint(cx, MISSING_INLINE_IN_PUBLIC_ITEMS, sp, msg);
}
}
}

Expand Down Expand Up @@ -103,17 +109,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {

let desc = "a function";
let attrs = cx.tcx.hir_attrs(it.hir_id());
check_missing_inline_attrs(cx, attrs, it.span, desc);
check_missing_inline_attrs(cx, attrs, it.span, desc, None);
},
hir::ItemKind::Trait(
ref _constness,
ref _is_auto,
ref _unsafe,
_ident,
_generics,
_bounds,
trait_items,
) => {
hir::ItemKind::Trait(.., trait_items) => {
// note: we need to check if the trait is exported so we can't use
// `LateLintPass::check_trait_item` here.
for &tit in trait_items {
Expand All @@ -127,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
let desc = "a default trait method";
let item = cx.tcx.hir_trait_item(tit);
let attrs = cx.tcx.hir_attrs(item.hir_id());
check_missing_inline_attrs(cx, attrs, item.span, desc);
check_missing_inline_attrs(cx, attrs, item.span, desc, Some(tit.hir_id()));
}
},
}
Expand Down Expand Up @@ -182,7 +180,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
}

let attrs = cx.tcx.hir_attrs(impl_item.hir_id());
check_missing_inline_attrs(cx, attrs, impl_item.span, desc);
check_missing_inline_attrs(cx, attrs, impl_item.span, desc, None);
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/missing_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,10 @@ pub mod issue15301 {
println!("Just called a Rust function from Rust!");
}
}

pub mod issue15491 {
pub trait Foo {
#[allow(clippy::missing_inline_in_public_items)]
fn foo(&self) {}
}
}
Loading