From 8d8c0bfb0e2d50ca2a7a4fbbd10cb2e62a319539 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 2 May 2024 11:55:11 -0700 Subject: [PATCH 1/6] useless_attribute: allow clippy::disallowed_types Closes https://github.com/rust-lang/rust-clippy/issues/12753 --- clippy_lints/src/attrs/useless_attribute.rs | 1 + .../ui-toml/toml_disallowed_types/conf_disallowed_types.rs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/clippy_lints/src/attrs/useless_attribute.rs b/clippy_lints/src/attrs/useless_attribute.rs index 7575f502a7c2..7515ee9bbc87 100644 --- a/clippy_lints/src/attrs/useless_attribute.rs +++ b/clippy_lints/src/attrs/useless_attribute.rs @@ -35,6 +35,7 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute]) | "unsafe_removed_from_name" | "module_name_repetitions" | "single_component_path_imports" + | "disallowed_types" ) }) { diff --git a/tests/ui-toml/toml_disallowed_types/conf_disallowed_types.rs b/tests/ui-toml/toml_disallowed_types/conf_disallowed_types.rs index 7f28efd676f2..f02bd07cfe7b 100644 --- a/tests/ui-toml/toml_disallowed_types/conf_disallowed_types.rs +++ b/tests/ui-toml/toml_disallowed_types/conf_disallowed_types.rs @@ -40,3 +40,9 @@ fn main() { let _ = HashMap; let _: usize = 64_usize; } + +mod useless_attribute { + // Regression test for https://github.com/rust-lang/rust-clippy/issues/12753 + #[allow(clippy::disallowed_types)] + use std::collections::HashMap; +} From 17d2ab8f6247ea325bdc7e1c9943a0dcaf31568d Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 2 May 2024 11:56:05 -0700 Subject: [PATCH 2/6] useless_attribute: allow unused_braces --- clippy_lints/src/attrs/useless_attribute.rs | 1 + tests/ui/useless_attribute.fixed | 1 + tests/ui/useless_attribute.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/clippy_lints/src/attrs/useless_attribute.rs b/clippy_lints/src/attrs/useless_attribute.rs index 7515ee9bbc87..40572731bb14 100644 --- a/clippy_lints/src/attrs/useless_attribute.rs +++ b/clippy_lints/src/attrs/useless_attribute.rs @@ -25,6 +25,7 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute]) || is_word(lint, sym!(unreachable_pub)) || is_word(lint, sym!(unused)) || is_word(lint, sym!(unused_import_braces)) + || is_word(lint, sym!(unused_braces)) || extract_clippy_lint(lint).map_or(false, |s| { matches!( s.as_str(), diff --git a/tests/ui/useless_attribute.fixed b/tests/ui/useless_attribute.fixed index 81759086f79e..cb1c03464040 100644 --- a/tests/ui/useless_attribute.fixed +++ b/tests/ui/useless_attribute.fixed @@ -86,6 +86,7 @@ mod module { #[rustfmt::skip] #[allow(unused_import_braces)] +#[allow(unused_braces)] use module::{Struct}; fn main() { diff --git a/tests/ui/useless_attribute.rs b/tests/ui/useless_attribute.rs index 59a9dcf093bc..44a52cba043e 100644 --- a/tests/ui/useless_attribute.rs +++ b/tests/ui/useless_attribute.rs @@ -86,6 +86,7 @@ mod module { #[rustfmt::skip] #[allow(unused_import_braces)] +#[allow(unused_braces)] use module::{Struct}; fn main() { From e9761bdc015430b818ee771b2acf00a00b40bd97 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 2 May 2024 11:56:38 -0700 Subject: [PATCH 3/6] useless_attribute: allow dead_code Closes https://github.com/rust-lang/rust-clippy/issues/4467 --- clippy_lints/src/attrs/useless_attribute.rs | 1 + tests/ui/useless_attribute.fixed | 4 ++++ tests/ui/useless_attribute.rs | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/clippy_lints/src/attrs/useless_attribute.rs b/clippy_lints/src/attrs/useless_attribute.rs index 40572731bb14..720e4e7932d0 100644 --- a/clippy_lints/src/attrs/useless_attribute.rs +++ b/clippy_lints/src/attrs/useless_attribute.rs @@ -26,6 +26,7 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute]) || is_word(lint, sym!(unused)) || is_word(lint, sym!(unused_import_braces)) || is_word(lint, sym!(unused_braces)) + || is_word(lint, sym!(dead_code)) || extract_clippy_lint(lint).map_or(false, |s| { matches!( s.as_str(), diff --git a/tests/ui/useless_attribute.fixed b/tests/ui/useless_attribute.fixed index cb1c03464040..5fafc05cddeb 100644 --- a/tests/ui/useless_attribute.fixed +++ b/tests/ui/useless_attribute.fixed @@ -92,3 +92,7 @@ use module::{Struct}; fn main() { test_indented_attr(); } + +// Regression test for https://github.com/rust-lang/rust-clippy/issues/4467 +#[allow(dead_code)] +use std::collections as puppy_doggy; diff --git a/tests/ui/useless_attribute.rs b/tests/ui/useless_attribute.rs index 44a52cba043e..c9de7677ff8c 100644 --- a/tests/ui/useless_attribute.rs +++ b/tests/ui/useless_attribute.rs @@ -92,3 +92,7 @@ use module::{Struct}; fn main() { test_indented_attr(); } + +// Regression test for https://github.com/rust-lang/rust-clippy/issues/4467 +#[allow(dead_code)] +use std::collections as puppy_doggy; From 96e69d9b43f1de81ead394c76b5552ab4daf88f1 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 2 May 2024 11:57:06 -0700 Subject: [PATCH 4/6] useless_attribute: allow hidden_glob_reexports Closes https://github.com/rust-lang/rust-clippy/issues/11595 --- clippy_lints/src/attrs/useless_attribute.rs | 3 ++- tests/ui/useless_attribute.fixed | 21 +++++++++++++++++++++ tests/ui/useless_attribute.rs | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/attrs/useless_attribute.rs b/clippy_lints/src/attrs/useless_attribute.rs index 720e4e7932d0..00f324e4de54 100644 --- a/clippy_lints/src/attrs/useless_attribute.rs +++ b/clippy_lints/src/attrs/useless_attribute.rs @@ -26,7 +26,8 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute]) || is_word(lint, sym!(unused)) || is_word(lint, sym!(unused_import_braces)) || is_word(lint, sym!(unused_braces)) - || is_word(lint, sym!(dead_code)) + || is_word(lint, sym::dead_code) + || is_word(lint, sym!(hidden_glob_reexports)) || extract_clippy_lint(lint).map_or(false, |s| { matches!( s.as_str(), diff --git a/tests/ui/useless_attribute.fixed b/tests/ui/useless_attribute.fixed index 5fafc05cddeb..401c384c39ca 100644 --- a/tests/ui/useless_attribute.fixed +++ b/tests/ui/useless_attribute.fixed @@ -96,3 +96,24 @@ fn main() { // Regression test for https://github.com/rust-lang/rust-clippy/issues/4467 #[allow(dead_code)] use std::collections as puppy_doggy; + +// Regression test for https://github.com/rust-lang/rust-clippy/issues/11595 +pub mod hidden_glob_reexports { + #![allow(unreachable_pub)] + + mod my_prelude { + pub struct MyCoolTypeInternal; + pub use MyCoolTypeInternal as MyCoolType; + } + + mod my_uncool_type { + pub(crate) struct MyUncoolType; + } + + // This exports `MyCoolType`. + pub use my_prelude::*; + + // This hides `my_prelude::MyCoolType`. + #[allow(hidden_glob_reexports)] + use my_uncool_type::MyUncoolType as MyCoolType; +} diff --git a/tests/ui/useless_attribute.rs b/tests/ui/useless_attribute.rs index c9de7677ff8c..6629942ff5e4 100644 --- a/tests/ui/useless_attribute.rs +++ b/tests/ui/useless_attribute.rs @@ -96,3 +96,24 @@ fn main() { // Regression test for https://github.com/rust-lang/rust-clippy/issues/4467 #[allow(dead_code)] use std::collections as puppy_doggy; + +// Regression test for https://github.com/rust-lang/rust-clippy/issues/11595 +pub mod hidden_glob_reexports { + #![allow(unreachable_pub)] + + mod my_prelude { + pub struct MyCoolTypeInternal; + pub use MyCoolTypeInternal as MyCoolType; + } + + mod my_uncool_type { + pub(crate) struct MyUncoolType; + } + + // This exports `MyCoolType`. + pub use my_prelude::*; + + // This hides `my_prelude::MyCoolType`. + #[allow(hidden_glob_reexports)] + use my_uncool_type::MyUncoolType as MyCoolType; +} From db0cbbacb774e7c645c740b27914aec313ed0a7e Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 2 May 2024 11:57:27 -0700 Subject: [PATCH 5/6] useless_attribute: allow ambiguous_glob_exports Closes https://github.com/rust-lang/rust-clippy/issues/10878 --- clippy_lints/src/attrs/useless_attribute.rs | 1 + tests/ui/useless_attribute.fixed | 17 +++++++++++++++++ tests/ui/useless_attribute.rs | 17 +++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/clippy_lints/src/attrs/useless_attribute.rs b/clippy_lints/src/attrs/useless_attribute.rs index 00f324e4de54..f6f4d7804404 100644 --- a/clippy_lints/src/attrs/useless_attribute.rs +++ b/clippy_lints/src/attrs/useless_attribute.rs @@ -28,6 +28,7 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute]) || is_word(lint, sym!(unused_braces)) || is_word(lint, sym::dead_code) || is_word(lint, sym!(hidden_glob_reexports)) + || is_word(lint, sym!(ambiguous_glob_reexports)) || extract_clippy_lint(lint).map_or(false, |s| { matches!( s.as_str(), diff --git a/tests/ui/useless_attribute.fixed b/tests/ui/useless_attribute.fixed index 401c384c39ca..231fc0a892ad 100644 --- a/tests/ui/useless_attribute.fixed +++ b/tests/ui/useless_attribute.fixed @@ -117,3 +117,20 @@ pub mod hidden_glob_reexports { #[allow(hidden_glob_reexports)] use my_uncool_type::MyUncoolType as MyCoolType; } + +// Regression test for https://github.com/rust-lang/rust-clippy/issues/10878 +pub mod ambiguous_glob_exports { + #![allow(unreachable_pub)] + + mod my_prelude { + pub struct MyType; + } + + mod my_type { + pub struct MyType; + } + + #[allow(ambiguous_glob_reexports)] + pub use my_prelude::*; + pub use my_type::*; +} diff --git a/tests/ui/useless_attribute.rs b/tests/ui/useless_attribute.rs index 6629942ff5e4..8dfcd2110a4b 100644 --- a/tests/ui/useless_attribute.rs +++ b/tests/ui/useless_attribute.rs @@ -117,3 +117,20 @@ pub mod hidden_glob_reexports { #[allow(hidden_glob_reexports)] use my_uncool_type::MyUncoolType as MyCoolType; } + +// Regression test for https://github.com/rust-lang/rust-clippy/issues/10878 +pub mod ambiguous_glob_exports { + #![allow(unreachable_pub)] + + mod my_prelude { + pub struct MyType; + } + + mod my_type { + pub struct MyType; + } + + #[allow(ambiguous_glob_reexports)] + pub use my_prelude::*; + pub use my_type::*; +} From 566bfff8bb36eb4d52a637e28732ab38439491c5 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 2 May 2024 12:05:45 -0700 Subject: [PATCH 6/6] useless_attribute: Update docs for allowed attributes --- clippy_lints/src/attrs/mod.rs | 12 ++++- clippy_lints/src/attrs/useless_attribute.rs | 54 ++++++++++++--------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/clippy_lints/src/attrs/mod.rs b/clippy_lints/src/attrs/mod.rs index 8f47bc7653b7..39f406077995 100644 --- a/clippy_lints/src/attrs/mod.rs +++ b/clippy_lints/src/attrs/mod.rs @@ -61,11 +61,21 @@ declare_clippy_lint! { /// /// This lint permits lint attributes for lints emitted on the items themself. /// For `use` items these lints are: + /// * ambiguous_glob_reexports + /// * dead_code /// * deprecated + /// * hidden_glob_reexports /// * unreachable_pub - /// * unused_imports + /// * unused + /// * unused_braces + /// * unused_import_braces + /// * clippy::disallowed_types /// * clippy::enum_glob_use /// * clippy::macro_use_imports + /// * clippy::module_name_repetitions + /// * clippy::redundant_pub_crate + /// * clippy::single_component_path_imports + /// * clippy::unsafe_removed_from_name /// * clippy::wildcard_imports /// /// For `extern crate` items these lints are: diff --git a/clippy_lints/src/attrs/useless_attribute.rs b/clippy_lints/src/attrs/useless_attribute.rs index f6f4d7804404..f0868231d01a 100644 --- a/clippy_lints/src/attrs/useless_attribute.rs +++ b/clippy_lints/src/attrs/useless_attribute.rs @@ -2,6 +2,7 @@ use super::utils::{extract_clippy_lint, is_lint_level, is_word}; use super::{Attribute, USELESS_ATTRIBUTE}; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::{first_line_of_span, snippet_opt}; +use rustc_ast::NestedMetaItem; use rustc_errors::Applicability; use rustc_hir::{Item, ItemKind}; use rustc_lint::{LateContext, LintContext}; @@ -20,31 +21,40 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute]) for lint in lint_list { match item.kind { ItemKind::Use(..) => { - if is_word(lint, sym::unused_imports) - || is_word(lint, sym::deprecated) - || is_word(lint, sym!(unreachable_pub)) - || is_word(lint, sym!(unused)) - || is_word(lint, sym!(unused_import_braces)) - || is_word(lint, sym!(unused_braces)) - || is_word(lint, sym::dead_code) - || is_word(lint, sym!(hidden_glob_reexports)) - || is_word(lint, sym!(ambiguous_glob_reexports)) - || extract_clippy_lint(lint).map_or(false, |s| { - matches!( - s.as_str(), - "wildcard_imports" - | "enum_glob_use" - | "redundant_pub_crate" - | "macro_use_imports" - | "unsafe_removed_from_name" - | "module_name_repetitions" - | "single_component_path_imports" - | "disallowed_types" - ) - }) + if let NestedMetaItem::MetaItem(meta_item) = lint + && meta_item.is_word() + && let Some(ident) = meta_item.ident() + && matches!( + ident.name.as_str(), + "ambiguous_glob_reexports" + | "dead_code" + | "deprecated" + | "hidden_glob_reexports" + | "unreachable_pub" + | "unused" + | "unused_braces" + | "unused_import_braces" + | "unused_imports" + ) { return; } + + if extract_clippy_lint(lint).is_some_and(|symbol| { + matches!( + symbol.as_str(), + "wildcard_imports" + | "enum_glob_use" + | "redundant_pub_crate" + | "macro_use_imports" + | "unsafe_removed_from_name" + | "module_name_repetitions" + | "single_component_path_imports" + | "disallowed_types" + ) + }) { + return; + } }, ItemKind::ExternCrate(..) => { if is_word(lint, sym::unused_imports) && skip_unused_imports {