Skip to content

Commit 5f80080

Browse files
authored
missing_inline_in_public_items: fix lint emission source HirId (#15501)
use trait item's HirId when emitting lint at `check_item` level Fixes rust-lang/rust-clippy#15491 changelog: [`missing_inline_in_public_items`]: fix trait item lint emission
2 parents 7b611d4 + 4140545 commit 5f80080

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

clippy_lints/src/missing_inline.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::{span_lint, span_lint_hir};
22
use rustc_hir::attrs::AttributeKind;
33
use rustc_hir::def_id::DefId;
44
use rustc_hir::{self as hir, Attribute, find_attr};
@@ -64,14 +64,20 @@ declare_clippy_lint! {
6464
"detects missing `#[inline]` attribute for public callables (functions, trait methods, methods...)"
6565
}
6666

67-
fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[Attribute], sp: Span, desc: &'static str) {
67+
fn check_missing_inline_attrs(
68+
cx: &LateContext<'_>,
69+
attrs: &[Attribute],
70+
sp: Span,
71+
desc: &'static str,
72+
hir_id: Option<hir::HirId>,
73+
) {
6874
if !find_attr!(attrs, AttributeKind::Inline(..)) {
69-
span_lint(
70-
cx,
71-
MISSING_INLINE_IN_PUBLIC_ITEMS,
72-
sp,
73-
format!("missing `#[inline]` for {desc}"),
74-
);
75+
let msg = format!("missing `#[inline]` for {desc}");
76+
if let Some(hir_id) = hir_id {
77+
span_lint_hir(cx, MISSING_INLINE_IN_PUBLIC_ITEMS, hir_id, sp, msg);
78+
} else {
79+
span_lint(cx, MISSING_INLINE_IN_PUBLIC_ITEMS, sp, msg);
80+
}
7581
}
7682
}
7783

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

104110
let desc = "a function";
105111
let attrs = cx.tcx.hir_attrs(it.hir_id());
106-
check_missing_inline_attrs(cx, attrs, it.span, desc);
112+
check_missing_inline_attrs(cx, attrs, it.span, desc, None);
107113
},
108-
hir::ItemKind::Trait(
109-
ref _constness,
110-
ref _is_auto,
111-
ref _unsafe,
112-
_ident,
113-
_generics,
114-
_bounds,
115-
trait_items,
116-
) => {
114+
hir::ItemKind::Trait(.., trait_items) => {
117115
// note: we need to check if the trait is exported so we can't use
118116
// `LateLintPass::check_trait_item` here.
119117
for &tit in trait_items {
@@ -127,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
127125
let desc = "a default trait method";
128126
let item = cx.tcx.hir_trait_item(tit);
129127
let attrs = cx.tcx.hir_attrs(item.hir_id());
130-
check_missing_inline_attrs(cx, attrs, item.span, desc);
128+
check_missing_inline_attrs(cx, attrs, item.span, desc, Some(tit.hir_id()));
131129
}
132130
},
133131
}
@@ -182,7 +180,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
182180
}
183181

184182
let attrs = cx.tcx.hir_attrs(impl_item.hir_id());
185-
check_missing_inline_attrs(cx, attrs, impl_item.span, desc);
183+
check_missing_inline_attrs(cx, attrs, impl_item.span, desc, None);
186184
}
187185
}
188186

tests/ui/missing_inline.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,10 @@ pub mod issue15301 {
9797
println!("Just called a Rust function from Rust!");
9898
}
9999
}
100+
101+
pub mod issue15491 {
102+
pub trait Foo {
103+
#[allow(clippy::missing_inline_in_public_items)]
104+
fn foo(&self) {}
105+
}
106+
}

0 commit comments

Comments
 (0)