From f2f2a005b4efd3e44ac6a02ea2b9660d28401679 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 12 Mar 2021 15:30:50 +0100 Subject: Merge commit '6ed6f1e6a1a8f414ba7e6d9b8222e7e5a1686e42' into clippyup --- clippy_lints/src/loops/empty_loop.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 clippy_lints/src/loops/empty_loop.rs (limited to 'clippy_lints/src/loops/empty_loop.rs') diff --git a/clippy_lints/src/loops/empty_loop.rs b/clippy_lints/src/loops/empty_loop.rs new file mode 100644 index 00000000000..43e85538f28 --- /dev/null +++ b/clippy_lints/src/loops/empty_loop.rs @@ -0,0 +1,17 @@ +use super::EMPTY_LOOP; +use crate::utils::{is_in_panic_handler, is_no_std_crate, span_lint_and_help}; + +use rustc_hir::{Block, Expr}; +use rustc_lint::LateContext; + +pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) { + if loop_block.stmts.is_empty() && loop_block.expr.is_none() && !is_in_panic_handler(cx, expr) { + let msg = "empty `loop {}` wastes CPU cycles"; + let help = if is_no_std_crate(cx) { + "you should either use `panic!()` or add a call pausing or sleeping the thread to the loop body" + } else { + "you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body" + }; + span_lint_and_help(cx, EMPTY_LOOP, expr.span, msg, None, help); + } +} -- cgit 1.4.1-3-g733a5