Skip to content

Commit 62fd1d5

Browse files
committed
Auto merge of rust-lang#12646 - GuillaumeGomez:regression-test-12537, r=blyxyas
Turn `duplicated_attributes` into a late lint Fixes rust-lang#12537. changelog: Turn `duplicated_attributes` into a late lint
2 parents ccc93f9 + f7d49a3 commit 62fd1d5

File tree

8 files changed

+77
-39
lines changed

8 files changed

+77
-39
lines changed

clippy_lints/src/attrs/duplicated_attributes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use super::DUPLICATED_ATTRIBUTES;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use rustc_ast::{Attribute, MetaItem};
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_lint::EarlyContext;
5+
use rustc_lint::LateContext;
66
use rustc_span::{sym, Span};
77
use std::collections::hash_map::Entry;
88

99
fn emit_if_duplicated(
10-
cx: &EarlyContext<'_>,
10+
cx: &LateContext<'_>,
1111
attr: &MetaItem,
1212
attr_paths: &mut FxHashMap<String, Span>,
1313
complete_path: String,
@@ -26,7 +26,7 @@ fn emit_if_duplicated(
2626
}
2727

2828
fn check_duplicated_attr(
29-
cx: &EarlyContext<'_>,
29+
cx: &LateContext<'_>,
3030
attr: &MetaItem,
3131
attr_paths: &mut FxHashMap<String, Span>,
3232
parent: &mut Vec<String>,
@@ -64,7 +64,7 @@ fn check_duplicated_attr(
6464
}
6565
}
6666

67-
pub fn check(cx: &EarlyContext<'_>, attrs: &[Attribute]) {
67+
pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
6868
let mut attr_paths = FxHashMap::default();
6969

7070
for attr in attrs {

clippy_lints/src/attrs/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod useless_attribute;
1717
mod utils;
1818

1919
use clippy_config::msrvs::Msrv;
20-
use rustc_ast::{Attribute, Crate, MetaItemKind, NestedMetaItem};
20+
use rustc_ast::{Attribute, MetaItemKind, NestedMetaItem};
2121
use rustc_hir::{ImplItem, Item, ItemKind, TraitItem};
2222
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
2323
use rustc_session::{declare_lint_pass, impl_lint_pass};
@@ -534,11 +534,13 @@ declare_lint_pass!(Attributes => [
534534
BLANKET_CLIPPY_RESTRICTION_LINTS,
535535
SHOULD_PANIC_WITHOUT_EXPECT,
536536
MIXED_ATTRIBUTES_STYLE,
537+
DUPLICATED_ATTRIBUTES,
537538
]);
538539

539540
impl<'tcx> LateLintPass<'tcx> for Attributes {
540541
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
541542
blanket_clippy_restriction_lints::check_command_line(cx);
543+
duplicated_attributes::check(cx, cx.tcx.hir().krate_attrs());
542544
}
543545

544546
fn check_attribute(&mut self, cx: &LateContext<'tcx>, attr: &'tcx Attribute) {
@@ -578,6 +580,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
578580
_ => {},
579581
}
580582
mixed_attributes_style::check(cx, item.span, attrs);
583+
duplicated_attributes::check(cx, attrs);
581584
}
582585

583586
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
@@ -606,17 +609,11 @@ impl_lint_pass!(EarlyAttributes => [
606609
MAYBE_MISUSED_CFG,
607610
DEPRECATED_CLIPPY_CFG_ATTR,
608611
UNNECESSARY_CLIPPY_CFG,
609-
DUPLICATED_ATTRIBUTES,
610612
]);
611613

612614
impl EarlyLintPass for EarlyAttributes {
613-
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
614-
duplicated_attributes::check(cx, &krate.attrs);
615-
}
616-
617615
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
618616
empty_line_after::check(cx, item);
619-
duplicated_attributes::check(cx, &item.attrs);
620617
}
621618

622619
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {

tests/ui/auxiliary/proc_macro_attr.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,17 @@ pub fn with_empty_docs(_attr: TokenStream, input: TokenStream) -> TokenStream {
176176
}
177177
.into()
178178
}
179+
180+
#[proc_macro_attribute]
181+
pub fn duplicated_attr(_attr: TokenStream, input: TokenStream) -> TokenStream {
182+
let item = parse_macro_input!(input as syn::Item);
183+
let attrs: Vec<syn::Attribute> = vec![];
184+
quote! {
185+
#(#attrs)*
186+
#[allow(unused)]
187+
#[allow(unused)]
188+
#[allow(unused)]
189+
#item
190+
}
191+
.into()
192+
}

tests/ui/duplicated_attributes.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
//@aux-build:proc_macro_attr.rs
2+
13
#![warn(clippy::duplicated_attributes)]
24
#![cfg(any(unix, windows))]
35
#![allow(dead_code)]
46
#![allow(dead_code)] //~ ERROR: duplicated attribute
57
#![cfg(any(unix, windows))] // Should not warn!
68

9+
#[macro_use]
10+
extern crate proc_macro_attr;
11+
712
#[cfg(any(unix, windows, target_os = "linux"))]
813
#[allow(dead_code)]
914
#[allow(dead_code)] //~ ERROR: duplicated attribute
@@ -12,7 +17,10 @@ fn foo() {}
1217

1318
#[cfg(unix)]
1419
#[cfg(windows)]
15-
#[cfg(unix)] //~ ERROR: duplicated attribute
20+
#[cfg(unix)] // cfgs are not handled
1621
fn bar() {}
1722

23+
#[proc_macro_attr::duplicated_attr()] // Should not warn!
24+
fn babar() {}
25+
1826
fn main() {}

tests/ui/duplicated_attributes.stderr

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
11
error: duplicated attribute
2-
--> tests/ui/duplicated_attributes.rs:4:10
2+
--> tests/ui/duplicated_attributes.rs:6:10
33
|
44
LL | #![allow(dead_code)]
55
| ^^^^^^^^^
66
|
77
note: first defined here
8-
--> tests/ui/duplicated_attributes.rs:3:10
8+
--> tests/ui/duplicated_attributes.rs:5:10
99
|
1010
LL | #![allow(dead_code)]
1111
| ^^^^^^^^^
1212
help: remove this attribute
13-
--> tests/ui/duplicated_attributes.rs:4:10
13+
--> tests/ui/duplicated_attributes.rs:6:10
1414
|
1515
LL | #![allow(dead_code)]
1616
| ^^^^^^^^^
1717
= note: `-D clippy::duplicated-attributes` implied by `-D warnings`
1818
= help: to override `-D warnings` add `#[allow(clippy::duplicated_attributes)]`
1919

2020
error: duplicated attribute
21-
--> tests/ui/duplicated_attributes.rs:9:9
21+
--> tests/ui/duplicated_attributes.rs:14:9
2222
|
2323
LL | #[allow(dead_code)]
2424
| ^^^^^^^^^
2525
|
2626
note: first defined here
27-
--> tests/ui/duplicated_attributes.rs:8:9
27+
--> tests/ui/duplicated_attributes.rs:13:9
2828
|
2929
LL | #[allow(dead_code)]
3030
| ^^^^^^^^^
3131
help: remove this attribute
32-
--> tests/ui/duplicated_attributes.rs:9:9
32+
--> tests/ui/duplicated_attributes.rs:14:9
3333
|
3434
LL | #[allow(dead_code)]
3535
| ^^^^^^^^^
3636

37-
error: duplicated attribute
38-
--> tests/ui/duplicated_attributes.rs:15:7
39-
|
40-
LL | #[cfg(unix)]
41-
| ^^^^
42-
|
43-
note: first defined here
44-
--> tests/ui/duplicated_attributes.rs:13:7
45-
|
46-
LL | #[cfg(unix)]
47-
| ^^^^
48-
help: remove this attribute
49-
--> tests/ui/duplicated_attributes.rs:15:7
50-
|
51-
LL | #[cfg(unix)]
52-
| ^^^^
53-
54-
error: aborting due to 3 previous errors
37+
error: aborting due to 2 previous errors
5538

tests/ui/unnecessary_clippy_cfg.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,41 @@ error: no need to put clippy lints behind a `clippy` cfg
5757
LL | #![cfg_attr(clippy, deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `#![deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg)]`
5959

60-
error: aborting due to 8 previous errors
60+
error: duplicated attribute
61+
--> tests/ui/unnecessary_clippy_cfg.rs:8:26
62+
|
63+
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
64+
| ^^^^^^^^^
65+
|
66+
note: first defined here
67+
--> tests/ui/unnecessary_clippy_cfg.rs:6:26
68+
|
69+
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
70+
| ^^^^^^^^^
71+
help: remove this attribute
72+
--> tests/ui/unnecessary_clippy_cfg.rs:8:26
73+
|
74+
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
75+
| ^^^^^^^^^
76+
= note: `-D clippy::duplicated-attributes` implied by `-D warnings`
77+
= help: to override `-D warnings` add `#[allow(clippy::duplicated_attributes)]`
78+
79+
error: duplicated attribute
80+
--> tests/ui/unnecessary_clippy_cfg.rs:17:25
81+
|
82+
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
83+
| ^^^^^^^^^
84+
|
85+
note: first defined here
86+
--> tests/ui/unnecessary_clippy_cfg.rs:15:25
87+
|
88+
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
89+
| ^^^^^^^^^
90+
help: remove this attribute
91+
--> tests/ui/unnecessary_clippy_cfg.rs:17:25
92+
|
93+
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
94+
| ^^^^^^^^^
95+
96+
error: aborting due to 10 previous errors
6197

tests/ui/useless_attribute.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build:proc_macro_derive.rs
22

3-
#![allow(unused)]
3+
#![allow(unused, clippy::duplicated_attributes)]
44
#![warn(clippy::useless_attribute)]
55
#![warn(unreachable_pub)]
66
#![feature(rustc_private)]

tests/ui/useless_attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build:proc_macro_derive.rs
22

3-
#![allow(unused)]
3+
#![allow(unused, clippy::duplicated_attributes)]
44
#![warn(clippy::useless_attribute)]
55
#![warn(unreachable_pub)]
66
#![feature(rustc_private)]

0 commit comments

Comments
 (0)