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

Invalid suggestion from needless_bool when condition spans multiple lines #3481

Closed
dtolnay opened this issue Dec 2, 2018 · 3 comments
Closed
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@dtolnay
Copy link
Member

dtolnay commented Dec 2, 2018

fn main() {
    let b = false;
    let used_addr = 0u64;
    let used_ring_size = 0;

    let x = if b {
        unimplemented!()
    } else if used_addr
        .checked_add(used_ring_size)
        .map_or(true, |n| n % 2 == 0)
    {
        false
    } else {
        true
    };

    println!("{}", x);
}
warning: this if-then-else expression returns a bool literal
  --> src/main.rs:8:12
   |
8  |       } else if used_addr
   |  ____________^
9  | |         .checked_add(used_ring_size)
10 | |         .map_or(true, |n| n % 2 == 0)
11 | |     {
...  |
14 | |         true
15 | |     };
   | |_____^
   |
   = note: #[warn(clippy::needless_bool)] on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_bool
help: you can reduce it to
   |
8  |     } else !used_addr
9  |         .checked_add(used_ring_size)
10 |         .map_or(true, |n| n % 2 == 0);
   |

Clippy's suggestion is not legal syntax:

} else !used_addr
    .checked_add(used_ring_size)
    .map_or(true, |n| n % 2 == 0);

clippy 0.0.212 (b2601be 2018-11-27)

@oli-obk oli-obk added C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy A-musing labels Dec 3, 2018
@fkohlgrueber
Copy link
Contributor

What should the suggestion be here? Like this?

fn main() {
    let b = false;
    let used_addr = 0u64;
    let used_ring_size = 0;

    let x = if b {
        unimplemented!()
    } else {
        !used_addr
            .checked_add(used_ring_size)
            .map_or(true, |n| n % 2 == 0)
    };
    println!("{}", x);
}

@flip1995
Copy link
Member

flip1995 commented Dec 6, 2018

Yes it seems the problem here is, that it makes the right suggestion, but it has to be put in a block (only in the case of an else if?):

} else bool_expr;

vs

} else {
    bool_expr
};

@g-bartoszek
Copy link

@dtolnay Here is my attempt to fix it: #3680

@oli-obk oli-obk closed this as completed Jan 21, 2019
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Sep 13, 2021
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 good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

6 participants