diff options
| author | nahuakang <kangnahua@gmail.com> | 2021-02-21 16:48:16 +0100 |
|---|---|---|
| committer | Yoshitomo Nakanishi <yurayura.rounin.3@gmail.com> | 2021-03-02 18:14:20 +0900 |
| commit | 287a4f8ab176a95cff2b82f6414d37e33809110a (patch) | |
| tree | 2a890939107eb4370deb75d9149732a46ab62c9f | |
| parent | d0b657c0b7335ccb4a594af100ab49d8bd660e97 (diff) | |
| download | rust-287a4f8ab176a95cff2b82f6414d37e33809110a.tar.gz rust-287a4f8ab176a95cff2b82f6414d37e33809110a.zip | |
Refactor empty loop to its own module
| -rw-r--r-- | clippy_lints/src/loops/empty_loop.rs | 17 | ||||
| -rw-r--r-- | clippy_lints/src/loops/mod.rs | 16 |
2 files changed, 20 insertions, 13 deletions
diff --git a/clippy_lints/src/loops/empty_loop.rs b/clippy_lints/src/loops/empty_loop.rs new file mode 100644 index 00000000000..fdc99e190e3 --- /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_empty_loop(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.tcx.hir().krate()) { + "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); + } +} diff --git a/clippy_lints/src/loops/mod.rs b/clippy_lints/src/loops/mod.rs index cadf9241256..d205f053679 100644 --- a/clippy_lints/src/loops/mod.rs +++ b/clippy_lints/src/loops/mod.rs @@ -1,3 +1,4 @@ +mod empty_loop; mod explicit_counter_loop; mod for_loop_arg; mod for_loop_over_map_kv; @@ -14,10 +15,7 @@ mod utils; mod while_let_on_iterator; use crate::utils::sugg::Sugg; -use crate::utils::{ - higher, is_in_panic_handler, is_no_std_crate, snippet_with_applicability, span_lint_and_help, span_lint_and_sugg, - sugg, -}; +use crate::utils::{higher, snippet_with_applicability, span_lint_and_sugg, sugg}; use rustc_errors::Applicability; use rustc_hir::{Block, Expr, ExprKind, LoopSource, MatchSource, Pat, StmtKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; @@ -565,15 +563,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops { // (even if the "match" or "if let" is used for declaration) if let ExprKind::Loop(ref block, _, LoopSource::Loop, _) = expr.kind { // also check for empty `loop {}` statements, skipping those in #[panic_handler] - if block.stmts.is_empty() && 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.tcx.hir().krate()) { - "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); - } + empty_loop::check_empty_loop(cx, expr, block); // extract the expression from the first statement (if any) in a block let inner_stmt_expr = extract_expr_from_first_stmt(block); |
