Skip to content

Commit

Permalink
fix check_infinite_loop by checking for break or return inside loop body
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke committed Nov 13, 2019
1 parent 42f32a0 commit 45dc16f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
39 changes: 36 additions & 3 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2358,17 +2358,50 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, e
return;
};
let mutable_static_in_cond = var_visitor.def_ids.iter().any(|(_, v)| *v);
if no_cond_variable_mutated && !mutable_static_in_cond {

let mut has_break_or_return_visitor = HasBreakOrReturnVisitor {
has_break_or_return: false,
};
walk_expr(&mut has_break_or_return_visitor, expr);
let has_break_or_return = has_break_or_return_visitor.has_break_or_return;

if no_cond_variable_mutated && !mutable_static_in_cond && !has_break_or_return {
span_lint(
cx,
WHILE_IMMUTABLE_CONDITION,
cond.span,
"Variable in the condition are not mutated in the loop body. \
This either leads to an infinite or to a never running loop.",
"variables in the condition are not mutated in the loop body. \
This may lead to an infinite or to a never running loop",
);
}
}

struct HasBreakOrReturnVisitor {
has_break_or_return: bool,
}

impl<'a, 'tcx> Visitor<'tcx> for HasBreakOrReturnVisitor {
fn visit_expr(&mut self, expr: &'tcx Expr) {
if self.has_break_or_return {
return;
}

match expr.kind {
ExprKind::Ret(_) | ExprKind::Break(_, _) => {
self.has_break_or_return = true;
return;
},
_ => {},
}

walk_expr(self, expr);
}

fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::None
}
}

/// Collects the set of variables in an expression
/// Stops analysis if a function call is found
/// Note: In some cases such as `self`, there are no mutable annotation,
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/infinite_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ impl Counter {
}
}

fn while_loop_with_break_and_return() {
let y = 0;
while y < 10 {
if y == 0 {
break;
}
println!("OK - loop contains break");
}

while y < 10 {
if y == 0 {
return;
}
println!("OK - loop contains return");
}
}

fn main() {
immutable_condition();
unused_var();
Expand All @@ -186,4 +203,6 @@ fn main() {
let mut c = Counter { count: 0 };
c.inc_n(5);
c.print_n(2);

while_loop_with_break_and_return();
}
18 changes: 9 additions & 9 deletions tests/ui/infinite_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:23:11
|
LL | while y < 10 {
| ^^^^^^
|
= note: `#[deny(clippy::while_immutable_condition)]` on by default

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:28:11
|
LL | while y < 10 && x < 3 {
| ^^^^^^^^^^^^^^^

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:35:11
|
LL | while !cond {
| ^^^^^

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:79:11
|
LL | while i < 3 {
| ^^^^^

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:84:11
|
LL | while i < 3 && j > 0 {
| ^^^^^^^^^^^^^^

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:88:11
|
LL | while i < 3 {
| ^^^^^

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:103:11
|
LL | while i < 3 {
| ^^^^^

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:108:11
|
LL | while i < 3 {
| ^^^^^

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
--> $DIR/infinite_loop.rs:174:15
|
LL | while self.count < n {
Expand Down

0 comments on commit 45dc16f

Please sign in to comment.