Skip to content

Commit

Permalink
useless_attribute: Add unreachable_pub to whitelists
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed May 18, 2019
1 parent 60a609a commit 40fc725
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
9 changes: 5 additions & 4 deletions clippy_lints/src/attrs.rs
Expand Up @@ -49,9 +49,9 @@ declare_clippy_lint! {
/// **What it does:** Checks for `extern crate` and `use` items annotated with
/// lint attributes.
///
/// This lint whitelists `#[allow(unused_imports)]` and `#[allow(deprecated)]` on
/// `use` items and `#[allow(unused_imports)]` on `extern crate` items with a
/// `#[macro_use]` attribute.
/// This lint whitelists `#[allow(unused_imports)]`, `#[allow(deprecated)]` and
/// `#[allow(unreachable_pub)]` on `use` items and `#[allow(unused_imports)]` on
/// `extern crate` items with a `#[macro_use]` attribute.
///
/// **Why is this bad?** Lint attributes have no effect on crate imports. Most
/// likely a `!` was forgotten.
Expand Down Expand Up @@ -239,13 +239,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes {
if let Some(ident) = attr.ident() {
match &*ident.as_str() {
"allow" | "warn" | "deny" | "forbid" => {
// whitelist `unused_imports` and `deprecated` for `use` items
// whitelist `unused_imports`, `deprecated` and `unreachable_pub` for `use` items
// and `unused_imports` for `extern crate` items with `macro_use`
for lint in lint_list {
match item.node {
ItemKind::Use(..) => {
if is_word(lint, sym!(unused_imports))
|| is_word(lint, sym!(deprecated))
|| is_word(lint, sym!(unreachable_pub))
{
return;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/useless_attribute.rs
@@ -1,6 +1,7 @@
// aux-build:proc_macro_derive.rs

#![warn(clippy::useless_attribute)]
#![warn(unreachable_pub)]

#[allow(dead_code)]
#[cfg_attr(feature = "cargo-clippy", allow(dead_code))]
Expand Down Expand Up @@ -32,4 +33,16 @@ pub use foo::Bar;
#[derive(DeriveSomething)]
struct Baz;

// don't lint on unreachable_pub for `use` items
mod a {
mod b {
#[allow(dead_code)]
#[allow(unreachable_pub)]
pub struct C {}
}

#[allow(unreachable_pub)]
pub use self::b::C;
}

fn main() {}
4 changes: 2 additions & 2 deletions tests/ui/useless_attribute.stderr
@@ -1,13 +1,13 @@
error: useless lint attribute
--> $DIR/useless_attribute.rs:5:1
--> $DIR/useless_attribute.rs:6:1
|
LL | #[allow(dead_code)]
| ^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(dead_code)]`
|
= note: `-D clippy::useless-attribute` implied by `-D warnings`

error: useless lint attribute
--> $DIR/useless_attribute.rs:6:1
--> $DIR/useless_attribute.rs:7:1
|
LL | #[cfg_attr(feature = "cargo-clippy", allow(dead_code))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code)`
Expand Down

0 comments on commit 40fc725

Please sign in to comment.