mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 23:20:39 +00:00
while_immutable_cond: check condition for mutation
This commit is contained in:
parent
ce37099bd3
commit
63425de77d
2 changed files with 20 additions and 5 deletions
|
@ -28,11 +28,14 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, expr: &'
|
|||
return;
|
||||
}
|
||||
let used_in_condition = &var_visitor.ids;
|
||||
let no_cond_variable_mutated = if let Some(used_mutably) = mutated_variables(expr, cx) {
|
||||
used_in_condition.is_disjoint(&used_mutably)
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
let mutated_in_body = mutated_variables(expr, cx);
|
||||
let mutated_in_condition = mutated_variables(cond, cx);
|
||||
let no_cond_variable_mutated =
|
||||
if let (Some(used_mutably_body), Some(used_mutably_cond)) = (mutated_in_body, mutated_in_condition) {
|
||||
used_in_condition.is_disjoint(&used_mutably_body) && used_in_condition.is_disjoint(&used_mutably_cond)
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
let mutable_static_in_cond = var_visitor.def_ids.iter().any(|(_, v)| *v);
|
||||
|
||||
let mut has_break_or_return_visitor = HasBreakOrReturnVisitor {
|
||||
|
|
|
@ -192,11 +192,23 @@ fn while_loop_with_break_and_return() {
|
|||
}
|
||||
}
|
||||
|
||||
fn immutable_condition_false_positive(mut n: u64) -> u32 {
|
||||
let mut count = 0;
|
||||
while {
|
||||
n >>= 1;
|
||||
n != 0
|
||||
} {
|
||||
count += 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
fn main() {
|
||||
immutable_condition();
|
||||
unused_var();
|
||||
used_immutable();
|
||||
internally_mutable();
|
||||
immutable_condition_false_positive(5);
|
||||
|
||||
let mut c = Counter { count: 0 };
|
||||
c.inc_n(5);
|
||||
|
|
Loading…
Reference in a new issue