From 2d57902a27d09f327670e9ae3001e1d27aba5352 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 18 Aug 2016 11:08:35 +0200 Subject: [PATCH 1/3] don't lint if snippet is shortert than 1 character This happens with various combinations of cfg attributes and macros expansion. Not linting here is the safe route, as everything else might produce false positives. --- clippy_lints/src/attrs.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 014ace0fb..4bb32ec4d 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -109,14 +109,16 @@ impl LateLintPass for AttrPass { if let MetaItemKind::List(ref name, _) = attr.node.value.node { match &**name { "allow" | "warn" | "deny" | "forbid" => { - span_lint_and_then(cx, USELESS_ATTRIBUTE, attr.span, - "useless lint attribute", - |db| { - if let Some(mut sugg) = snippet_opt(cx, attr.span) { - sugg.insert(1, '!'); - db.span_suggestion(attr.span, "if you just forgot a `!`, use", sugg); + if let Some(mut sugg) = snippet_opt(cx, attr.span) { + if sugg.len() > 1 { + span_lint_and_then(cx, USELESS_ATTRIBUTE, attr.span, + "useless lint attribute", + |db| { + sugg.insert(1, '!'); + db.span_suggestion(attr.span, "if you just forgot a `!`, use", sugg); + }); } - }); + } }, _ => {}, } From 14d2cd0b0af2490415d776694f10f581bed5a065 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 18 Aug 2016 13:07:47 +0200 Subject: [PATCH 2/3] `unused_import` is a valid lint to be changed on `use` statements --- clippy_lints/src/attrs.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 4bb32ec4d..086716c1f 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -106,9 +106,19 @@ impl LateLintPass for AttrPass { ItemExternCrate(_) | ItemUse(_) => { for attr in &item.attrs { - if let MetaItemKind::List(ref name, _) = attr.node.value.node { + if let MetaItemKind::List(ref name, ref lint_list) = attr.node.value.node { match &**name { "allow" | "warn" | "deny" | "forbid" => { + // whitelist `unused_imports` + for lint in lint_list { + if let MetaItemKind::Word(ref word) = lint.node { + if word == "unused_imports" { + if let ItemUse(_) = item.node { + return; + } + } + } + } if let Some(mut sugg) = snippet_opt(cx, attr.span) { if sugg.len() > 1 { span_lint_and_then(cx, USELESS_ATTRIBUTE, attr.span, From 6dd003ac3913c19871d61c19d7a7ddbc30765ae6 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 19 Aug 2016 17:31:14 +0200 Subject: [PATCH 3/3] add regression test --- tests/compile-fail/useless_attribute.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/compile-fail/useless_attribute.rs b/tests/compile-fail/useless_attribute.rs index 645d7c154..1826f3a20 100644 --- a/tests/compile-fail/useless_attribute.rs +++ b/tests/compile-fail/useless_attribute.rs @@ -7,4 +7,8 @@ //~| SUGGESTION #![allow(dead_code)] extern crate clippy_lints; +// don't lint on unused_import for `use` items +#[allow(unused_imports)] +use std::collections; + fn main() {}