Skip to content

Don't trigger while_immutable_condition for mutable fields #2622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,7 @@ struct MutVarsDelegate {
}

impl<'tcx> MutVarsDelegate {
fn update(&mut self, cat: &'tcx Categorization, sp: Span) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a lint for this. Passing args to a function which are only ever used to recusively call itself

fn update(&mut self, cat: &'tcx Categorization) {
match *cat {
Categorization::Local(id) =>
if let Some(used) = self.used_mutably.get_mut(&id) {
Expand All @@ -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),
_ => {}
}
}
Expand All @@ -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) {}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/infinite_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/infinite_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down