Skip to content

Commit fd62c18

Browse files
committed
Auto merge of #6162 - josephlr:empty-loop-no-std, r=flip1995
Update empty_loop documentation/message. Originally part of #6161, but now this PR only deals with `std` crates This change: - Updates the `std` message . - Updates the docs to mention how the busy loops should be fixed - Gives examples of how to do this for `no_std` targets - Updates the tests/stderr files to test this change. changelog: Update `empty_loop` lint documentation
2 parents 6b01c39 + 3807634 commit fd62c18

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

clippy_lints/src/loops.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,24 @@ declare_clippy_lint! {
294294
declare_clippy_lint! {
295295
/// **What it does:** Checks for empty `loop` expressions.
296296
///
297-
/// **Why is this bad?** Those busy loops burn CPU cycles without doing
298-
/// anything. Think of the environment and either block on something or at least
299-
/// make the thread sleep for some microseconds.
297+
/// **Why is this bad?** These busy loops burn CPU cycles without doing
298+
/// anything. It is _almost always_ a better idea to `panic!` than to have
299+
/// a busy loop.
300+
///
301+
/// If panicking isn't possible, think of the environment and either:
302+
/// - block on something
303+
/// - sleep the thread for some microseconds
304+
/// - yield or pause the thread
305+
///
306+
/// For `std` targets, this can be done with
307+
/// [`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html)
308+
/// or [`std::thread::yield_now`](https://doc.rust-lang.org/std/thread/fn.yield_now.html).
309+
///
310+
/// For `no_std` targets, doing this is more complicated, especially because
311+
/// `#[panic_handler]`s can't panic. To stop/pause the thread, you will
312+
/// probably need to invoke some target-specific intrinsic. Examples include:
313+
/// - [`x86_64::instructions::hlt`](https://docs.rs/x86_64/0.12.2/x86_64/instructions/fn.hlt.html)
314+
/// - [`cortex_m::asm::wfi`](https://docs.rs/cortex-m/0.6.3/cortex_m/asm/fn.wfi.html)
300315
///
301316
/// **Known problems:** None.
302317
///
@@ -529,13 +544,15 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
529544
// (even if the "match" or "if let" is used for declaration)
530545
if let ExprKind::Loop(ref block, _, LoopSource::Loop) = expr.kind {
531546
// also check for empty `loop {}` statements
547+
// TODO(issue #6161): Enable for no_std crates (outside of #[panic_handler])
532548
if block.stmts.is_empty() && block.expr.is_none() && !is_no_std_crate(cx.tcx.hir().krate()) {
533-
span_lint(
549+
span_lint_and_help(
534550
cx,
535551
EMPTY_LOOP,
536552
expr.span,
537-
"empty `loop {}` detected. You may want to either use `panic!()` or add \
538-
`std::thread::sleep(..);` to the loop body.",
553+
"empty `loop {}` wastes CPU cycles",
554+
None,
555+
"You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.",
539556
);
540557
}
541558

tests/ui/crashes/ice-360.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ LL | | }
1212
|
1313
= note: `-D clippy::while-let-loop` implied by `-D warnings`
1414

15-
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
15+
error: empty `loop {}` wastes CPU cycles
1616
--> $DIR/ice-360.rs:10:9
1717
|
1818
LL | loop {}
1919
| ^^^^^^^
2020
|
2121
= note: `-D clippy::empty-loop` implied by `-D warnings`
22+
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
2223

2324
error: aborting due to 2 previous errors
2425

tests/ui/empty_loop.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
1+
error: empty `loop {}` wastes CPU cycles
22
--> $DIR/empty_loop.rs:9:5
33
|
44
LL | loop {}
55
| ^^^^^^^
66
|
77
= note: `-D clippy::empty-loop` implied by `-D warnings`
8+
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
89

9-
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
10+
error: empty `loop {}` wastes CPU cycles
1011
--> $DIR/empty_loop.rs:11:9
1112
|
1213
LL | loop {}
1314
| ^^^^^^^
15+
|
16+
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
1417

15-
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
18+
error: empty `loop {}` wastes CPU cycles
1619
--> $DIR/empty_loop.rs:15:9
1720
|
1821
LL | 'inner: loop {}
1922
| ^^^^^^^^^^^^^^^
23+
|
24+
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
2025

2126
error: aborting due to 3 previous errors
2227

File renamed without changes.

0 commit comments

Comments
 (0)