Skip to content

Detect prefixed attributes as duplicated #15212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions clippy_lints/src/attrs/duplicated_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::DUPLICATED_ATTRIBUTES;
use clippy_utils::diagnostics::span_lint_and_then;
use itertools::Itertools;
use rustc_ast::{Attribute, MetaItem};
use rustc_ast_pretty::pprust::path_to_string;
use rustc_data_structures::fx::FxHashMap;
use rustc_lint::EarlyContext;
use rustc_span::{Span, Symbol, sym};
Expand Down Expand Up @@ -35,30 +36,38 @@ fn check_duplicated_attr(
if attr.span.from_expansion() {
return;
}
let Some(ident) = attr.ident() else { return };
let name = ident.name;
if name == sym::doc || name == sym::cfg_attr_trace || name == sym::rustc_on_unimplemented || name == sym::reason {
// FIXME: Would be nice to handle `cfg_attr` as well. Only problem is to check that cfg
// conditions are the same.
// `#[rustc_on_unimplemented]` contains duplicated subattributes, that's expected.
return;
}
if let Some(direct_parent) = parent.last()
&& *direct_parent == sym::cfg_trace
&& [sym::all, sym::not, sym::any].contains(&name)
{
// FIXME: We don't correctly check `cfg`s for now, so if it's more complex than just a one
// level `cfg`, we leave.
return;
let attr_path = path_to_string(&attr.path);
if let Some(ident) = attr.ident() {
let name = ident.name;
if name == sym::doc || name == sym::cfg_attr_trace || name == sym::rustc_on_unimplemented || name == sym::reason
{
// FIXME: Would be nice to handle `cfg_attr` as well. Only problem is to check that cfg
// conditions are the same.
// `#[rustc_on_unimplemented]` contains duplicated subattributes, that's expected.
return;
}
if let Some(direct_parent) = parent.last()
&& *direct_parent == sym::cfg_trace
&& [sym::all, sym::not, sym::any].contains(&name)
{
// FIXME: We don't correctly check `cfg`s for now, so if it's more complex than just a one
// level `cfg`, we leave.
return;
}
}
if let Some(value) = attr.value_str() {
emit_if_duplicated(
cx,
attr,
attr_paths,
format!("{}:{name}={value}", parent.iter().join(":")),
format!("{}:{attr_path}={value}", parent.iter().join(":")),
);
} else if let Some(sub_attrs) = attr.meta_item_list() {
let name = if let Some(ident) = attr.ident() {
ident.name
} else {
Symbol::intern(&attr_path)
};
parent.push(name);
for sub_attr in sub_attrs {
if let Some(meta) = sub_attr.meta_item() {
Expand All @@ -67,7 +76,7 @@ fn check_duplicated_attr(
}
parent.pop();
} else {
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}", parent.iter().join(":")));
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{attr_path}", parent.iter().join(":")));
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/duplicated_attributes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macro_attr.rs
#![warn(clippy::duplicated_attributes, clippy::duplicated_attributes)] //~ ERROR: duplicated attribute
#![feature(rustc_attrs)]
#![warn(clippy::duplicated_attributes)]
#![cfg(any(unix, windows))]
#![allow(dead_code)]
#![allow(dead_code)] //~ ERROR: duplicated attribute
Expand Down
23 changes: 20 additions & 3 deletions tests/ui/duplicated_attributes.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
error: duplicated attribute
--> tests/ui/duplicated_attributes.rs:2:40
|
LL | #![warn(clippy::duplicated_attributes, clippy::duplicated_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first defined here
--> tests/ui/duplicated_attributes.rs:2:9
|
LL | #![warn(clippy::duplicated_attributes, clippy::duplicated_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: remove this attribute
--> tests/ui/duplicated_attributes.rs:2:40
|
LL | #![warn(clippy::duplicated_attributes, clippy::duplicated_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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:6:10
|
Expand All @@ -14,8 +33,6 @@ help: remove this attribute
|
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:14:9
Expand All @@ -34,5 +51,5 @@ help: remove this attribute
LL | #[allow(dead_code)]
| ^^^^^^^^^

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

1 change: 1 addition & 0 deletions tests/ui/indexing_slicing_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
unused
)]
#![warn(clippy::indexing_slicing)]
//~^ duplicated_attributes

extern crate proc_macros;
use proc_macros::with_span;
Expand Down
59 changes: 39 additions & 20 deletions tests/ui/indexing_slicing_slice.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
error: duplicated attribute
--> tests/ui/indexing_slicing_slice.rs:14:9
|
LL | #![warn(clippy::indexing_slicing)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first defined here
--> tests/ui/indexing_slicing_slice.rs:3:9
|
LL | #![warn(clippy::indexing_slicing)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: remove this attribute
--> tests/ui/indexing_slicing_slice.rs:14:9
|
LL | #![warn(clippy::indexing_slicing)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
= note: `-D clippy::duplicated-attributes` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::duplicated_attributes)]`

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:115:6
--> tests/ui/indexing_slicing_slice.rs:116:6
|
LL | &x[index..];
| ^^^^^^^^^^
Expand All @@ -9,47 +28,47 @@ LL | &x[index..];
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:117:6
--> tests/ui/indexing_slicing_slice.rs:118:6
|
LL | &x[..index];
| ^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:119:6
--> tests/ui/indexing_slicing_slice.rs:120:6
|
LL | &x[index_from..index_to];
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:121:6
--> tests/ui/indexing_slicing_slice.rs:122:6
|
LL | &x[index_from..][..index_to];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:121:6
--> tests/ui/indexing_slicing_slice.rs:122:6
|
LL | &x[index_from..][..index_to];
| ^^^^^^^^^^^^^^^
|
= help: consider using `.get(n..)` or .get_mut(n..)` instead

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:124:6
--> tests/ui/indexing_slicing_slice.rs:125:6
|
LL | &x[5..][..10];
| ^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead

error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:124:8
--> tests/ui/indexing_slicing_slice.rs:125:8
|
LL | &x[5..][..10];
| ^
Expand All @@ -58,94 +77,94 @@ LL | &x[5..][..10];
= help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]`

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:127:6
--> tests/ui/indexing_slicing_slice.rs:128:6
|
LL | &x[0..][..3];
| ^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:129:6
--> tests/ui/indexing_slicing_slice.rs:130:6
|
LL | &x[1..][..5];
| ^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead

error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:137:12
--> tests/ui/indexing_slicing_slice.rs:138:12
|
LL | &y[0..=4];
| ^

error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:139:11
--> tests/ui/indexing_slicing_slice.rs:140:11
|
LL | &y[..=4];
| ^

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:145:6
--> tests/ui/indexing_slicing_slice.rs:146:6
|
LL | &v[10..100];
| ^^^^^^^^^^
|
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:147:6
--> tests/ui/indexing_slicing_slice.rs:148:6
|
LL | &x[10..][..100];
| ^^^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead

error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:147:8
--> tests/ui/indexing_slicing_slice.rs:148:8
|
LL | &x[10..][..100];
| ^^

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:150:6
--> tests/ui/indexing_slicing_slice.rs:151:6
|
LL | &v[10..];
| ^^^^^^^
|
= help: consider using `.get(n..)` or .get_mut(n..)` instead

error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:152:6
--> tests/ui/indexing_slicing_slice.rs:153:6
|
LL | &v[..100];
| ^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead

error: indexing may panic
--> tests/ui/indexing_slicing_slice.rs:170:5
--> tests/ui/indexing_slicing_slice.rs:171:5
|
LL | map_with_get[true];
| ^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic
--> tests/ui/indexing_slicing_slice.rs:174:5
--> tests/ui/indexing_slicing_slice.rs:175:5
|
LL | s[0];
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic
--> tests/ui/indexing_slicing_slice.rs:178:5
--> tests/ui/indexing_slicing_slice.rs:179:5
|
LL | y[0];
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: aborting due to 19 previous errors
error: aborting due to 20 previous errors

1 change: 1 addition & 0 deletions tests/ui/needless_collect_indirect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::uninlined_format_args, clippy::useless_vec)]
#![allow(clippy::needless_if, clippy::uninlined_format_args)]
//~^ duplicated_attributes
#![warn(clippy::needless_collect)]
//@no-rustfix
use std::collections::{BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
Expand Down
Loading