Turn duplicated_attributes into a late lint

This commit is contained in:
Guillaume Gomez 2024-04-08 14:12:32 +02:00
parent 2202493a67
commit f7d49a340d
8 changed files with 77 additions and 39 deletions

View File

@ -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,
@ -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>,
@ -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 {

View File

@ -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};
@ -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) {
@ -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<'_>) {
@ -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) {

View File

@ -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()
}

View File

@ -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
@ -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() {}

View File

@ -1,16 +1,16 @@
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)]
| ^^^^^^^^^
@ -18,38 +18,21 @@ LL | #![allow(dead_code)]
= 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

View File

@ -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

View File

@ -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)]

View File

@ -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)]