@@ -293,9 +293,24 @@ declare_clippy_lint! {
293
293
declare_clippy_lint ! {
294
294
/// **What it does:** Checks for empty `loop` expressions.
295
295
///
296
- /// **Why is this bad?** Those busy loops burn CPU cycles without doing
297
- /// anything. Think of the environment and either block on something or at least
298
- /// make the thread sleep for some microseconds.
296
+ /// **Why is this bad?** These busy loops burn CPU cycles without doing
297
+ /// anything. It is _almost always_ a better idea to `panic!` than to have
298
+ /// a busy loop.
299
+ ///
300
+ /// If panicking isn't possible, think of the environment and either:
301
+ /// - block on something
302
+ /// - sleep the thread for some microseconds
303
+ /// - yield or pause the thread
304
+ ///
305
+ /// For `std` targets, this can be done with
306
+ /// [`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html)
307
+ /// or [`std::thread::yield_now`](https://doc.rust-lang.org/std/thread/fn.yield_now.html).
308
+ ///
309
+ /// For `no_std` targets, doing this is more complicated, especially because
310
+ /// `#[panic_handler]`s can't panic. To stop/pause the thread, you will
311
+ /// probably need to invoke some target-specific intrinsic. Examples include:
312
+ /// - [`x86_64::instructions::hlt`](https://docs.rs/x86_64/0.12.2/x86_64/instructions/fn.hlt.html)
313
+ /// - [`cortex_m::asm::wfi`](https://docs.rs/cortex-m/0.6.3/cortex_m/asm/fn.wfi.html)
299
314
///
300
315
/// **Known problems:** None.
301
316
///
@@ -502,13 +517,15 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
502
517
// (even if the "match" or "if let" is used for declaration)
503
518
if let ExprKind :: Loop ( ref block, _, LoopSource :: Loop ) = expr. kind {
504
519
// also check for empty `loop {}` statements
520
+ // TODO(issue #6161): Enable for no_std crates (outside of #[panic_handler])
505
521
if block. stmts . is_empty ( ) && block. expr . is_none ( ) && !is_no_std_crate ( cx. tcx . hir ( ) . krate ( ) ) {
506
- span_lint (
522
+ span_lint_and_help (
507
523
cx,
508
524
EMPTY_LOOP ,
509
525
expr. span ,
510
- "empty `loop {}` detected. You may want to either use `panic!()` or add \
511
- `std::thread::sleep(..);` to the loop body.",
526
+ "empty `loop {}` wastes CPU cycles" ,
527
+ None ,
528
+ "You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body." ,
512
529
) ;
513
530
}
514
531
0 commit comments