Skip to content

Commit 2e4b4ce

Browse files
committed
useless_attribute: Permit wildcard_imports and enum_glob_use
1 parent 001c1c5 commit 2e4b4ce

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

clippy_lints/src/attrs.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ declare_clippy_lint! {
7171
/// **What it does:** Checks for `extern crate` and `use` items annotated with
7272
/// lint attributes.
7373
///
74-
/// This lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]` and
75-
/// `#[allow(unreachable_pub)]` on `use` items and `#[allow(unused_imports)]` on
74+
/// This lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]`,
75+
/// `#[allow(unreachable_pub)]`, `#[allow(clippy::wildcard_imports)]` and
76+
/// `#[allow(clippy::enum_glob_use)]` on `use` items and `#[allow(unused_imports)]` on
7677
/// `extern crate` items with a `#[macro_use]` attribute.
7778
///
7879
/// **Why is this bad?** Lint attributes have no effect on crate imports. Most
@@ -318,7 +319,8 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
318319
if let Some(ident) = attr.ident() {
319320
match &*ident.as_str() {
320321
"allow" | "warn" | "deny" | "forbid" => {
321-
// permit `unused_imports`, `deprecated` and `unreachable_pub` for `use` items
322+
// permit `unused_imports`, `deprecated`, `unreachable_pub`,
323+
// `clippy::wildcard_imports`, and `clippy::enum_glob_use` for `use` items
322324
// and `unused_imports` for `extern crate` items with `macro_use`
323325
for lint in lint_list {
324326
match item.kind {
@@ -327,6 +329,9 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
327329
|| is_word(lint, sym!(deprecated))
328330
|| is_word(lint, sym!(unreachable_pub))
329331
|| is_word(lint, sym!(unused))
332+
|| extract_clippy_lint(lint)
333+
.map_or(false, |s| s == "wildcard_imports")
334+
|| extract_clippy_lint(lint).map_or(false, |s| s == "enum_glob_use")
330335
{
331336
return;
332337
}
@@ -387,24 +392,25 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
387392
}
388393
}
389394

390-
fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
391-
fn extract_name(lint: &NestedMetaItem) -> Option<SymbolStr> {
392-
if_chain! {
393-
if let Some(meta_item) = lint.meta_item();
394-
if meta_item.path.segments.len() > 1;
395-
if let tool_name = meta_item.path.segments[0].ident;
396-
if tool_name.as_str() == "clippy";
397-
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
398-
then {
399-
return Some(lint_name.as_str());
400-
}
395+
/// Returns the lint name if it is clippy lint.
396+
fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<SymbolStr> {
397+
if_chain! {
398+
if let Some(meta_item) = lint.meta_item();
399+
if meta_item.path.segments.len() > 1;
400+
if let tool_name = meta_item.path.segments[0].ident;
401+
if tool_name.as_str() == "clippy";
402+
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
403+
then {
404+
return Some(lint_name.as_str());
401405
}
402-
None
403406
}
407+
None
408+
}
404409

410+
fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
405411
let lint_store = cx.lints();
406412
for lint in items {
407-
if let Some(lint_name) = extract_name(lint) {
413+
if let Some(lint_name) = extract_clippy_lint(lint) {
408414
if let CheckLintNameResult::Tool(Err((None, _))) =
409415
lint_store.check_lint_name(&lint_name, Some(sym!(clippy)))
410416
{

tests/ui/useless_attribute.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ mod a {
4949
pub use self::b::C;
5050
}
5151

52+
// don't lint on clippy::wildcard_imports for `use` items
53+
#[allow(clippy::wildcard_imports)]
54+
pub use std::io::prelude::*;
55+
56+
// don't lint on clippy::enum_glob_use for `use` items
57+
#[allow(clippy::enum_glob_use)]
58+
pub use std::cmp::Ordering::*;
59+
5260
fn test_indented_attr() {
5361
#![allow(clippy::almost_swapped)]
5462
use std::collections::HashSet;

tests/ui/useless_attribute.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ mod a {
4949
pub use self::b::C;
5050
}
5151

52+
// don't lint on clippy::wildcard_imports for `use` items
53+
#[allow(clippy::wildcard_imports)]
54+
pub use std::io::prelude::*;
55+
56+
// don't lint on clippy::enum_glob_use for `use` items
57+
#[allow(clippy::enum_glob_use)]
58+
pub use std::cmp::Ordering::*;
59+
5260
fn test_indented_attr() {
5361
#[allow(clippy::almost_swapped)]
5462
use std::collections::HashSet;

tests/ui/useless_attribute.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | #[cfg_attr(feature = "cargo-clippy", allow(dead_code))]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code)`
1414

1515
error: useless lint attribute
16-
--> $DIR/useless_attribute.rs:53:5
16+
--> $DIR/useless_attribute.rs:61:5
1717
|
1818
LL | #[allow(clippy::almost_swapped)]
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(clippy::almost_swapped)]`

0 commit comments

Comments
 (0)