Skip to content

Commit 27255bc

Browse files
authored
Merge pull request #1174 from oli-obk/bugfix
don't lint `unused_attribute` if snippet is shorter than 1 character
2 parents 663d849 + 6dd003a commit 27255bc

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

clippy_lints/src/attrs.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,29 @@ impl LateLintPass for AttrPass {
106106
ItemExternCrate(_) |
107107
ItemUse(_) => {
108108
for attr in &item.attrs {
109-
if let MetaItemKind::List(ref name, _) = attr.node.value.node {
109+
if let MetaItemKind::List(ref name, ref lint_list) = attr.node.value.node {
110110
match &**name {
111111
"allow" | "warn" | "deny" | "forbid" => {
112-
span_lint_and_then(cx, USELESS_ATTRIBUTE, attr.span,
113-
"useless lint attribute",
114-
|db| {
115-
if let Some(mut sugg) = snippet_opt(cx, attr.span) {
116-
sugg.insert(1, '!');
117-
db.span_suggestion(attr.span, "if you just forgot a `!`, use", sugg);
112+
// whitelist `unused_imports`
113+
for lint in lint_list {
114+
if let MetaItemKind::Word(ref word) = lint.node {
115+
if word == "unused_imports" {
116+
if let ItemUse(_) = item.node {
117+
return;
118+
}
119+
}
118120
}
119-
});
121+
}
122+
if let Some(mut sugg) = snippet_opt(cx, attr.span) {
123+
if sugg.len() > 1 {
124+
span_lint_and_then(cx, USELESS_ATTRIBUTE, attr.span,
125+
"useless lint attribute",
126+
|db| {
127+
sugg.insert(1, '!');
128+
db.span_suggestion(attr.span, "if you just forgot a `!`, use", sugg);
129+
});
130+
}
131+
}
120132
},
121133
_ => {},
122134
}

tests/compile-fail/useless_attribute.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
//~| SUGGESTION #![allow(dead_code)]
88
extern crate clippy_lints;
99

10+
// don't lint on unused_import for `use` items
11+
#[allow(unused_imports)]
12+
use std::collections;
13+
1014
fn main() {}

0 commit comments

Comments
 (0)