Skip to content

Commit 1c71163

Browse files
committed
stop [mixed_attributes_style] from linting on different attributes
1 parent 8521427 commit 1c71163

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

clippy_lints/src/attrs/mixed_attributes_style.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@ use clippy_utils::diagnostics::span_lint;
33
use rustc_ast::{AttrKind, AttrStyle, Attribute};
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_lint::{LateContext, LintContext};
6-
use rustc_span::Span;
6+
use rustc_span::{Span, Symbol};
77
use std::sync::Arc;
88

99
#[derive(Hash, PartialEq, Eq)]
1010
enum SimpleAttrKind {
1111
Doc,
12-
Normal,
12+
/// A normal attribute, with its name symbols.
13+
Normal(Vec<Symbol>),
1314
}
1415

1516
impl From<&AttrKind> for SimpleAttrKind {
1617
fn from(value: &AttrKind) -> Self {
1718
match value {
18-
AttrKind::Normal(_) => Self::Normal,
19+
AttrKind::Normal(attr) => {
20+
let path_symbols = attr
21+
.item
22+
.path
23+
.segments
24+
.iter()
25+
.map(|seg| seg.ident.name)
26+
.collect::<Vec<_>>();
27+
Self::Normal(path_symbols)
28+
},
1929
AttrKind::DocComment(..) => Self::Doc,
2030
}
2131
}

clippy_lints/src/attrs/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,20 @@ declare_clippy_lint! {
465465

466466
declare_clippy_lint! {
467467
/// ### What it does
468-
/// Checks that an item has only one kind of attributes.
468+
/// Checks for item that has same kind of attributes with mixed styles (inner/outer).
469469
///
470470
/// ### Why is this bad?
471-
/// Having both kinds of attributes makes it more complicated to read code.
471+
/// Having both style of said attributes makes it more complicated to read code.
472+
///
473+
/// ### Known problems
474+
/// This lint currently have false-nagative when mixing same attributes
475+
/// but they have different path symbols, for example:
476+
/// ```ignore
477+
/// #[custom_attribute]
478+
/// pub fn foo() {
479+
/// #![my_crate::custom_attribute]
480+
/// }
481+
/// ```
472482
///
473483
/// ### Example
474484
/// ```no_run

tests/ui/mixed_attributes_style.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
//@aux-build:proc_macro_attr.rs
2+
#![feature(custom_inner_attributes)]
13
#![warn(clippy::mixed_attributes_style)]
24
#![allow(clippy::duplicated_attributes)]
35

6+
use proc_macro_attr::dummy;
7+
48
#[allow(unused)] //~ ERROR: item has both inner and outer attributes
59
fn foo1() {
610
#![allow(unused)]
@@ -53,3 +57,45 @@ mod baz {
5357
mod quz {
5458
#![allow(unused)]
5559
}
60+
61+
// issue #12530, don't lint different attributes entirely
62+
#[cfg(test)]
63+
mod tests {
64+
#![allow(clippy::unreadable_literal)]
65+
}
66+
#[cfg(unix)]
67+
mod another_mod {
68+
#![allow(clippy::question_mark)]
69+
}
70+
/// Nested mod - Good
71+
mod nested_mod {
72+
#[allow(dead_code)] //~ ERROR: item has both inner and outer attributes
73+
mod inner_mod {
74+
#![allow(dead_code)]
75+
}
76+
}
77+
/// Nested mod - Good //~ ERROR: item has both inner and outer attributes
78+
#[allow(unused)]
79+
mod nest_mod_2 {
80+
#![allow(unused)]
81+
82+
#[allow(dead_code)] //~ ERROR: item has both inner and outer attributes
83+
mod inner_mod {
84+
#![allow(dead_code)]
85+
}
86+
}
87+
/// Nested mod - Possible FN
88+
#[cfg(test)]
89+
mod nested_mod_false_nagative {
90+
#![allow(unused)]
91+
92+
#[allow(dead_code)] // This should lint but it does not, removing the `#[cfg(test)]` solves the problem.
93+
mod inner_mod {
94+
#![allow(dead_code)]
95+
}
96+
}
97+
// Different path symbols - Known FN
98+
#[dummy]
99+
fn use_dummy() {
100+
#![proc_macro_attr::dummy]
101+
}
+29-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: item has both inner and outer attributes
2-
--> tests/ui/mixed_attributes_style.rs:4:1
2+
--> tests/ui/mixed_attributes_style.rs:8:1
33
|
44
LL | / #[allow(unused)]
55
LL | | fn foo1() {
@@ -10,7 +10,7 @@ LL | | #![allow(unused)]
1010
= help: to override `-D warnings` add `#[allow(clippy::mixed_attributes_style)]`
1111

1212
error: item has both inner and outer attributes
13-
--> tests/ui/mixed_attributes_style.rs:18:1
13+
--> tests/ui/mixed_attributes_style.rs:22:1
1414
|
1515
LL | / /// linux
1616
LL | |
@@ -19,12 +19,37 @@ LL | | //! windows
1919
| |_______________^
2020

2121
error: item has both inner and outer attributes
22-
--> tests/ui/mixed_attributes_style.rs:33:1
22+
--> tests/ui/mixed_attributes_style.rs:37:1
2323
|
2424
LL | / #[allow(unused)]
2525
LL | | mod bar {
2626
LL | | #![allow(unused)]
2727
| |_____________________^
2828

29-
error: aborting due to 3 previous errors
29+
error: item has both inner and outer attributes
30+
--> tests/ui/mixed_attributes_style.rs:72:5
31+
|
32+
LL | / #[allow(dead_code)]
33+
LL | | mod inner_mod {
34+
LL | | #![allow(dead_code)]
35+
| |____________________________^
36+
37+
error: item has both inner and outer attributes
38+
--> tests/ui/mixed_attributes_style.rs:77:1
39+
|
40+
LL | / /// Nested mod - Good
41+
LL | | #[allow(unused)]
42+
LL | | mod nest_mod_2 {
43+
LL | | #![allow(unused)]
44+
| |_____________________^
45+
46+
error: item has both inner and outer attributes
47+
--> tests/ui/mixed_attributes_style.rs:82:5
48+
|
49+
LL | / #[allow(dead_code)]
50+
LL | | mod inner_mod {
51+
LL | | #![allow(dead_code)]
52+
| |____________________________^
53+
54+
error: aborting due to 6 previous errors
3055

0 commit comments

Comments
 (0)