Open
Description
Code like the following triggers clippy::while_immutable_condition
:
static mut GLOBAL: *mut i8 = 0 as *mut _;
fn main() {
let mut x = 0;
let p: *mut i8 = &mut x;
unsafe {
GLOBAL = p;
while *p == 0 {
*GLOBAL += 1;
}
}
}
The exact error message is:
error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never
running loop.
--> src/main.rs:8:15
|
8 | while *p > 0 {
| ^^^^^^
|
= note: #[deny(clippy::while_immutable_condition)] on by default
= help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_immutable_condition
There are clearly two mut pointers involved above, but it's also possible for this to happen with e.g. external functions:
extern {
fn stash_the_pointer(x: *mut i8);
fn use_the_pointer();
}
fn main() {
let mut x = 0;
let p: *mut i8 = &mut x;
unsafe {
stash_the_pointer(p);
while *p == 0 {
use_the_pointer();
}
}
}
Clippy complains also about the above, although in reality, stash_the_pointer
could've saved the pointer somewhere, allowing use_the_pointer
to update it.
$ cargo clippy --version
clippy 0.0.212 (2e26fdc 2018-11-22)
Having the lint disregard mut pointers entirely would be a simple way of papering over this, but a more accurate fix might be something along the lines of:
- If the while condition is based on a mut pointer;
- And the pointer has been assigned to a static global or passed to a function;
- And there is a use of a static global or a function call in the loop;
- Then the lint shouldn't fire.
But maybe it's not that simple, I didn't really think about it very hard.