Skip to content

Commit a3cdcf1

Browse files
authored
Merge pull request #2622 from flip1995/imm_while_fields
Don't trigger while_immutable_condition for mutable fields
2 parents b7f5871 + cecfdea commit a3cdcf1

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

clippy_lints/src/loops.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,7 @@ struct MutVarsDelegate {
22372237
}
22382238

22392239
impl<'tcx> MutVarsDelegate {
2240-
fn update(&mut self, cat: &'tcx Categorization, sp: Span) {
2240+
fn update(&mut self, cat: &'tcx Categorization) {
22412241
match *cat {
22422242
Categorization::Local(id) =>
22432243
if let Some(used) = self.used_mutably.get_mut(&id) {
@@ -2249,7 +2249,7 @@ impl<'tcx> MutVarsDelegate {
22492249
//`while`-body, not just the ones in the condition.
22502250
self.skip = true
22512251
},
2252-
Categorization::Deref(ref cmt, _) => self.update(&cmt.cat, sp),
2252+
Categorization::Deref(ref cmt, _) | Categorization::Interior(ref cmt, _) => self.update(&cmt.cat),
22532253
_ => {}
22542254
}
22552255
}
@@ -2263,14 +2263,14 @@ impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
22632263

22642264
fn consume_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: ConsumeMode) {}
22652265

2266-
fn borrow(&mut self, _: NodeId, sp: Span, cmt: cmt<'tcx>, _: ty::Region, bk: ty::BorrowKind, _: LoanCause) {
2266+
fn borrow(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, _: ty::Region, bk: ty::BorrowKind, _: LoanCause) {
22672267
if let ty::BorrowKind::MutBorrow = bk {
2268-
self.update(&cmt.cat, sp)
2268+
self.update(&cmt.cat)
22692269
}
22702270
}
22712271

2272-
fn mutate(&mut self, _: NodeId, sp: Span, cmt: cmt<'tcx>, _: MutateMode) {
2273-
self.update(&cmt.cat, sp)
2272+
fn mutate(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, _: MutateMode) {
2273+
self.update(&cmt.cat)
22742274
}
22752275

22762276
fn decl_without_init(&mut self, _: NodeId, _: Span) {}

tests/ui/infinite_loop.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ fn immutable_condition() {
5555
}
5656
};
5757
c();
58+
59+
let mut tup = (0, 0);
60+
while tup.0 < 5 {
61+
tup.0 += 1;
62+
println!("OK - tup.0 gets mutated")
63+
}
5864
}
5965

6066
fn unused_var() {

tests/ui/infinite_loop.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,39 @@ error: Variable in the condition are not mutated in the loop body. This either l
1919
| ^^^^^
2020

2121
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
22-
--> $DIR/infinite_loop.rs:64:11
22+
--> $DIR/infinite_loop.rs:70:11
2323
|
24-
64 | while i < 3 {
24+
70 | while i < 3 {
2525
| ^^^^^
2626

2727
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
28-
--> $DIR/infinite_loop.rs:69:11
28+
--> $DIR/infinite_loop.rs:75:11
2929
|
30-
69 | while i < 3 && j > 0 {
30+
75 | while i < 3 && j > 0 {
3131
| ^^^^^^^^^^^^^^
3232

3333
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
34-
--> $DIR/infinite_loop.rs:73:11
34+
--> $DIR/infinite_loop.rs:79:11
3535
|
36-
73 | while i < 3 {
36+
79 | while i < 3 {
3737
| ^^^^^
3838

3939
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
40-
--> $DIR/infinite_loop.rs:88:11
40+
--> $DIR/infinite_loop.rs:94:11
4141
|
42-
88 | while i < 3 {
42+
94 | while i < 3 {
4343
| ^^^^^
4444

4545
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
46-
--> $DIR/infinite_loop.rs:93:11
46+
--> $DIR/infinite_loop.rs:99:11
4747
|
48-
93 | while i < 3 {
48+
99 | while i < 3 {
4949
| ^^^^^
5050

5151
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
52-
--> $DIR/infinite_loop.rs:156:15
52+
--> $DIR/infinite_loop.rs:162:15
5353
|
54-
156 | while self.count < n {
54+
162 | while self.count < n {
5555
| ^^^^^^^^^^^^^^
5656

5757
error: aborting due to 9 previous errors

0 commit comments

Comments
 (0)