Skip to content

Allow more attributes in clippy::useless_attribute #12755

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

Merged
merged 6 commits into from
May 6, 2024
Merged
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
12 changes: 11 additions & 1 deletion clippy_lints/src/attrs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
49 changes: 32 additions & 17 deletions clippy_lints/src/attrs/useless_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -20,26 +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))
|| 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"
)
})
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 {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui-toml/toml_disallowed_types/conf_disallowed_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
43 changes: 43 additions & 0 deletions tests/ui/useless_attribute.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,51 @@ mod module {

#[rustfmt::skip]
#[allow(unused_import_braces)]
#[allow(unused_braces)]
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;

// 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;
}

// 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::*;
}
43 changes: 43 additions & 0 deletions tests/ui/useless_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,51 @@ mod module {

#[rustfmt::skip]
#[allow(unused_import_braces)]
#[allow(unused_braces)]
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;

// 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;
}

// 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::*;
}