From cecfdeab196f30cf12bfa757e3e15ce9e3417689 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 3 Apr 2018 16:41:30 +0200 Subject: [PATCH] Don't trigger while_immutable_condition for mutable fields of tuples/structs --- clippy_lints/src/loops.rs | 12 ++++++------ tests/ui/infinite_loop.rs | 6 ++++++ tests/ui/infinite_loop.stderr | 24 ++++++++++++------------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 6f04940ae31..9a6b7627b47 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -2237,7 +2237,7 @@ struct MutVarsDelegate { } impl<'tcx> MutVarsDelegate { - fn update(&mut self, cat: &'tcx Categorization, sp: Span) { + fn update(&mut self, cat: &'tcx Categorization) { match *cat { Categorization::Local(id) => if let Some(used) = self.used_mutably.get_mut(&id) { @@ -2249,7 +2249,7 @@ impl<'tcx> MutVarsDelegate { //`while`-body, not just the ones in the condition. self.skip = true }, - Categorization::Deref(ref cmt, _) => self.update(&cmt.cat, sp), + Categorization::Deref(ref cmt, _) | Categorization::Interior(ref cmt, _) => self.update(&cmt.cat), _ => {} } } @@ -2263,14 +2263,14 @@ impl<'tcx> Delegate<'tcx> for MutVarsDelegate { fn consume_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: ConsumeMode) {} - fn borrow(&mut self, _: NodeId, sp: Span, cmt: cmt<'tcx>, _: ty::Region, bk: ty::BorrowKind, _: LoanCause) { + fn borrow(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, _: ty::Region, bk: ty::BorrowKind, _: LoanCause) { if let ty::BorrowKind::MutBorrow = bk { - self.update(&cmt.cat, sp) + self.update(&cmt.cat) } } - fn mutate(&mut self, _: NodeId, sp: Span, cmt: cmt<'tcx>, _: MutateMode) { - self.update(&cmt.cat, sp) + fn mutate(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, _: MutateMode) { + self.update(&cmt.cat) } fn decl_without_init(&mut self, _: NodeId, _: Span) {} diff --git a/tests/ui/infinite_loop.rs b/tests/ui/infinite_loop.rs index 4029f9a9b29..353d34134eb 100644 --- a/tests/ui/infinite_loop.rs +++ b/tests/ui/infinite_loop.rs @@ -55,6 +55,12 @@ fn immutable_condition() { } }; c(); + + let mut tup = (0, 0); + while tup.0 < 5 { + tup.0 += 1; + println!("OK - tup.0 gets mutated") + } } fn unused_var() { diff --git a/tests/ui/infinite_loop.stderr b/tests/ui/infinite_loop.stderr index d24fd925e6d..26ec9582fb4 100644 --- a/tests/ui/infinite_loop.stderr +++ b/tests/ui/infinite_loop.stderr @@ -19,39 +19,39 @@ error: Variable in the condition are not mutated in the loop body. This either l | ^^^^^ 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:64:11 + --> $DIR/infinite_loop.rs:70:11 | -64 | while i < 3 { +70 | while i < 3 { | ^^^^^ 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:69:11 + --> $DIR/infinite_loop.rs:75:11 | -69 | while i < 3 && j > 0 { +75 | while i < 3 && j > 0 { | ^^^^^^^^^^^^^^ 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:73:11 + --> $DIR/infinite_loop.rs:79:11 | -73 | while i < 3 { +79 | while i < 3 { | ^^^^^ 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:88:11 + --> $DIR/infinite_loop.rs:94:11 | -88 | while i < 3 { +94 | while i < 3 { | ^^^^^ 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:93:11 + --> $DIR/infinite_loop.rs:99:11 | -93 | while i < 3 { +99 | while i < 3 { | ^^^^^ 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:156:15 + --> $DIR/infinite_loop.rs:162:15 | -156 | while self.count < n { +162 | while self.count < n { | ^^^^^^^^^^^^^^ error: aborting due to 9 previous errors