Skip to content

search_is_some suggests broken code when match guard is present #15102

Open
@tremwil

Description

@tremwil

Summary

Using the matches! macro with a match guard inside the find makes search_is_some generates incorrect code in the suggestion where the match expression is emitted three times.

This also makes cargo clippy --fix fail to apply the suggestion.

I tested the code below on rustc 1.86, but it also fails in 1.87 and the latest nightly (Playground).

Reproducer

I tried this code (Playground):

fn main() {
    let values = [None, Some(3)];
    let has_even = values.iter().find(|v| matches!(v, Some(x) if x % 2 == 0)).is_some();
    println!("{has_even}");
}

I expected to see this happen:

warning: called `is_some()` after searching an `Iterator` with `find`
 --> src/main.rs:4:34
  |
4 |     let has_even = values.iter().find(|v| matches!(v, Some(x) if x % 2 == 0)).is_some();
  |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `any(|v| matches!(v, Some(x) if x % 2 == 0))`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some
  = note: `#[warn(clippy::search_is_some)]` on by default

Instead, this happened:

warning: called `is_some()` after searching an `Iterator` with `find`
 --> src/main.rs:4:34
  |
4 |     let has_even = values.iter().find(|v| matches!(v, Some(x) if x % 2 == 0)).is_some();
  |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `any(|v| matches!(&v, Some(x)Some(x)Some(x) if &x % 2 == 0))`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some
  = note: `#[warn(clippy::search_is_some)]` on by default

Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions