Skip to content

Commit befb659

Browse files
committed
Auto merge of #12755 - 9999years:useless-attribute, r=xFrednet,GuillaumeGomez
Allow more attributes in `clippy::useless_attribute` Fixes #12753 Fixes #4467 Fixes #11595 Fixes #10878 changelog: [`useless_attribute`]: Attributes allowed on `use` items now include `ambiguous_glob_exports`, `hidden_glob_reexports`, `dead_code`, `unused_braces`, and `clippy::disallowed_types`.
2 parents 3ef3a13 + 566bfff commit befb659

File tree

5 files changed

+135
-18
lines changed

5 files changed

+135
-18
lines changed

clippy_lints/src/attrs/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,21 @@ declare_clippy_lint! {
6161
///
6262
/// This lint permits lint attributes for lints emitted on the items themself.
6363
/// For `use` items these lints are:
64+
/// * ambiguous_glob_reexports
65+
/// * dead_code
6466
/// * deprecated
67+
/// * hidden_glob_reexports
6568
/// * unreachable_pub
66-
/// * unused_imports
69+
/// * unused
70+
/// * unused_braces
71+
/// * unused_import_braces
72+
/// * clippy::disallowed_types
6773
/// * clippy::enum_glob_use
6874
/// * clippy::macro_use_imports
75+
/// * clippy::module_name_repetitions
76+
/// * clippy::redundant_pub_crate
77+
/// * clippy::single_component_path_imports
78+
/// * clippy::unsafe_removed_from_name
6979
/// * clippy::wildcard_imports
7080
///
7181
/// For `extern crate` items these lints are:

clippy_lints/src/attrs/useless_attribute.rs

+32-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::utils::{extract_clippy_lint, is_lint_level, is_word};
22
use super::{Attribute, USELESS_ATTRIBUTE};
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::source::{first_line_of_span, snippet_opt};
5+
use rustc_ast::NestedMetaItem;
56
use rustc_errors::Applicability;
67
use rustc_hir::{Item, ItemKind};
78
use rustc_lint::{LateContext, LintContext};
@@ -20,26 +21,40 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute])
2021
for lint in lint_list {
2122
match item.kind {
2223
ItemKind::Use(..) => {
23-
if is_word(lint, sym::unused_imports)
24-
|| is_word(lint, sym::deprecated)
25-
|| is_word(lint, sym!(unreachable_pub))
26-
|| is_word(lint, sym!(unused))
27-
|| is_word(lint, sym!(unused_import_braces))
28-
|| extract_clippy_lint(lint).map_or(false, |s| {
29-
matches!(
30-
s.as_str(),
31-
"wildcard_imports"
32-
| "enum_glob_use"
33-
| "redundant_pub_crate"
34-
| "macro_use_imports"
35-
| "unsafe_removed_from_name"
36-
| "module_name_repetitions"
37-
| "single_component_path_imports"
38-
)
39-
})
24+
if let NestedMetaItem::MetaItem(meta_item) = lint
25+
&& meta_item.is_word()
26+
&& let Some(ident) = meta_item.ident()
27+
&& matches!(
28+
ident.name.as_str(),
29+
"ambiguous_glob_reexports"
30+
| "dead_code"
31+
| "deprecated"
32+
| "hidden_glob_reexports"
33+
| "unreachable_pub"
34+
| "unused"
35+
| "unused_braces"
36+
| "unused_import_braces"
37+
| "unused_imports"
38+
)
4039
{
4140
return;
4241
}
42+
43+
if extract_clippy_lint(lint).is_some_and(|symbol| {
44+
matches!(
45+
symbol.as_str(),
46+
"wildcard_imports"
47+
| "enum_glob_use"
48+
| "redundant_pub_crate"
49+
| "macro_use_imports"
50+
| "unsafe_removed_from_name"
51+
| "module_name_repetitions"
52+
| "single_component_path_imports"
53+
| "disallowed_types"
54+
)
55+
}) {
56+
return;
57+
}
4358
},
4459
ItemKind::ExternCrate(..) => {
4560
if is_word(lint, sym::unused_imports) && skip_unused_imports {

tests/ui-toml/toml_disallowed_types/conf_disallowed_types.rs

+6
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ fn main() {
4040
let _ = HashMap;
4141
let _: usize = 64_usize;
4242
}
43+
44+
mod useless_attribute {
45+
// Regression test for https://github.com/rust-lang/rust-clippy/issues/12753
46+
#[allow(clippy::disallowed_types)]
47+
use std::collections::HashMap;
48+
}

tests/ui/useless_attribute.fixed

+43
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,51 @@ mod module {
8686

8787
#[rustfmt::skip]
8888
#[allow(unused_import_braces)]
89+
#[allow(unused_braces)]
8990
use module::{Struct};
9091

9192
fn main() {
9293
test_indented_attr();
9394
}
95+
96+
// Regression test for https://github.com/rust-lang/rust-clippy/issues/4467
97+
#[allow(dead_code)]
98+
use std::collections as puppy_doggy;
99+
100+
// Regression test for https://github.com/rust-lang/rust-clippy/issues/11595
101+
pub mod hidden_glob_reexports {
102+
#![allow(unreachable_pub)]
103+
104+
mod my_prelude {
105+
pub struct MyCoolTypeInternal;
106+
pub use MyCoolTypeInternal as MyCoolType;
107+
}
108+
109+
mod my_uncool_type {
110+
pub(crate) struct MyUncoolType;
111+
}
112+
113+
// This exports `MyCoolType`.
114+
pub use my_prelude::*;
115+
116+
// This hides `my_prelude::MyCoolType`.
117+
#[allow(hidden_glob_reexports)]
118+
use my_uncool_type::MyUncoolType as MyCoolType;
119+
}
120+
121+
// Regression test for https://github.com/rust-lang/rust-clippy/issues/10878
122+
pub mod ambiguous_glob_exports {
123+
#![allow(unreachable_pub)]
124+
125+
mod my_prelude {
126+
pub struct MyType;
127+
}
128+
129+
mod my_type {
130+
pub struct MyType;
131+
}
132+
133+
#[allow(ambiguous_glob_reexports)]
134+
pub use my_prelude::*;
135+
pub use my_type::*;
136+
}

tests/ui/useless_attribute.rs

+43
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,51 @@ mod module {
8686

8787
#[rustfmt::skip]
8888
#[allow(unused_import_braces)]
89+
#[allow(unused_braces)]
8990
use module::{Struct};
9091

9192
fn main() {
9293
test_indented_attr();
9394
}
95+
96+
// Regression test for https://github.com/rust-lang/rust-clippy/issues/4467
97+
#[allow(dead_code)]
98+
use std::collections as puppy_doggy;
99+
100+
// Regression test for https://github.com/rust-lang/rust-clippy/issues/11595
101+
pub mod hidden_glob_reexports {
102+
#![allow(unreachable_pub)]
103+
104+
mod my_prelude {
105+
pub struct MyCoolTypeInternal;
106+
pub use MyCoolTypeInternal as MyCoolType;
107+
}
108+
109+
mod my_uncool_type {
110+
pub(crate) struct MyUncoolType;
111+
}
112+
113+
// This exports `MyCoolType`.
114+
pub use my_prelude::*;
115+
116+
// This hides `my_prelude::MyCoolType`.
117+
#[allow(hidden_glob_reexports)]
118+
use my_uncool_type::MyUncoolType as MyCoolType;
119+
}
120+
121+
// Regression test for https://github.com/rust-lang/rust-clippy/issues/10878
122+
pub mod ambiguous_glob_exports {
123+
#![allow(unreachable_pub)]
124+
125+
mod my_prelude {
126+
pub struct MyType;
127+
}
128+
129+
mod my_type {
130+
pub struct MyType;
131+
}
132+
133+
#[allow(ambiguous_glob_reexports)]
134+
pub use my_prelude::*;
135+
pub use my_type::*;
136+
}

0 commit comments

Comments
 (0)