2021-02-21 15:48:16 +00:00
|
|
|
use super::EMPTY_LOOP;
|
2021-03-16 00:55:45 +00:00
|
|
|
use crate::utils::{is_in_panic_handler, is_no_std_crate};
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-02-21 15:48:16 +00:00
|
|
|
|
|
|
|
use rustc_hir::{Block, Expr};
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
|
2021-03-02 02:49:14 +00:00
|
|
|
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
|
2021-02-21 15:48:16 +00:00
|
|
|
if loop_block.stmts.is_empty() && loop_block.expr.is_none() && !is_in_panic_handler(cx, expr) {
|
|
|
|
let msg = "empty `loop {}` wastes CPU cycles";
|
2021-03-11 09:37:58 +00:00
|
|
|
let help = if is_no_std_crate(cx) {
|
2021-02-21 15:48:16 +00:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
}
|