mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 17:07:17 +00:00
Merge pull request #2515 from kimsnj/infinite_loop
lint: while immutable condition: do not lint constants
This commit is contained in:
commit
6776e5123a
3 changed files with 35 additions and 11 deletions
|
@ -22,6 +22,8 @@ use syntax::codemap::Span;
|
||||||
use utils::sugg;
|
use utils::sugg;
|
||||||
use utils::const_to_u64;
|
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,
|
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,
|
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};
|
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then};
|
||||||
|
@ -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) {
|
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 {
|
let mut mut_var_visitor = MutableVarsVisitor {
|
||||||
cx,
|
cx,
|
||||||
ids: HashMap::new(),
|
ids: HashMap::new(),
|
||||||
|
@ -2152,12 +2159,12 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, b
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if mut_var_visitor.ids.len() == 0 {
|
if mut_var_visitor.ids.is_empty() {
|
||||||
span_lint(
|
span_lint(
|
||||||
cx,
|
cx,
|
||||||
WHILE_IMMUTABLE_CONDITION,
|
WHILE_IMMUTABLE_CONDITION,
|
||||||
cond.span,
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -2175,7 +2182,7 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, b
|
||||||
cx,
|
cx,
|
||||||
WHILE_IMMUTABLE_CONDITION,
|
WHILE_IMMUTABLE_CONDITION,
|
||||||
expr.span,
|
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.",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
use std::cell::Cell;
|
||||||
|
|
||||||
fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() }
|
fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() }
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
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
|
--> $DIR/infinite_loop.rs:10:11
|
||||||
|
|
|
|
||||||
10 | while y < 10 {
|
10 | while y < 10 {
|
||||||
|
@ -6,19 +6,19 @@ error: all variables in condition are immutable. This might lead to infinite loo
|
||||||
|
|
|
|
||||||
= note: `-D while-immutable-condition` implied by `-D warnings`
|
= 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
|
--> $DIR/infinite_loop.rs:15:11
|
||||||
|
|
|
|
||||||
15 | while y < 10 && x < 3 {
|
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
|
--> $DIR/infinite_loop.rs:22:11
|
||||||
|
|
|
|
||||||
22 | while !cond {
|
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
|
--> $DIR/infinite_loop.rs:52:5
|
||||||
|
|
|
|
||||||
52 | / while i < 3 {
|
52 | / while i < 3 {
|
||||||
|
@ -27,7 +27,7 @@ error: Variable in the condition are not mutated in the loop body. This might le
|
||||||
55 | | }
|
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
|
--> $DIR/infinite_loop.rs:57:5
|
||||||
|
|
|
|
||||||
57 | / while i < 3 && j > 0 {
|
57 | / while i < 3 && j > 0 {
|
||||||
|
@ -35,7 +35,7 @@ error: Variable in the condition are not mutated in the loop body. This might le
|
||||||
59 | | }
|
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
|
--> $DIR/infinite_loop.rs:61:5
|
||||||
|
|
|
|
||||||
61 | / while i < 3 {
|
61 | / while i < 3 {
|
||||||
|
@ -45,7 +45,7 @@ error: Variable in the condition are not mutated in the loop body. This might le
|
||||||
65 | | }
|
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
|
--> $DIR/infinite_loop.rs:76:5
|
||||||
|
|
|
|
||||||
76 | / while i < 3 {
|
76 | / while i < 3 {
|
||||||
|
@ -54,7 +54,7 @@ error: Variable in the condition are not mutated in the loop body. This might le
|
||||||
79 | | }
|
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
|
--> $DIR/infinite_loop.rs:81:5
|
||||||
|
|
|
|
||||||
81 | / while i < 3 {
|
81 | / while i < 3 {
|
||||||
|
|
Loading…
Reference in a new issue