Skip to content

Commit 76d583b

Browse files
committed
Auto merge of rust-lang#138165 - jdonszelmann:inline, r=oli-obk
Rewrite `inline` attribute parser to use new infrastructure and improve diagnostics for all parsed attributes r? `@oli-obk` This PR: - creates a new parser for inline attributes - creates consistent error messages and error codes between attribute parsers; inline and others - as such changes a few error messages for other attributes to be (in my eyes) much more consistent - tests ast-lowering lints introduced by rust-lang#138164 since this is now useful for the first time - Coalesce some useless error codes Builds on top of rust-lang#138164 Closes rust-lang#137950
2 parents 4b60d49 + bc2ed2c commit 76d583b

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
use super::INLINE_ALWAYS;
2-
use super::utils::is_word;
32
use clippy_utils::diagnostics::span_lint;
3+
use rustc_attr_data_structures::{find_attr, AttributeKind, InlineAttr};
44
use rustc_hir::Attribute;
55
use rustc_lint::LateContext;
66
use rustc_span::symbol::Symbol;
7-
use rustc_span::{Span, sym};
7+
use rustc_span::Span;
88

99
pub(super) fn check(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Attribute]) {
1010
if span.from_expansion() {
1111
return;
1212
}
1313

14-
for attr in attrs {
15-
if let Some(values) = attr.meta_item_list() {
16-
if values.len() != 1 || !attr.has_name(sym::inline) {
17-
continue;
18-
}
19-
if is_word(&values[0], sym::always) {
20-
span_lint(
21-
cx,
22-
INLINE_ALWAYS,
23-
attr.span(),
24-
format!("you have declared `#[inline(always)]` on `{name}`. This is usually a bad idea"),
25-
);
26-
}
27-
}
14+
if let Some(span) = find_attr!(attrs, AttributeKind::Inline(InlineAttr::Always, span) => *span) {
15+
span_lint(
16+
cx,
17+
INLINE_ALWAYS,
18+
span,
19+
format!("you have declared `#[inline(always)]` on `{name}`. This is usually a bad idea"),
20+
);
2821
}
2922
}

clippy_lints/src/inline_fn_without_body.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::sugg::DiagExt;
3+
use rustc_attr_data_structures::{find_attr, AttributeKind};
34
use rustc_errors::Applicability;
45
use rustc_hir::{TraitFn, TraitItem, TraitItemKind};
56
use rustc_lint::{LateContext, LateLintPass};
67
use rustc_session::declare_lint_pass;
7-
use rustc_span::sym;
88

99
declare_clippy_lint! {
1010
/// ### What it does
@@ -32,15 +32,19 @@ declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]);
3232
impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
3333
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
3434
if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind
35-
&& let Some(attr) = cx.tcx.hir_attrs(item.hir_id()).iter().find(|a| a.has_name(sym::inline))
35+
&& let Some(attr_span) = find_attr!(cx
36+
.tcx
37+
.hir_attrs(item.hir_id()),
38+
AttributeKind::Inline(_, span) => *span
39+
)
3640
{
3741
span_lint_and_then(
3842
cx,
3943
INLINE_FN_WITHOUT_BODY,
40-
attr.span(),
44+
attr_span,
4145
format!("use of `#[inline]` on trait method `{}` which has no body", item.ident),
4246
|diag| {
43-
diag.suggest_remove_item(cx, attr.span(), "remove", Applicability::MachineApplicable);
47+
diag.suggest_remove_item(cx, attr_span, "remove", Applicability::MachineApplicable);
4448
},
4549
);
4650
}

clippy_lints/src/missing_inline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use clippy_utils::diagnostics::span_lint;
2+
use rustc_attr_data_structures::{find_attr, AttributeKind};
23
use rustc_hir as hir;
34
use rustc_hir::Attribute;
45
use rustc_lint::{LateContext, LateLintPass, LintContext};
56
use rustc_middle::ty::AssocItemContainer;
67
use rustc_session::declare_lint_pass;
7-
use rustc_span::{Span, sym};
8+
use rustc_span::Span;
89

910
declare_clippy_lint! {
1011
/// ### What it does
@@ -64,8 +65,7 @@ declare_clippy_lint! {
6465
}
6566

6667
fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[Attribute], sp: Span, desc: &'static str) {
67-
let has_inline = attrs.iter().any(|a| a.has_name(sym::inline));
68-
if !has_inline {
68+
if !find_attr!(attrs, AttributeKind::Inline(..)) {
6969
span_lint(
7070
cx,
7171
MISSING_INLINE_IN_PUBLIC_ITEMS,

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet;
44
use clippy_utils::ty::{for_each_top_level_late_bound_region, is_copy};
55
use clippy_utils::{is_self, is_self_ty};
6+
use rustc_attr_data_structures::{find_attr, AttributeKind, InlineAttr};
7+
use rustc_data_structures::fx::FxHashSet;
68
use core::ops::ControlFlow;
79
use rustc_abi::ExternAbi;
8-
use rustc_ast::attr;
9-
use rustc_data_structures::fx::FxHashSet;
1010
use rustc_errors::Applicability;
1111
use rustc_hir as hir;
1212
use rustc_hir::intravisit::FnKind;
@@ -270,11 +270,13 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
270270
return;
271271
}
272272
let attrs = cx.tcx.hir_attrs(hir_id);
273+
if find_attr!(attrs, AttributeKind::Inline(InlineAttr::Always, _)) {
274+
return;
275+
}
276+
273277
for a in attrs {
274-
if let Some(meta_items) = a.meta_item_list()
275-
&& (a.has_name(sym::proc_macro_derive)
276-
|| (a.has_name(sym::inline) && attr::list_contains_name(&meta_items, sym::always)))
277-
{
278+
// FIXME(jdonszelmann): make part of the find_attr above
279+
if a.has_name(sym::proc_macro_derive) {
278280
return;
279281
}
280282
}

0 commit comments

Comments
 (0)