Skip to content

Commit 7fddc61

Browse files
authored
Merge pull request #2415 from HMPerson1/fix-2356
Fix `ImplItem`s being ignored
2 parents 02ee625 + 10d2fed commit 7fddc61

9 files changed

+146
-17
lines changed

clippy_lints/src/const_static_lifetime.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use syntax::ast::{Item, ItemKind, Ty, TyKind};
1+
use syntax::ast::*;
22
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
33
use utils::{in_macro, snippet, span_lint_and_then};
44

@@ -86,4 +86,22 @@ impl EarlyLintPass for StaticConst {
8686
}
8787
}
8888
}
89+
90+
fn check_trait_item(&mut self, cx: &EarlyContext, item: &TraitItem) {
91+
if !in_macro(item.span) {
92+
// Match only constants...
93+
if let TraitItemKind::Const(ref var_type, _) = item.node {
94+
self.visit_type(var_type, cx);
95+
}
96+
}
97+
}
98+
99+
fn check_impl_item(&mut self, cx: &EarlyContext, item: &ImplItem) {
100+
if !in_macro(item.span) {
101+
// Match only constants...
102+
if let ImplItemKind::Const(ref var_type, _) = item.node {
103+
self.visit_type(var_type, cx);
104+
}
105+
}
106+
}
89107
}

clippy_lints/src/non_expressive_names.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,32 @@ impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
313313
impl EarlyLintPass for NonExpressiveNames {
314314
fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
315315
if let ItemKind::Fn(ref decl, _, _, _, _, ref blk) = item.node {
316-
if !attr::contains_name(&item.attrs, "test") {
317-
let mut visitor = SimilarNamesLocalVisitor {
318-
names: Vec::new(),
319-
cx: cx,
320-
lint: self,
321-
single_char_names: Vec::new(),
322-
};
323-
// initialize with function arguments
324-
for arg in &decl.inputs {
325-
SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat);
326-
}
327-
// walk all other bindings
328-
walk_block(&mut visitor, blk);
329-
}
316+
do_check(self, cx, &item.attrs, decl, blk);
317+
}
318+
}
319+
320+
fn check_impl_item(&mut self, cx: &EarlyContext, item: &ImplItem) {
321+
if let ImplItemKind::Method(ref sig, ref blk) = item.node {
322+
do_check(self, cx, &item.attrs, &sig.decl, blk);
323+
}
324+
}
325+
326+
}
327+
328+
fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext, attrs: &[Attribute], decl: &FnDecl, blk: &Block) {
329+
if !attr::contains_name(attrs, "test") {
330+
let mut visitor = SimilarNamesLocalVisitor {
331+
names: Vec::new(),
332+
cx: cx,
333+
lint: lint,
334+
single_char_names: Vec::new(),
335+
};
336+
// initialize with function arguments
337+
for arg in &decl.inputs {
338+
SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat);
330339
}
340+
// walk all other bindings
341+
walk_block(&mut visitor, blk);
331342
}
332343
}
333344

clippy_lints/src/utils/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ pub fn get_enclosing_block<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, node: NodeI
518518
Node::NodeItem(&Item {
519519
node: ItemFn(_, _, _, _, _, eid),
520520
..
521+
}) | Node::NodeImplItem(&ImplItem {
522+
node: ImplItemKind::Method(_, eid),
523+
..
521524
}) => match cx.tcx.hir.body(eid).value.node {
522525
ExprBlock(ref block) => Some(block),
523526
_ => None,

tests/ui/const_static_lifetime.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ fn main() {
3535
println!("{:?}", VAR_HEIGHT);
3636
println!("{}", false_positive);
3737
}
38+
39+
trait Bar {
40+
const TRAIT_VAR: &'static str;
41+
}
42+
43+
impl Foo {
44+
const IMPL_VAR: &'static str = "var";
45+
}
46+
47+
impl Bar for Foo {
48+
const TRAIT_VAR: &'static str = "foo";
49+
}

tests/ui/const_static_lifetime.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,23 @@ error: Constants have by default a `'static` lifetime
7878
24 | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static.
7979
| -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]`
8080

81-
error: aborting due to 13 previous errors
81+
error: Constants have by default a `'static` lifetime
82+
--> $DIR/const_static_lifetime.rs:40:23
83+
|
84+
40 | const TRAIT_VAR: &'static str;
85+
| -^^^^^^^---- help: consider removing `'static`: `&str`
86+
87+
error: Constants have by default a `'static` lifetime
88+
--> $DIR/const_static_lifetime.rs:44:22
89+
|
90+
44 | const IMPL_VAR: &'static str = "var";
91+
| -^^^^^^^---- help: consider removing `'static`: `&str`
92+
93+
error: Constants have by default a `'static` lifetime
94+
--> $DIR/const_static_lifetime.rs:48:23
95+
|
96+
48 | const TRAIT_VAR: &'static str = "foo";
97+
| -^^^^^^^---- help: consider removing `'static`: `&str`
98+
99+
error: aborting due to 16 previous errors
82100

tests/ui/issue_2356.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![deny(while_let_on_iterator)]
2+
3+
use std::iter::Iterator;
4+
5+
struct Foo;
6+
7+
impl Foo {
8+
fn foo1<I: Iterator<Item = usize>>(mut it: I) {
9+
while let Some(_) = it.next() {
10+
println!("{:?}", it.size_hint());
11+
}
12+
}
13+
14+
fn foo2<I: Iterator<Item = usize>>(mut it: I) {
15+
while let Some(e) = it.next() {
16+
println!("{:?}", e);
17+
}
18+
}
19+
}
20+
21+
fn main() {
22+
Foo::foo1(vec![].into_iter());
23+
Foo::foo2(vec![].into_iter());
24+
}

tests/ui/issue_2356.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: this loop could be written as a `for` loop
2+
--> $DIR/issue_2356.rs:15:29
3+
|
4+
15 | while let Some(e) = it.next() {
5+
| ^^^^^^^^^ help: try: `for e in it { .. }`
6+
|
7+
note: lint level defined here
8+
--> $DIR/issue_2356.rs:1:9
9+
|
10+
1 | #![deny(while_let_on_iterator)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

tests/ui/non_expressive_names.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,14 @@ fn underscores_and_numbers() {
141141
let __1___2 = 12; //~ERROR Consider a more descriptive name
142142
let _1_ok= 1;
143143
}
144+
145+
struct Bar;
146+
147+
impl Bar {
148+
fn bar() {
149+
let _1 = 1;
150+
let ____1 = 1;
151+
let __1___2 = 12;
152+
let _1_ok= 1;
153+
}
154+
}

tests/ui/non_expressive_names.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,23 @@ error: consider choosing a more descriptive name
149149
141 | let __1___2 = 12; //~ERROR Consider a more descriptive name
150150
| ^^^^^^^
151151

152-
error: aborting due to 14 previous errors
152+
error: consider choosing a more descriptive name
153+
--> $DIR/non_expressive_names.rs:149:13
154+
|
155+
149 | let _1 = 1;
156+
| ^^
157+
158+
error: consider choosing a more descriptive name
159+
--> $DIR/non_expressive_names.rs:150:13
160+
|
161+
150 | let ____1 = 1;
162+
| ^^^^^
163+
164+
error: consider choosing a more descriptive name
165+
--> $DIR/non_expressive_names.rs:151:13
166+
|
167+
151 | let __1___2 = 12;
168+
| ^^^^^^^
169+
170+
error: aborting due to 17 previous errors
153171

0 commit comments

Comments
 (0)