useless_attribute: Permit wildcard_imports and enum_glob_use

This commit is contained in:
Taiki Endo 2020-09-01 12:09:32 +09:00
parent 001c1c51d2
commit 2e4b4cebbb
4 changed files with 39 additions and 17 deletions

View file

@ -71,8 +71,9 @@ declare_clippy_lint! {
/// **What it does:** Checks for `extern crate` and `use` items annotated with /// **What it does:** Checks for `extern crate` and `use` items annotated with
/// lint attributes. /// lint attributes.
/// ///
/// This lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]` and /// This lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]`,
/// `#[allow(unreachable_pub)]` on `use` items and `#[allow(unused_imports)]` on /// `#[allow(unreachable_pub)]`, `#[allow(clippy::wildcard_imports)]` and
/// `#[allow(clippy::enum_glob_use)]` on `use` items and `#[allow(unused_imports)]` on
/// `extern crate` items with a `#[macro_use]` attribute. /// `extern crate` items with a `#[macro_use]` attribute.
/// ///
/// **Why is this bad?** Lint attributes have no effect on crate imports. Most /// **Why is this bad?** Lint attributes have no effect on crate imports. Most
@ -318,7 +319,8 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
if let Some(ident) = attr.ident() { if let Some(ident) = attr.ident() {
match &*ident.as_str() { match &*ident.as_str() {
"allow" | "warn" | "deny" | "forbid" => { "allow" | "warn" | "deny" | "forbid" => {
// permit `unused_imports`, `deprecated` and `unreachable_pub` for `use` items // permit `unused_imports`, `deprecated`, `unreachable_pub`,
// `clippy::wildcard_imports`, and `clippy::enum_glob_use` for `use` items
// and `unused_imports` for `extern crate` items with `macro_use` // and `unused_imports` for `extern crate` items with `macro_use`
for lint in lint_list { for lint in lint_list {
match item.kind { match item.kind {
@ -327,6 +329,9 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|| is_word(lint, sym!(deprecated)) || is_word(lint, sym!(deprecated))
|| is_word(lint, sym!(unreachable_pub)) || is_word(lint, sym!(unreachable_pub))
|| is_word(lint, sym!(unused)) || is_word(lint, sym!(unused))
|| extract_clippy_lint(lint)
.map_or(false, |s| s == "wildcard_imports")
|| extract_clippy_lint(lint).map_or(false, |s| s == "enum_glob_use")
{ {
return; return;
} }
@ -387,8 +392,8 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
} }
} }
fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) { /// Returns the lint name if it is clippy lint.
fn extract_name(lint: &NestedMetaItem) -> Option<SymbolStr> { fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<SymbolStr> {
if_chain! { if_chain! {
if let Some(meta_item) = lint.meta_item(); if let Some(meta_item) = lint.meta_item();
if meta_item.path.segments.len() > 1; if meta_item.path.segments.len() > 1;
@ -402,9 +407,10 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMet
None None
} }
fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
let lint_store = cx.lints(); let lint_store = cx.lints();
for lint in items { for lint in items {
if let Some(lint_name) = extract_name(lint) { if let Some(lint_name) = extract_clippy_lint(lint) {
if let CheckLintNameResult::Tool(Err((None, _))) = if let CheckLintNameResult::Tool(Err((None, _))) =
lint_store.check_lint_name(&lint_name, Some(sym!(clippy))) lint_store.check_lint_name(&lint_name, Some(sym!(clippy)))
{ {

View file

@ -49,6 +49,14 @@ mod a {
pub use self::b::C; pub use self::b::C;
} }
// don't lint on clippy::wildcard_imports for `use` items
#[allow(clippy::wildcard_imports)]
pub use std::io::prelude::*;
// don't lint on clippy::enum_glob_use for `use` items
#[allow(clippy::enum_glob_use)]
pub use std::cmp::Ordering::*;
fn test_indented_attr() { fn test_indented_attr() {
#![allow(clippy::almost_swapped)] #![allow(clippy::almost_swapped)]
use std::collections::HashSet; use std::collections::HashSet;

View file

@ -49,6 +49,14 @@ mod a {
pub use self::b::C; pub use self::b::C;
} }
// don't lint on clippy::wildcard_imports for `use` items
#[allow(clippy::wildcard_imports)]
pub use std::io::prelude::*;
// don't lint on clippy::enum_glob_use for `use` items
#[allow(clippy::enum_glob_use)]
pub use std::cmp::Ordering::*;
fn test_indented_attr() { fn test_indented_attr() {
#[allow(clippy::almost_swapped)] #[allow(clippy::almost_swapped)]
use std::collections::HashSet; use std::collections::HashSet;

View file

@ -13,7 +13,7 @@ LL | #[cfg_attr(feature = "cargo-clippy", allow(dead_code))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code)`
error: useless lint attribute error: useless lint attribute
--> $DIR/useless_attribute.rs:53:5 --> $DIR/useless_attribute.rs:61:5
| |
LL | #[allow(clippy::almost_swapped)] LL | #[allow(clippy::almost_swapped)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(clippy::almost_swapped)]` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(clippy::almost_swapped)]`