@@ -4,9 +4,10 @@ use crate::utils::sugg::Sugg;
4
4
use crate :: utils:: usage:: { is_unused, mutated_variables} ;
5
5
use crate :: utils:: {
6
6
contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
7
- is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, last_path_segment, match_trait_method,
8
- match_type, match_var, multispan_sugg, qpath_res, snippet, snippet_with_applicability, snippet_with_macro_callsite,
9
- span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, SpanlessEq ,
7
+ is_in_panic_handler, is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, last_path_segment,
8
+ match_trait_method, match_type, match_var, multispan_sugg, qpath_res, snippet, snippet_with_applicability,
9
+ snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg,
10
+ SpanlessEq ,
10
11
} ;
11
12
use if_chain:: if_chain;
12
13
use rustc_ast:: ast;
@@ -516,17 +517,15 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
516
517
// (also matches an explicit "match" instead of "if let")
517
518
// (even if the "match" or "if let" is used for declaration)
518
519
if let ExprKind :: Loop ( ref block, _, LoopSource :: Loop ) = expr. kind {
519
- // also check for empty `loop {}` statements
520
- // TODO(issue #6161): Enable for no_std crates (outside of #[panic_handler])
521
- if block. stmts . is_empty ( ) && block. expr . is_none ( ) && !is_no_std_crate ( cx. tcx . hir ( ) . krate ( ) ) {
522
- span_lint_and_help (
523
- cx,
524
- EMPTY_LOOP ,
525
- expr. span ,
526
- "empty `loop {}` wastes CPU cycles" ,
527
- None ,
528
- "You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body." ,
529
- ) ;
520
+ // also check for empty `loop {}` statements, skipping those in #[panic_handler]
521
+ if block. stmts . is_empty ( ) && block. expr . is_none ( ) && !is_in_panic_handler ( cx, expr) {
522
+ let msg = "empty `loop {}` wastes CPU cycles" ;
523
+ let help = if is_no_std_crate ( cx. tcx . hir ( ) . krate ( ) ) {
524
+ "You should either use `panic!()` or add a call pausing or sleeping the thread to the loop body."
525
+ } else {
526
+ "You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body."
527
+ } ;
528
+ span_lint_and_help ( cx, EMPTY_LOOP , expr. span , msg, None , help) ;
530
529
}
531
530
532
531
// extract the expression from the first statement (if any) in a block
0 commit comments