Skip to content

Commit 0086b6a

Browse files
committed
don't lint allow_attributes on attributes from proc macros
1 parent 9524cff commit 0086b6a

9 files changed

+131
-28
lines changed

clippy_lints/src/allow_attributes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ast::AttrStyle;
2-
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro};
33
use rustc_ast as ast;
44
use rustc_errors::Applicability;
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -57,6 +57,7 @@ impl LateLintPass<'_> for AllowAttribute {
5757
if let AttrStyle::Outer = attr.style;
5858
if let Some(ident) = attr.ident();
5959
if ident.name == rustc_span::symbol::sym::allow;
60+
if !is_from_proc_macro(cx, &(attr, cx));
6061
then {
6162
span_lint_and_sugg(
6263
cx,

clippy_lints/src/attrs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! checks for attributes
22
3-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
43
use clippy_utils::macros::{is_panic, macro_backtrace};
54
use clippy_utils::msrvs::{self, Msrv};
65
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
6+
use clippy_utils::{
7+
diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then},
8+
is_from_proc_macro,
9+
};
710
use if_chain::if_chain;
811
use rustc_ast::{AttrKind, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem};
912
use rustc_errors::Applicability;
@@ -540,7 +543,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, name: Symbol, items: &[NestedMe
540543
}
541544
}
542545

