From 9a24ab8466b8e11aa00ae998671e6f41f80edb09 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 12 May 2019 12:10:23 +0200 Subject: [PATCH] Add tests for empty_loop lint --- tests/ui/auxiliary/macro_rules.rs | 9 ++++++ tests/ui/empty_loop.rs | 52 +++++++++++++++++++++++++++++++ tests/ui/empty_loop.stderr | 22 +++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 tests/ui/auxiliary/macro_rules.rs create mode 100644 tests/ui/empty_loop.rs create mode 100644 tests/ui/empty_loop.stderr diff --git a/tests/ui/auxiliary/macro_rules.rs b/tests/ui/auxiliary/macro_rules.rs new file mode 100644 index 00000000000..486e419bee5 --- /dev/null +++ b/tests/ui/auxiliary/macro_rules.rs @@ -0,0 +1,9 @@ +#![allow(dead_code)] + +/// Used to test that certain lints don't trigger in imported external macros +#[macro_export] +macro_rules! foofoo { + () => { + loop {} + }; +} diff --git a/tests/ui/empty_loop.rs b/tests/ui/empty_loop.rs new file mode 100644 index 00000000000..fb9f2cb9cf4 --- /dev/null +++ b/tests/ui/empty_loop.rs @@ -0,0 +1,52 @@ +// aux-build:macro_rules.rs + +#![warn(clippy::empty_loop)] +#![allow(clippy::unused_label)] + +#[macro_use] +extern crate macro_rules; + +fn should_trigger() { + loop {} + loop { + loop {} + } + + 'outer: loop { + 'inner: loop {} + } +} + +fn should_not_trigger() { + loop { + panic!("This is fine") + } + let ten_millis = std::time::Duration::from_millis(10); + loop { + std::thread::sleep(ten_millis) + } + + #[allow(clippy::never_loop)] + 'outer: loop { + 'inner: loop { + break 'inner; + } + break 'outer; + } + + // Make sure `allow` works for this lint + #[allow(clippy::empty_loop)] + loop {} + + // We don't lint loops inside macros + macro_rules! foo { + () => { + loop {} + }; + } + + // We don't lint external macros + foofoo!() +} + +fn main() {} diff --git a/tests/ui/empty_loop.stderr b/tests/ui/empty_loop.stderr new file mode 100644 index 00000000000..41b79900425 --- /dev/null +++ b/tests/ui/empty_loop.stderr @@ -0,0 +1,22 @@ +error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body. + --> $DIR/empty_loop.rs:10:5 + | +LL | loop {} + | ^^^^^^^ + | + = note: `-D clippy::empty-loop` implied by `-D warnings` + +error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body. + --> $DIR/empty_loop.rs:12:9 + | +LL | loop {} + | ^^^^^^^ + +error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body. + --> $DIR/empty_loop.rs:16:9 + | +LL | 'inner: loop {} + | ^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors +