Skip to content

Commit e9b7a71

Browse files
committed
Auto merge of #4085 - phansch:empty_loop_tests, r=matthiaskrgr
Add tests for empty_loop lint changelog: none Closes #4072
2 parents feb18c3 + 9a24ab8 commit e9b7a71

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

tests/ui/auxiliary/macro_rules.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(dead_code)]
2+
3+
/// Used to test that certain lints don't trigger in imported external macros
4+
#[macro_export]
5+
macro_rules! foofoo {
6+
() => {
7+
loop {}
8+
};
9+
}

tests/ui/empty_loop.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// aux-build:macro_rules.rs
2+
3+
#![warn(clippy::empty_loop)]
4+
#![allow(clippy::unused_label)]
5+
6+
#[macro_use]
7+
extern crate macro_rules;
8+
9+
fn should_trigger() {
10+
loop {}
11+
loop {
12+
loop {}
13+
}
14+
15+
'outer: loop {
16+
'inner: loop {}
17+
}
18+
}
19+
20+
fn should_not_trigger() {
21+
loop {
22+
panic!("This is fine")
23+
}
24+
let ten_millis = std::time::Duration::from_millis(10);
25+
loop {
26+
std::thread::sleep(ten_millis)
27+
}
28+
29+
#[allow(clippy::never_loop)]
30+
'outer: loop {
31+
'inner: loop {
32+
break 'inner;
33+
}
34+
break 'outer;
35+
}
36+
37+
// Make sure `allow` works for this lint
38+
#[allow(clippy::empty_loop)]
39+
loop {}
40+
41+
// We don't lint loops inside macros
42+
macro_rules! foo {
43+
() => {
44+
loop {}
45+
};
46+
}
47+
48+
// We don't lint external macros
49+
foofoo!()
50+
}
51+
52+
fn main() {}

tests/ui/empty_loop.stderr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
2+
--> $DIR/empty_loop.rs:10:5
3+
|
4+
LL | loop {}
5+
| ^^^^^^^
6+
|
7+
= note: `-D clippy::empty-loop` implied by `-D warnings`
8+
9+
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
10+
--> $DIR/empty_loop.rs:12:9
11+
|
12+
LL | loop {}
13+
| ^^^^^^^
14+
15+
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
16+
--> $DIR/empty_loop.rs:16:9
17+
|
18+
LL | 'inner: loop {}
19+
| ^^^^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)