Skip to content

Commit 287a4f8

Browse files
nahuakangY-Nak
authored andcommitted
Refactor empty loop to its own module
1 parent d0b657c commit 287a4f8

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

clippy_lints/src/loops/empty_loop.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use super::EMPTY_LOOP;
2+
use crate::utils::{is_in_panic_handler, is_no_std_crate, span_lint_and_help};
3+
4+
use rustc_hir::{Block, Expr};
5+
use rustc_lint::LateContext;
6+
7+
pub(super) fn check_empty_loop(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
8+
if loop_block.stmts.is_empty() && loop_block.expr.is_none() && !is_in_panic_handler(cx, expr) {
9+
let msg = "empty `loop {}` wastes CPU cycles";
10+
let help = if is_no_std_crate(cx.tcx.hir().krate()) {
11+
"you should either use `panic!()` or add a call pausing or sleeping the thread to the loop body"
12+
} else {
13+
"you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body"
14+
};
15+
span_lint_and_help(cx, EMPTY_LOOP, expr.span, msg, None, help);
16+
}
17+
}

clippy_lints/src/loops/mod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod empty_loop;
12
mod explicit_counter_loop;
23
mod for_loop_arg;
34
mod for_loop_over_map_kv;
@@ -14,10 +15,7 @@ mod utils;
1415
mod while_let_on_iterator;
1516

1617
use crate::utils::sugg::Sugg;
17-
use crate::utils::{
18-
higher, is_in_panic_handler, is_no_std_crate, snippet_with_applicability, span_lint_and_help, span_lint_and_sugg,
19-
sugg,
20-
};
18+
use crate::utils::{higher, snippet_with_applicability, span_lint_and_sugg, sugg};
2119
use rustc_errors::Applicability;
2220
use rustc_hir::{Block, Expr, ExprKind, LoopSource, MatchSource, Pat, StmtKind};
2321
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -565,15 +563,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
565563
// (even if the "match" or "if let" is used for declaration)
566564
if let ExprKind::Loop(ref block, _, LoopSource::Loop, _) = expr.kind {
567565
// also check for empty `loop {}` statements, skipping those in #[panic_handler]
568-
if block.stmts.is_empty() && block.expr.is_none() && !is_in_panic_handler(cx, expr) {
569-
let msg = "empty `loop {}` wastes CPU cycles";
570-
let help = if is_no_std_crate(cx.tcx.hir().krate()) {
571-
"you should either use `panic!()` or add a call pausing or sleeping the thread to the loop body"
572-
} else {
573-
"you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body"
574-
};
575-
span_lint_and_help(cx, EMPTY_LOOP, expr.span, msg, None, help);
576-
}
566+
empty_loop::check_empty_loop(cx, expr, block);
577567

578568
// extract the expression from the first statement (if any) in a block
579569
let inner_stmt_expr = extract_expr_from_first_stmt(block);

0 commit comments

Comments
 (0)