Fix `get_enclosing_block`

This commit is contained in:
HMPerson1 2018-01-30 14:52:45 -05:00
parent d5bac82837
commit 3a4ea45821
No known key found for this signature in database
GPG Key ID: 1FB477DDD27821CE
3 changed files with 41 additions and 0 deletions

View File

@ -518,6 +518,9 @@ pub fn get_enclosing_block<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, node: NodeI
Node::NodeItem(&Item {
node: ItemFn(_, _, _, _, _, eid),
..
}) | Node::NodeImplItem(&ImplItem {
node: ImplItemKind::Method(_, eid),
..
}) => match cx.tcx.hir.body(eid).value.node {
ExprBlock(ref block) => Some(block),
_ => None,

24
tests/ui/issue_2356.rs Normal file
View File

@ -0,0 +1,24 @@
#![deny(while_let_on_iterator)]
use std::iter::Iterator;
struct Foo;
impl Foo {
fn foo1<I: Iterator<Item = usize>>(mut it: I) {
while let Some(_) = it.next() {
println!("{:?}", it.size_hint());
}
}
fn foo2<I: Iterator<Item = usize>>(mut it: I) {
while let Some(e) = it.next() {
println!("{:?}", e);
}
}
}
fn main() {
Foo::foo1(vec![].into_iter());
Foo::foo2(vec![].into_iter());
}

View File

@ -0,0 +1,14 @@
error: this loop could be written as a `for` loop
--> $DIR/issue_2356.rs:15:29
|
15 | while let Some(e) = it.next() {
| ^^^^^^^^^ help: try: `for e in it { .. }`
|
note: lint level defined here
--> $DIR/issue_2356.rs:1:9
|
1 | #![deny(while_let_on_iterator)]
| ^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error