Skip to content

Commit

Permalink
Auto merge of rust-lang#12646 - GuillaumeGomez:regression-test-12537,…
Browse files Browse the repository at this point in the history
… r=blyxyas

Turn `duplicated_attributes` into a late lint

Fixes rust-lang#12537.

changelog: Turn `duplicated_attributes` into a late lint
  • Loading branch information
bors committed Apr 9, 2024
2 parents ccc93f9 + f7d49a3 commit 62fd1d5
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 39 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/attrs/duplicated_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::DUPLICATED_ATTRIBUTES;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_ast::{Attribute, MetaItem};
use rustc_data_structures::fx::FxHashMap;
use rustc_lint::EarlyContext;
use rustc_lint::LateContext;
use rustc_span::{sym, Span};
use std::collections::hash_map::Entry;

fn emit_if_duplicated(
cx: &EarlyContext<'_>,
cx: &LateContext<'_>,
attr: &MetaItem,
attr_paths: &mut FxHashMap<String, Span>,
complete_path: String,
Expand All @@ -26,7 +26,7 @@ fn emit_if_duplicated(
}

fn check_duplicated_attr(
cx: &EarlyContext<'_>,
cx: &LateContext<'_>,
attr: &MetaItem,
attr_paths: &mut FxHashMap<String, Span>,
parent: &mut Vec<String>,
Expand Down Expand Up @@ -64,7 +64,7 @@ fn check_duplicated_attr(
}
}

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

for attr in attrs {
Expand Down
11 changes: 4 additions & 7 deletions clippy_lints/src/attrs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod useless_attribute;
mod utils;

use clippy_config::msrvs::Msrv;
use rustc_ast::{Attribute, Crate, MetaItemKind, NestedMetaItem};
use rustc_ast::{Attribute, MetaItemKind, NestedMetaItem};
use rustc_hir::{ImplItem, Item, ItemKind, TraitItem};
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, impl_lint_pass};
Expand Down Expand Up @@ -534,11 +534,13 @@ declare_lint_pass!(Attributes => [
BLANKET_CLIPPY_RESTRICTION_LINTS,
SHOULD_PANIC_WITHOUT_EXPECT,
MIXED_ATTRIBUTES_STYLE,
DUPLICATED_ATTRIBUTES,
]);

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

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

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

impl EarlyLintPass for EarlyAttributes {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
duplicated_attributes::check(cx, &krate.attrs);
}

fn check_item(&mut self, cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
empty_line_after::check(cx, item);
duplicated_attributes::check(cx, &item.attrs);
}

fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/auxiliary/proc_macro_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,17 @@ pub fn with_empty_docs(_attr: TokenStream, input: TokenStream) -> TokenStream {
}
.into()
}

#[proc_macro_attribute]
pub fn duplicated_attr(_attr: TokenStream, input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as syn::Item);
let attrs: Vec<syn::Attribute> = vec![];
quote! {
#(#attrs)*
#[allow(unused)]
#[allow(unused)]
#[allow(unused)]
#item
}
.into()
}
10 changes: 9 additions & 1 deletion tests/ui/duplicated_attributes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
//@aux-build:proc_macro_attr.rs

#![warn(clippy::duplicated_attributes)]
#![cfg(any(unix, windows))]
#![allow(dead_code)]
#![allow(dead_code)] //~ ERROR: duplicated attribute
#![cfg(any(unix, windows))] // Should not warn!

#[macro_use]
extern crate proc_macro_attr;

#[cfg(any(unix, windows, target_os = "linux"))]
#[allow(dead_code)]
#[allow(dead_code)] //~ ERROR: duplicated attribute
Expand All @@ -12,7 +17,10 @@ fn foo() {}

#[cfg(unix)]
#[cfg(windows)]
#[cfg(unix)] //~ ERROR: duplicated attribute
#[cfg(unix)] // cfgs are not handled
fn bar() {}

#[proc_macro_attr::duplicated_attr()] // Should not warn!
fn babar() {}

