mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-01 11:13:43 +00:00
eliminate the second visit
to save a bit performance.
Thanks @y21 for the hint!
This commit is contained in:
parent
3e9a6d142e
commit
0d26f9183b
@ -34,27 +34,15 @@ pub(super) fn check<'tcx>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// First, find any `break` or `return` without entering any inner loop,
|
let mut loop_visitor = LoopVisitor {
|
||||||
// then, find `return` or labeled `break` which breaks this loop with entering inner loop,
|
|
||||||
// otherwise this loop is a infinite loop.
|
|
||||||
let mut direct_visitor = LoopVisitor {
|
|
||||||
cx,
|
cx,
|
||||||
label,
|
label,
|
||||||
is_finite: false,
|
is_finite: false,
|
||||||
enter_nested_loop: false,
|
loop_depth: 0,
|
||||||
};
|
};
|
||||||
direct_visitor.visit_block(loop_block);
|
loop_visitor.visit_block(loop_block);
|
||||||
|
|
||||||
let is_finite_loop = direct_visitor.is_finite || {
|
let is_finite_loop = loop_visitor.is_finite;
|
||||||
let mut inner_loop_visitor = LoopVisitor {
|
|
||||||
cx,
|
|
||||||
label,
|
|
||||||
is_finite: false,
|
|
||||||
enter_nested_loop: true,
|
|
||||||
};
|
|
||||||
inner_loop_visitor.visit_block(loop_block);
|
|
||||||
inner_loop_visitor.is_finite
|
|
||||||
};
|
|
||||||
|
|
||||||
if !is_finite_loop {
|
if !is_finite_loop {
|
||||||
span_lint_and_then(cx, INFINITE_LOOPS, expr.span, "infinite loop detected", |diag| {
|
span_lint_and_then(cx, INFINITE_LOOPS, expr.span, "infinite loop detected", |diag| {
|
||||||
@ -103,26 +91,27 @@ fn get_parent_fn_ret_ty<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option
|
|||||||
struct LoopVisitor<'hir, 'tcx> {
|
struct LoopVisitor<'hir, 'tcx> {
|
||||||
cx: &'hir LateContext<'tcx>,
|
cx: &'hir LateContext<'tcx>,
|
||||||
label: Option<Label>,
|
label: Option<Label>,
|
||||||
|
loop_depth: usize,
|
||||||
is_finite: bool,
|
is_finite: bool,
|
||||||
enter_nested_loop: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'hir> Visitor<'hir> for LoopVisitor<'hir, '_> {
|
impl<'hir> Visitor<'hir> for LoopVisitor<'hir, '_> {
|
||||||
fn visit_expr(&mut self, ex: &'hir Expr<'_>) {
|
fn visit_expr(&mut self, ex: &'hir Expr<'_>) {
|
||||||
match &ex.kind {
|
match &ex.kind {
|
||||||
ExprKind::Break(hir::Destination { label, .. }, ..) => {
|
ExprKind::Break(hir::Destination { label, .. }, ..) => {
|
||||||
// When entering nested loop, only by breaking this loop's label
|
// Assuming breaks the loop when `loop_depth` is 0,
|
||||||
// would be considered as exiting this loop.
|
// as it could only means this `break` breaks current loop or any of its upper loop.
|
||||||
if self.enter_nested_loop {
|
// Or, the depth is not zero but the label is matched.
|
||||||
if label.is_some() && *label == self.label {
|
if self.loop_depth == 0 || (label.is_some() && *label == self.label) {
|
||||||
self.is_finite = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.is_finite = true;
|
self.is_finite = true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ExprKind::Ret(..) => self.is_finite = true,
|
ExprKind::Ret(..) => self.is_finite = true,
|
||||||
ExprKind::Loop(..) if !self.enter_nested_loop => (),
|
ExprKind::Loop(..) => {
|
||||||
|
self.loop_depth += 1;
|
||||||
|
walk_expr(self, ex);
|
||||||
|
self.loop_depth = self.loop_depth.saturating_sub(1);
|
||||||
|
},
|
||||||
_ => {
|
_ => {
|
||||||
// Calls to a function that never return
|
// Calls to a function that never return
|
||||||
if let Some(did) = fn_def_id(self.cx, ex) {
|
if let Some(did) = fn_def_id(self.cx, ex) {
|
||||||
|
Loading…
Reference in New Issue
Block a user