543-
fn check_lint_reason(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem], attr: &'_ Attribute) {
546+
fn check_lint_reason(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem], attr: &Attribute) {
544547
// Check for the feature
545548
if !cx.tcx.features().lint_reasons {
546549
return;
@@ -555,7 +558,7 @@ fn check_lint_reason(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem
555558
}
556559

557560
// Check if the attribute is in an external macro and therefore out of the developer's control
558-
if in_external_macro(cx.sess(), attr.span) {
561+
if in_external_macro(cx.sess(), attr.span) || is_from_proc_macro(cx, &(attr, cx)) {
559562
return;
560563
}
561564

clippy_utils/src/check_proc_macro.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
//! code was written, and check if the span contains that text. Note this will only work correctly
1313
//! if the span is not from a `macro_rules` based macro.
1414
15-
use rustc_ast::ast::{IntTy, LitIntType, LitKind, StrStyle, UintTy};
15+
use rustc_ast::{
16+
ast::{AttrKind, Attribute, IntTy, LitIntType, LitKind, StrStyle, UintTy},
17+
token::CommentKind,
18+
AttrStyle,
19+
};
1620
use rustc_hir::{
1721
intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId,
1822
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, Node, QPath, TraitItem,
@@ -271,6 +275,32 @@ fn fn_kind_pat(tcx: TyCtxt<'_>, kind: &FnKind<'_>, body: &Body<'_>, hir_id: HirI
271275
(start_pat, end_pat)
272276
}
273277

278+
fn attr_search_pat(attr: &Attribute) -> (Pat, Pat) {
279+
match attr.kind {
280+
AttrKind::Normal(..) => {
281+
if matches!(attr.style, AttrStyle::Outer) {
282+
(Pat::Str("#["), Pat::Str("]"))
283+
} else {
284+
(Pat::Str("#!["), Pat::Str("]"))
285+
}
286+
},
287+
AttrKind::DocComment(_kind @ CommentKind::Line, ..) => {
288+
if matches!(attr.style, AttrStyle::Outer) {
289+
(Pat::Str("///"), Pat::Str(""))
290+
} else {
291+
(Pat::Str("//!"), Pat::Str(""))
292+
}
293+
},
294+
AttrKind::DocComment(_kind @ CommentKind::Block, ..) => {
295+
if matches!(attr.style, AttrStyle::Outer) {
296+
(Pat::Str("/**"), Pat::Str("*/"))
297+
} else {
298+
(Pat::Str("/*!"), Pat::Str("*/"))
299+
}
300+
},
301+
}
302+
}
303+
274304
pub trait WithSearchPat {
275305
type Context: LintContext;
276306
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
@@ -310,6 +340,18 @@ impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
310340
}
311341
}
312342

343+
impl<'cx> WithSearchPat for (&Attribute, &LateContext<'cx>) {
344+
type Context = LateContext<'cx>;
345+
346+
fn search_pat(&self, _cx: &Self::Context) -> (Pat, Pat) {
347+
attr_search_pat(&self.0)
348+
}
349+
350+
fn span(&self) -> Span {
351+
self.0.span
352+
}
353+
}
354+
313355
/// Checks if the item likely came from a proc-macro.
314356
///
315357
/// This should be called after `in_external_macro` and the initial pattern matching of the ast as

tests/ui/allow_attributes.fixed

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//@run-rustfix
2+
//@aux-build:proc_macros.rs
23
#![allow(unused)]
34
#![warn(clippy::allow_attributes)]
45
#![feature(lint_reasons)]
6+
#![no_main]
57

6-
fn main() {}
8+
extern crate proc_macros;
9+
use proc_macros::{external, with_span};
710

811
// Using clippy::needless_borrow just as a placeholder, it isn't relevant.
912

@@ -17,9 +20,24 @@ struct T3;
1720
#[warn(clippy::needless_borrow)] // Should not lint
1821
struct T4;
1922
// `panic = "unwind"` should always be true
20-
#[cfg_attr(panic = "unwind", expect(dead_code))]
23+
#[cfg_attr(panic = "unwind", allow(dead_code))]
2124
struct CfgT;
2225

26+
fn ignore_external() {
27+
external! {
28+
#[allow(clippy::needless_borrow)]
29+
fn a() {}
30+
}
31+
}
32+
33+
fn ignore_proc_macro() {
34+
with_span! {
35+
span
36+
#[allow(clippy::needless_borrow)] // Should not lint
37+
fn a() {}
38+
}
39+
}
40+
2341
fn ignore_inner_attr() {
2442
#![allow(unused)] // Should not lint
2543
}

tests/ui/allow_attributes.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//@run-rustfix
2+
//@aux-build:proc_macros.rs
23
#![allow(unused)]
34
#![warn(clippy::allow_attributes)]
45
#![feature(lint_reasons)]
6+
#![no_main]
57

6-
fn main() {}
8+
extern crate proc_macros;
9+
use proc_macros::{external, with_span};
710

811
// Using clippy::needless_borrow just as a placeholder, it isn't relevant.
912

@@ -20,6 +23,21 @@ struct T4;
2023
#[cfg_attr(panic = "unwind", allow(dead_code))]
2124
struct CfgT;
2225

26+
fn ignore_external() {
27+
external! {
28+
#[allow(clippy::needless_borrow)] // Should not lint
29+
fn a() {}
30+
}
31+
}
32+
33+
fn ignore_proc_macro() {
34+
with_span! {
35+
span
36+
#[allow(clippy::needless_borrow)] // Should not lint
37+
fn a() {}
38+
}
39+
}
40+
2341
fn ignore_inner_attr() {
2442
#![allow(unused)] // Should not lint
2543
}

tests/ui/allow_attributes.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
error: #[allow] attribute found
2-
--> $DIR/allow_attributes.rs:11:3
2+
--> $DIR/allow_attributes.rs:14:3
33
|
44
LL | #[allow(dead_code)]
55
| ^^^^^ help: replace it with: `expect`
66
|
77
= note: `-D clippy::allow-attributes` implied by `-D warnings`
88

9-
error: #[allow] attribute found
10-
--> $DIR/allow_attributes.rs:20:30
11-
|
12-
LL | #[cfg_attr(panic = "unwind", allow(dead_code))]
13-
| ^^^^^ help: replace it with: `expect`
14-
15-
error: aborting due to 2 previous errors
9+
error: aborting due to previous error
1610

tests/ui/allow_attributes_false_positive.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1+
//@aux-build:proc_macros.rs
12
#![feature(lint_reasons)]
23
#![deny(clippy::allow_attributes_without_reason)]
4+
#![allow(unfulfilled_lint_expectations)]
5+
6+
extern crate proc_macros;
7+
use proc_macros::{external, with_span};
38

49
// These should trigger the lint
510
#[allow(dead_code)]
611
#[allow(dead_code, deprecated)]
12+
#[expect(dead_code)]
713
// These should be fine
814
#[allow(dead_code, reason = "This should be allowed")]
915
#[warn(dyn_drop, reason = "Warnings can also have reasons")]
1016
#[warn(deref_nullptr)]
1117
#[deny(deref_nullptr)]
1218
#[forbid(deref_nullptr)]
1319

14-
fn main() {}
20+
fn main() {
21+
external! {
22+
#[allow(dead_code)]
23+
fn a() {}
24+
}
25+
with_span! {
26+
span
27+
#[allow(dead_code)]
28+
fn b() {}
29+
}
30+
}
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
11
error: `allow` attribute without specifying a reason
2-
--> $DIR/allow_attributes_without_reason.rs:5:1
2+
--> $DIR/allow_attributes_without_reason.rs:4:1
33
|
4-
LL | #[allow(dead_code)]
5-
| ^^^^^^^^^^^^^^^^^^^
4+
LL | #![allow(unfulfilled_lint_expectations)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: try adding a reason at the end with `, reason = ".."`
88
note: the lint level is defined here
9-
--> $DIR/allow_attributes_without_reason.rs:2:9
9+
--> $DIR/allow_attributes_without_reason.rs:3:9
1010
|
1111
LL | #![deny(clippy::allow_attributes_without_reason)]
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error: `allow` attribute without specifying a reason
15-
--> $DIR/allow_attributes_without_reason.rs:6:1
15+
--> $DIR/allow_attributes_without_reason.rs:10:1
16+
|
17+
LL | #[allow(dead_code)]
18+
| ^^^^^^^^^^^^^^^^^^^
19+
|
20+
= help: try adding a reason at the end with `, reason = ".."`
21+
22+
error: `allow` attribute without specifying a reason
23+
--> $DIR/allow_attributes_without_reason.rs:11:1
1624
|
1725
LL | #[allow(dead_code, deprecated)]
1826
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1927
|
2028
= help: try adding a reason at the end with `, reason = ".."`
2129

22-
error: aborting due to 2 previous errors
30+
error: `expect` attribute without specifying a reason
31+
--> $DIR/allow_attributes_without_reason.rs:12:1
32+
|
33+
LL | #[expect(dead_code)]
34+
| ^^^^^^^^^^^^^^^^^^^^
35+
|
36+
= help: try adding a reason at the end with `, reason = ".."`
37+
38+
error: aborting due to 4 previous errors
2339

0 commit comments

Comments
 (0)