fn main() {}
31 changes: 7 additions & 24 deletions tests/ui/duplicated_attributes.stderr
Original file line number Diff line number Diff line change
@@ -1,55 +1,38 @@
error: duplicated attribute
--> tests/ui/duplicated_attributes.rs:4:10
--> tests/ui/duplicated_attributes.rs:6:10
|
LL | #![allow(dead_code)]
| ^^^^^^^^^
|
note: first defined here
--> tests/ui/duplicated_attributes.rs:3:10
--> tests/ui/duplicated_attributes.rs:5:10
|
LL | #![allow(dead_code)]
| ^^^^^^^^^
help: remove this attribute
--> tests/ui/duplicated_attributes.rs:4:10
--> tests/ui/duplicated_attributes.rs:6:10
|
LL | #![allow(dead_code)]
| ^^^^^^^^^
= note: `-D clippy::duplicated-attributes` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::duplicated_attributes)]`

error: duplicated attribute
--> tests/ui/duplicated_attributes.rs:9:9
--> tests/ui/duplicated_attributes.rs:14:9
|
LL | #[allow(dead_code)]
| ^^^^^^^^^
|
note: first defined here
--> tests/ui/duplicated_attributes.rs:8:9
--> tests/ui/duplicated_attributes.rs:13:9
|
LL | #[allow(dead_code)]
| ^^^^^^^^^
help: remove this attribute
--> tests/ui/duplicated_attributes.rs:9:9
--> tests/ui/duplicated_attributes.rs:14:9
|
LL | #[allow(dead_code)]
| ^^^^^^^^^

error: duplicated attribute
--> tests/ui/duplicated_attributes.rs:15:7
|
LL | #[cfg(unix)]
| ^^^^
|
note: first defined here
--> tests/ui/duplicated_attributes.rs:13:7
|
LL | #[cfg(unix)]
| ^^^^
help: remove this attribute
--> tests/ui/duplicated_attributes.rs:15:7
|
LL | #[cfg(unix)]
| ^^^^

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

38 changes: 37 additions & 1 deletion tests/ui/unnecessary_clippy_cfg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,41 @@ error: no need to put clippy lints behind a `clippy` cfg
LL | #![cfg_attr(clippy, deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `#![deny(clippy::non_minimal_cfg, clippy::maybe_misused_cfg)]`

error: aborting due to 8 previous errors
error: duplicated attribute
--> tests/ui/unnecessary_clippy_cfg.rs:8:26
|
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
| ^^^^^^^^^
|
note: first defined here
--> tests/ui/unnecessary_clippy_cfg.rs:6:26
|
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^
help: remove this attribute
--> tests/ui/unnecessary_clippy_cfg.rs:8:26
|
LL | #![cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
| ^^^^^^^^^
= note: `-D clippy::duplicated-attributes` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::duplicated_attributes)]`

error: duplicated attribute
--> tests/ui/unnecessary_clippy_cfg.rs:17:25
|
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
| ^^^^^^^^^
|
note: first defined here
--> tests/ui/unnecessary_clippy_cfg.rs:15:25
|
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg))]
| ^^^^^^^^^
help: remove this attribute
--> tests/ui/unnecessary_clippy_cfg.rs:17:25
|
LL | #[cfg_attr(clippy, deny(dead_code, clippy::non_minimal_cfg, clippy::maybe_misused_cfg))]
| ^^^^^^^^^

error: aborting due to 10 previous errors

2 changes: 1 addition & 1 deletion tests/ui/useless_attribute.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macro_derive.rs

#![allow(unused)]
#![allow(unused, clippy::duplicated_attributes)]
#![warn(clippy::useless_attribute)]
#![warn(unreachable_pub)]
#![feature(rustc_private)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/useless_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macro_derive.rs

#![allow(unused)]
#![allow(unused, clippy::duplicated_attributes)]
#![warn(clippy::useless_attribute)]
#![warn(unreachable_pub)]
#![feature(rustc_private)]
Expand Down

0 comments on commit 62fd1d5

Please sign in to comment.