From 3a4ea45821f3f8c5cb75425e21985279790d4ff1 Mon Sep 17 00:00:00 2001 From: HMPerson1 Date: Tue, 30 Jan 2018 14:52:45 -0500 Subject: [PATCH] Fix `get_enclosing_block` --- clippy_lints/src/utils/mod.rs | 3 +++ tests/ui/issue_2356.rs | 24 ++++++++++++++++++++++++ tests/ui/issue_2356.stderr | 14 ++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 tests/ui/issue_2356.rs create mode 100644 tests/ui/issue_2356.stderr diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index b7f1e7b2454..53ea5ab4a79 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -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, diff --git a/tests/ui/issue_2356.rs b/tests/ui/issue_2356.rs new file mode 100644 index 00000000000..d4cefb0f1e3 --- /dev/null +++ b/tests/ui/issue_2356.rs @@ -0,0 +1,24 @@ +#![deny(while_let_on_iterator)] + +use std::iter::Iterator; + +struct Foo; + +impl Foo { + fn foo1>(mut it: I) { + while let Some(_) = it.next() { + println!("{:?}", it.size_hint()); + } + } + + fn foo2>(mut it: I) { + while let Some(e) = it.next() { + println!("{:?}", e); + } + } +} + +fn main() { + Foo::foo1(vec![].into_iter()); + Foo::foo2(vec![].into_iter()); +} diff --git a/tests/ui/issue_2356.stderr b/tests/ui/issue_2356.stderr new file mode 100644 index 00000000000..4b82a0a7565 --- /dev/null +++ b/tests/ui/issue_2356.stderr @@ -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 +