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

block_in_if_condition_stmt: false positive with blocks inside iterator method calls #1141

Closed
birkenfeld opened this issue Aug 5, 2016 · 2 comments · Fixed by #6741
Closed
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

@birkenfeld
Copy link
Contributor

Example:

fn main() {
    let mut range = 0..10;
    debug_assert!(range.all(|i| { i < 10 }));
}

Maybe just an in_macro check missing?

@oli-obk
Copy link
Contributor

oli-obk commented Aug 8, 2016

actually this would also occur in if (0..10).all(|i| { i < 10 }), the reasoning is in the corresponding unit test:

// this is a sneaky case, where the block isn't directly in the condition, but is actually
// inside a closure that the condition is using. same principle applies. add some extra
// expressions to make sure linter isn't confused by them.

the "same principle" being

in an 'if' condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a 'let'

The above case should probably be linted against differently (disallow blocks without statements)

@matthiaskrgr matthiaskrgr changed the title block_in_if_condition_stmt: false positive in macros block_in_if_condition_stmt: false positive with blocks inside iterator method calls Mar 21, 2020
@matthiaskrgr
Copy link
Member

Just ran into this

let v = vec![1,2,3];

if v.into_iter().any(|x| {x == 4 }) {
    println!("contains 4!");
}
warning: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
 --> src/main.rs:6:26
  |
6 | if v.into_iter().any(|x| {x == 4 }) {
  |                          ^^^^^^^^^
  |
  = note: `#[warn(clippy::block_in_if_condition_stmt)]` on by default

IMO we should not lint here..

@flip1995 flip1995 added the C-bug Category: Clippy is not doing the correct thing label Mar 23, 2020
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 21, 2020
@bors bors closed this as completed in 2f19f5f Feb 14, 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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants