@@ -4,10 +4,10 @@ use crate::utils::sugg::Sugg;
44use crate :: utils:: usage:: { is_unused, mutated_variables} ;
55use crate :: utils:: {
66 contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
7- indent_of, 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, single_segment_path, snippet ,
9- snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg ,
10- span_lint_and_then, sugg, SpanlessEq ,
7+ indent_of, is_in_panic_handler , is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item,
8+ last_path_segment , match_trait_method, match_type, match_var, multispan_sugg, qpath_res, single_segment_path,
9+ snippet , snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help,
10+ span_lint_and_sugg , span_lint_and_then, sugg, SpanlessEq ,
1111} ;
1212use if_chain:: if_chain;
1313use rustc_ast:: ast;
@@ -543,17 +543,15 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
543543 // (also matches an explicit "match" instead of "if let")
544544 // (even if the "match" or "if let" is used for declaration)
545545 if let ExprKind :: Loop ( ref block, _, LoopSource :: Loop ) = expr. kind {
546- // also check for empty `loop {}` statements
547- // TODO(issue #6161): Enable for no_std crates (outside of #[panic_handler])
548- if block. stmts . is_empty ( ) && block. expr . is_none ( ) && !is_no_std_crate ( cx. tcx . hir ( ) . krate ( ) ) {
549- span_lint_and_help (
550- cx,
551- EMPTY_LOOP ,
552- expr. span ,
553- "empty `loop {}` wastes CPU cycles" ,
554- None ,
555- "You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body." ,
556- ) ;
546+ // also check for empty `loop {}` statements, skipping those in #[panic_handler]
547+ if block. stmts . is_empty ( ) && block. expr . is_none ( ) && !is_in_panic_handler ( cx, expr) {
548+ let msg = "empty `loop {}` wastes CPU cycles" ;
549+ let help = if is_no_std_crate ( cx. tcx . hir ( ) . krate ( ) ) {
550+ "You should either use `panic!()` or add a call pausing or sleeping the thread to the loop body."
551+ } else {
552+ "You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body."
553+ } ;
554+ span_lint_and_help ( cx, EMPTY_LOOP , expr. span , msg, None , help) ;
557555 }
558556
559557 // extract the expression from the first statement (if any) in a block
0 commit comments