-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2483 from kimsnj/infinite_loop
immutable while condition
- Loading branch information
Showing
5 changed files
with
319 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
fn fn_val(i: i32) -> i32 { unimplemented!() } | ||
fn fn_constref(i: &i32) -> i32 { unimplemented!() } | ||
fn fn_mutref(i: &mut i32) { unimplemented!() } | ||
fn fooi() -> i32 { unimplemented!() } | ||
fn foob() -> bool { unimplemented!() } | ||
|
||
fn immutable_condition() { | ||
// Should warn when all vars mentionned are immutable | ||
let y = 0; | ||
while y < 10 { | ||
println!("KO - y is immutable"); | ||
} | ||
|
||
let x = 0; | ||
while y < 10 && x < 3 { | ||
let mut k = 1; | ||
k += 2; | ||
println!("KO - x and y immutable"); | ||
} | ||
|
||
let cond = false; | ||
while !cond { | ||
println!("KO - cond immutable"); | ||
} | ||
|
||
let mut i = 0; | ||
while y < 10 && i < 3 { | ||
i += 1; | ||
println!("OK - i is mutable"); | ||
} | ||
|
||
let mut mut_cond = false; | ||
while !mut_cond || cond { | ||
mut_cond = true; | ||
println!("OK - mut_cond is mutable"); | ||
} | ||
|
||
while fooi() < x { | ||
println!("OK - Fn call results may vary"); | ||
} | ||
|
||
while foob() { | ||
println!("OK - Fn call results may vary"); | ||
} | ||
|
||
} | ||
|
||
fn unused_var() { | ||
// Should warn when a (mutable) var is not used in while body | ||
let (mut i, mut j) = (0, 0); | ||
|
||
while i < 3 { | ||
j = 3; | ||
println!("KO - i not mentionned"); | ||
} | ||
|
||
while i < 3 && j > 0 { | ||
println!("KO - i and j not mentionned"); | ||
} | ||
|
||
while i < 3 { | ||
let mut i = 5; | ||
fn_mutref(&mut i); | ||
println!("KO - shadowed"); | ||
} | ||
|
||
while i < 3 && j > 0 { | ||
i = 5; | ||
println!("OK - i in cond and mentionned"); | ||
} | ||
} | ||
|
||
fn used_immutable() { | ||
let mut i = 0; | ||
|
||
while i < 3 { | ||
fn_constref(&i); | ||
println!("KO - const reference"); | ||
} | ||
|
||
while i < 3 { | ||
fn_val(i); | ||
println!("KO - passed by value"); | ||
} | ||
|
||
while i < 3 { | ||
println!("OK - passed by mutable reference"); | ||
fn_mutref(&mut i) | ||
} | ||
|
||
while i < 3 { | ||
fn_mutref(&mut i); | ||
println!("OK - passed by mutable reference"); | ||
} | ||
} | ||
|
||
use std::cell::Cell; | ||
|
||
fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() } | ||
|
||
fn internally_mutable() { | ||
let b = Cell::new(true); | ||
|
||
while b.get() { // b cannot be silently coerced to `bool` | ||
maybe_i_mutate(&b); | ||
println!("OK - Method call within condition"); | ||
} | ||
} | ||
|
||
fn main() { | ||
immutable_condition(); | ||
unused_var(); | ||
used_immutable(); | ||
internally_mutable(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
error: all variables in condition are immutable. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:10:11 | ||
| | ||
10 | while y < 10 { | ||
| ^^^^^^ | ||
| | ||
= note: `-D while-immutable-condition` implied by `-D warnings` | ||
|
||
error: all variables in condition are immutable. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:15:11 | ||
| | ||
15 | while y < 10 && x < 3 { | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: all variables in condition are immutable. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:22:11 | ||
| | ||
22 | while !cond { | ||
| ^^^^^ | ||
|
||
error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:52:5 | ||
| | ||
52 | / while i < 3 { | ||
53 | | j = 3; | ||
54 | | println!("KO - i not mentionned"); | ||
55 | | } | ||
| |_____^ | ||
|
||
error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:57:5 | ||
| | ||
57 | / while i < 3 && j > 0 { | ||
58 | | println!("KO - i and j not mentionned"); | ||
59 | | } | ||
| |_____^ | ||
|
||
error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:61:5 | ||
| | ||
61 | / while i < 3 { | ||
62 | | let mut i = 5; | ||
63 | | fn_mutref(&mut i); | ||
64 | | println!("KO - shadowed"); | ||
65 | | } | ||
| |_____^ | ||
|
||
error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:76:5 | ||
| | ||
76 | / while i < 3 { | ||
77 | | fn_constref(&i); | ||
78 | | println!("KO - const reference"); | ||
79 | | } | ||
| |_____^ | ||
|
||
error: Variable in the condition are not mutated in the loop body. This might lead to infinite loops. | ||
--> $DIR/infinite_loop.rs:81:5 | ||
| | ||
81 | / while i < 3 { | ||
82 | | fn_val(i); | ||
83 | | println!("KO - passed by value"); | ||
84 | | } | ||
| |_____^ | ||
|
||
error: aborting due to 8 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4cf02c7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I am getting a warning that
might lead to an infinite loop.
4cf02c7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matthiaskrgr that looks like a very degenerate case ;)
But I see your point. The lint should probably not trigger on constants, just on immutable conditions