Skip to content
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

while_immutable_condition false positive with mut pointers #3548

Open
Deewiant opened this issue Dec 14, 2018 · 0 comments
Open

while_immutable_condition false positive with mut pointers #3548

Deewiant opened this issue Dec 14, 2018 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Deewiant
Copy link

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.

@oli-obk oli-obk added the C-bug Category: Clippy is not doing the correct thing label Dec 14, 2018
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 22, 2020
dtolnay added a commit to dtolnay/unsafe-libyaml that referenced this issue Jul 23, 2022
rust-lang/rust-clippy#3548

    error: variables in the condition are not mutated in the loop body
        --> src/scanner.rs:1031:15
         |
    1031 |           while CHECK!((*parser).buffer, b' ')
         |  _______________^
    1032 | |             || ((*parser).flow_level != 0 || (*parser).simple_key_allowed == 0)
    1033 | |                 && CHECK!((*parser).buffer, b'\t')
         | |__________________________________________________^
         |
         = note: `-D clippy::while-immutable-condition` implied by `-D clippy::all`
         = note: this may lead to an infinite or to a never running loop
         = note: this loop contains `return`s or `break`s
         = help: rewrite it as `if cond { loop { } }`
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_immutable_condition

    error: variables in the condition are not mutated in the loop body
        --> src/macros.rs:138:9
         |
    138  |         *$string.pointer >= b'0' && *$string.pointer <= b'9'
         |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
        ::: src/scanner.rs:1319:11
         |
    1319 |     while IS_DIGIT!((*parser).buffer) {
         |           --------------------------- in this macro invocation
         |
         = note: this may lead to an infinite or to a never running loop
         = note: this loop contains `return`s or `break`s
         = help: rewrite it as `if cond { loop { } }`
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_immutable_condition
         = note: this error originates in the macro `IS_DIGIT` (in Nightly builds, run with -Z macro-backtrace for more info)
dtolnay added a commit to dtolnay/unsafe-libyaml that referenced this issue Jul 23, 2022
rust-lang/rust-clippy#3548

    error: variables in the condition are not mutated in the loop body
        --> src/scanner.rs:1031:15
         |
    1031 |           while CHECK!((*parser).buffer, b' ')
         |  _______________^
    1032 | |             || ((*parser).flow_level != 0 || (*parser).simple_key_allowed == 0)
    1033 | |                 && CHECK!((*parser).buffer, b'\t')
         | |__________________________________________________^
         |
         = note: `-D clippy::while-immutable-condition` implied by `-D clippy::all`
         = note: this may lead to an infinite or to a never running loop
         = note: this loop contains `return`s or `break`s
         = help: rewrite it as `if cond { loop { } }`
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_immutable_condition

    error: variables in the condition are not mutated in the loop body
        --> src/macros.rs:138:9
         |
    138  |         *$string.pointer >= b'0' && *$string.pointer <= b'9'
         |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
        ::: src/scanner.rs:1319:11
         |
    1319 |     while IS_DIGIT!((*parser).buffer) {
         |           --------------------------- in this macro invocation
         |
         = note: this may lead to an infinite or to a never running loop
         = note: this loop contains `return`s or `break`s
         = help: rewrite it as `if cond { loop { } }`
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_immutable_condition
         = note: this error originates in the macro `IS_DIGIT` (in Nightly builds, run with -Z macro-backtrace for more info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

3 participants