Skip to content

Commit

Permalink
Merge pull request #2515 from kimsnj/infinite_loop
Browse files Browse the repository at this point in the history
lint: while immutable condition: do not lint constants
  • Loading branch information
oli-obk authored Mar 8, 2018
2 parents 4cf02c7 + ae5354e commit 6776e51
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
13 changes: 10 additions & 3 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use syntax::codemap::Span;
use utils::sugg;
use utils::const_to_u64;

use consts::constant;

use utils::{get_enclosing_block, get_parent_expr, higher, in_external_macro, is_integer_literal, is_refutable,
last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt,
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then};
Expand Down Expand Up @@ -2142,6 +2144,11 @@ fn path_name(e: &Expr) -> Option<Name> {
}

fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, block: &'tcx Block, expr: &'tcx Expr) {
if constant(cx, cond).is_some() {
// A pure constant condition (e.g. while false) is not linted.
return;
}

let mut mut_var_visitor = MutableVarsVisitor {
cx,
ids: HashMap::new(),
Expand All @@ -2152,12 +2159,12 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, b
return;
}

if mut_var_visitor.ids.len() == 0 {
if mut_var_visitor.ids.is_empty() {
span_lint(
cx,
WHILE_IMMUTABLE_CONDITION,
cond.span,
"all variables in condition are immutable. This might lead to infinite loops.",
"all variables in condition are immutable. This either leads to an infinite or to a never running loop.",
);
return;
}
Expand All @@ -2175,7 +2182,7 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, b
cx,
WHILE_IMMUTABLE_CONDITION,
expr.span,
"Variable in the condition are not mutated in the loop body. This might lead to infinite loops.",
"Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.",
);
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/infinite_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ fn used_immutable() {
}
}

const N: i32 = 5;
const B: bool = false;

fn consts() {
while false {
println!("Constants are not linted");
}

while B {
println!("Constants are not linted");
}

while N > 0 {
println!("Constants are not linted");
}
}

use std::cell::Cell;

fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() }
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/infinite_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
error: all variables in condition are immutable. This might lead to infinite loops.
error: all variables in condition are immutable. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:10:11
|
10 | while y < 10 {
| ^^^^^^
|
= note: `-D while-immutable-condition` implied by `-D warnings`

error: all variables in condition are immutable. This might lead to infinite loops.
error: all variables in condition are immutable. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:15:11
|
15 | while y < 10 && x < 3 {
| ^^^^^^^^^^^^^^^

error: all variables in condition are immutable. This might lead to infinite loops.
error: all variables in condition are immutable. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:22:11
|
22 | while !cond {
| ^^^^^

error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops.
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:52:5
|
52 | / while i < 3 {
Expand All @@ -27,15 +27,15 @@ error: Variable in the condition are not mutated in the loop body. This might le
55 | | }
| |_____^

error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops.
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:57:5
|
57 | / while i < 3 && j > 0 {
58 | | println!("KO - i and j not mentionned");
59 | | }
| |_____^

error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops.
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:61:5
|
61 | / while i < 3 {
Expand All @@ -45,7 +45,7 @@ error: Variable in the condition are not mutated in the loop body. This might le
65 | | }
| |_____^

error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops.
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:76:5
|
76 | / while i < 3 {
Expand All @@ -54,7 +54,7 @@ error: Variable in the condition are not mutated in the loop body. This might le
79 | | }
| |_____^

error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops.
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
--> $DIR/infinite_loop.rs:81:5
|
81 | / while i < 3 {
Expand Down

0 comments on commit 6776e51

Please sign in to comment.