Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions clippy_lints/src/loops/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clippy_utils::source::snippet;
use clippy_utils::visitors::{Descend, for_each_expr_without_closures};
use rustc_errors::Applicability;
use rustc_hir::{
Block, Destination, Expr, ExprKind, HirId, InlineAsmOperand, Node, Pat, Stmt, StmtKind, StructTailExpr,
Block, Destination, Expr, ExprKind, HirId, InlineAsm, InlineAsmOperand, Node, Pat, Stmt, StmtKind, StructTailExpr,
};
use rustc_lint::LateContext;
use rustc_span::{BytePos, Span, sym};
Expand Down Expand Up @@ -75,12 +75,19 @@ pub(super) fn check<'tcx>(
fn contains_any_break_or_continue(block: &Block<'_>) -> bool {
for_each_expr_without_closures(block, |e| match e.kind {
ExprKind::Break(..) | ExprKind::Continue(..) => ControlFlow::Break(()),
ExprKind::InlineAsm(asm) if contains_label(asm) => ControlFlow::Break(()),
ExprKind::Loop(..) => ControlFlow::Continue(Descend::No),
_ => ControlFlow::Continue(Descend::Yes),
})
.is_some()
}

fn contains_label(asm: &InlineAsm<'_>) -> bool {
asm.operands
.iter()
.any(|(op, _span)| matches!(op, InlineAsmOperand::Label { .. }))
}

/// The `never_loop` analysis keeps track of three things:
///
/// * Has any (reachable) code path hit a `continue` of the main loop?
Expand Down Expand Up @@ -378,7 +385,15 @@ fn never_loop_expr<'tcx>(
InlineAsmOperand::Const { .. } | InlineAsmOperand::SymFn { .. } | InlineAsmOperand::SymStatic { .. } => {
NeverLoopResult::Normal
},
InlineAsmOperand::Label { block } => never_loop_block(cx, block, local_labels, main_loop_id),
InlineAsmOperand::Label { block } =>
// We do not know whether the label will be executed or not, so `Diverging` must be
// downgraded to `Normal`.
{
match never_loop_block(cx, block, local_labels, main_loop_id) {
NeverLoopResult::Diverging { .. } => NeverLoopResult::Normal,
result => result,
}
},
})),
ExprKind::OffsetOf(_, _)
| ExprKind::Yield(_, _)
Expand Down
41 changes: 34 additions & 7 deletions tests/ui/never_loop.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#![feature(try_blocks)]
#![allow(
clippy::eq_op,
clippy::single_match,
unused_assignments,
unused_variables,
clippy::while_immutable_condition
)]
#![expect(clippy::eq_op, clippy::single_match, clippy::while_immutable_condition)]
//@no-rustfix

use std::arch::asm;

fn test1() {
let mut x = 0;
loop {
Expand Down Expand Up @@ -522,3 +519,33 @@ fn issue15350() {
}
}
}

fn issue15673() {
loop {
unsafe {
// No lint since we don't analyze the inside of the asm
asm! {
"/* {} */",
label {
break;
}
}
}
}

//~v never_loop
loop {
// We don't check the applicability in tests, but this should
// be `Applicability::MaybeIncorrect` because of the `break`
// in the `asm!()` labels.
unsafe {
asm! {
"/* {} */",
label {
break;
}
}
}
return;
}
}
Loading