From ae5354e6ef610804ae46456df2c9001f65c8786c Mon Sep 17 00:00:00 2001 From: Karim Snj Date: Wed, 7 Mar 2018 18:24:36 +0100 Subject: [PATCH] lint: while immutable condition: do not lint constants --- clippy_lints/src/loops.rs | 13 ++++++++++--- tests/ui/infinite_loop.rs | 17 +++++++++++++++++ tests/ui/infinite_loop.stderr | 16 ++++++++-------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 7873240a95c1..f476b960d889 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -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}; @@ -2142,6 +2144,11 @@ fn path_name(e: &Expr) -> Option { } 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(), @@ -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; } @@ -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.", ); } } diff --git a/tests/ui/infinite_loop.rs b/tests/ui/infinite_loop.rs index cc694583eecb..560400f359d2 100644 --- a/tests/ui/infinite_loop.rs +++ b/tests/ui/infinite_loop.rs @@ -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) { unimplemented!() } diff --git a/tests/ui/infinite_loop.stderr b/tests/ui/infinite_loop.stderr index fba90823173d..2addd4819e6f 100644 --- a/tests/ui/infinite_loop.stderr +++ b/tests/ui/infinite_loop.stderr @@ -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 | 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` -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 { @@ -27,7 +27,7 @@ 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 { @@ -35,7 +35,7 @@ error: Variable in the condition are not mutated in the loop body. This might le 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 { @@ -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 { @@ -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 {