Auto merge of #10094 - EricWu2003:increment-visitor-fix, r=xFrednet
fix logic in IncrementVisitor There used to be a logical bug where IncrementVisitor would completely stop checking an expression/block after seeing a continue statement. I am a little unsure of whether my fix to `IncrementVisitor` is logically sound (I hope it makes sense). Let me know what you think, and thanks in advance for the review! fixes #10058 --- changelog: FP: [`explicit_counter_loop`]: No longer ignores counter changes after `continue` expressions [#10094](https://github.com/rust-lang/rust-clippy/pull/10094) <!-- changelog_checked -->
This commit is contained in:
commit
b62319ce02
@ -25,7 +25,6 @@ pub(super) struct IncrementVisitor<'a, 'tcx> {
|
||||
cx: &'a LateContext<'tcx>, // context reference
|
||||
states: HirIdMap<IncrementVisitorVarState>, // incremented variables
|
||||
depth: u32, // depth of conditional expressions
|
||||
done: bool,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
|
||||
@ -34,7 +33,6 @@ impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
|
||||
cx,
|
||||
states: HirIdMap::default(),
|
||||
depth: 0,
|
||||
done: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,10 +49,6 @@ impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
|
||||
|
||||
impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
|
||||
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
||||
if self.done {
|
||||
return;
|
||||
}
|
||||
|
||||
// If node is a variable
|
||||
if let Some(def_id) = path_to_local(expr) {
|
||||
if let Some(parent) = get_parent_expr(self.cx, expr) {
|
||||
@ -95,7 +89,9 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
|
||||
walk_expr(self, expr);
|
||||
self.depth -= 1;
|
||||
} else if let ExprKind::Continue(_) = expr.kind {
|
||||
self.done = true;
|
||||
// If we see a `continue` block, then we increment depth so that the IncrementVisitor
|
||||
// state will be set to DontWarn if we see the variable being modified anywhere afterwards.
|
||||
self.depth += 1;
|
||||
} else {
|
||||
walk_expr(self, expr);
|
||||
}
|
||||
|
@ -189,3 +189,33 @@ mod issue_7920 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod issue_10058 {
|
||||
pub fn test() {
|
||||
// should not lint since we are increasing counter potentially more than once in the loop
|
||||
let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1];
|
||||
let mut counter = 0;
|
||||
for value in values {
|
||||
counter += 1;
|
||||
|
||||
if value == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test2() {
|
||||
// should not lint since we are increasing counter potentially more than once in the loop
|
||||
let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1];
|
||||
let mut counter = 0;
|
||||
for value in values {
|
||||
counter += 1;
|
||||
|
||||
if value != 0 {
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user