Skip to content

Commit abfa331

Browse files
committed
Auto merge of #6205 - josephlr:empty-loop2, r=flip1995
Enable empty_loop lint for no_std crates Depends on #6162. Fixes #6161 We skip the lint if the `loop {}` is in the `#[panic_handler]` as the main recommendation we give is to panic, which obviously isn't possible in a panic handler. changelog: Enable empty_loop lint for no_std crates
2 parents 96d5f45 + 00dee9d commit abfa331

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

clippy_lints/src/loops.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::utils::sugg::Sugg;
44
use crate::utils::usage::{is_unused, mutated_variables};
55
use 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
};
1212
use if_chain::if_chain;
1313
use 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

clippy_lints/src/utils/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,13 @@ pub fn is_entrypoint_fn(cx: &LateContext<'_>, def_id: DefId) -> bool {
468468
.map_or(false, |(entry_fn_def_id, _)| def_id == entry_fn_def_id.to_def_id())
469469
}
470470

471+
/// Returns `true` if the expression is in the program's `#[panic_handler]`.
472+
pub fn is_in_panic_handler(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
473+
let parent = cx.tcx.hir().get_parent_item(e.hir_id);
474+
let def_id = cx.tcx.hir().local_def_id(parent).to_def_id();
475+
Some(def_id) == cx.tcx.lang_items().panic_impl()
476+
}
477+
471478
/// Gets the name of the item the expression is in, if available.
472479
pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {
473480
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id);

tests/ui/crashes/ice-360.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | loop {}
1919
| ^^^^^^^
2020
|
2121
= note: `-D clippy::empty-loop` implied by `-D warnings`
22-
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
22+
= help: you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body
2323

2424
error: aborting due to 2 previous errors
2525

tests/ui/empty_loop.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ LL | loop {}
55
| ^^^^^^^
66
|
77
= note: `-D clippy::empty-loop` implied by `-D warnings`
8-
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
8+
= help: you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body
99

1010
error: empty `loop {}` wastes CPU cycles
1111
--> $DIR/empty_loop.rs:11:9
1212
|
1313
LL | loop {}
1414
| ^^^^^^^
1515
|
16-
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
16+
= help: you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body
1717

1818
error: empty `loop {}` wastes CPU cycles
1919
--> $DIR/empty_loop.rs:15:9
2020
|
2121
LL | 'inner: loop {}
2222
| ^^^^^^^^^^^^^^^
2323
|
24-
= help: You should either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
24+
= help: you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body
2525

2626
error: aborting due to 3 previous errors
2727

tests/ui/empty_loop_no_std.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ use core::panic::PanicInfo;
1010

1111
#[start]
1212
fn main(argc: isize, argv: *const *const u8) -> isize {
13+
// This should trigger the lint
1314
loop {}
1415
}
1516

1617
#[panic_handler]
1718
fn panic(_info: &PanicInfo) -> ! {
19+
// This should NOT trigger the lint
1820
loop {}
1921
}
2022

2123
#[lang = "eh_personality"]
22-
extern "C" fn eh_personality() {}
24+
extern "C" fn eh_personality() {
25+
// This should also trigger the lint
26+
loop {}
27+
}

tests/ui/empty_loop_no_std.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: empty `loop {}` wastes CPU cycles
2+
--> $DIR/empty_loop_no_std.rs:14:5
3+
|
4+
LL | loop {}
5+
| ^^^^^^^
6+
|
7+
= note: `-D clippy::empty-loop` implied by `-D warnings`
8+
= help: you should either use `panic!()` or add a call pausing or sleeping the thread to the loop body
9+
10+
error: empty `loop {}` wastes CPU cycles
11+
--> $DIR/empty_loop_no_std.rs:26:5
12+
|
13+
LL | loop {}
14+
| ^^^^^^^
15+
|
16+
= help: you should either use `panic!()` or add a call pausing or sleeping the thread to the loop body
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)