RS.CLIPPY.UNNECESSARY_FILTER_MAP

Using `filter_map` when a more succinct alternative exists

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: unnecessary_filter_map. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for filter_map calls that could be replaced by filter or map. More specifically it checks if the closure provided is only performing one of the filter or map operations and suggests the appropriate option.

Why is this bad?

Complexity. The intent is also clearer if only a single operation is being performed.

Example

let _ = (0..3).filter_map(|x| if x > 2 { Some(x) } else { None });

// As there is no transformation of the argument this could be written as:
let _ = (0..3).filter(|&x| x > 2);
let _ = (0..4).filter_map(|x| Some(x + 1));

// As there is no conditional check on the argument this could be written as:
let _ = (0..4).map(|x| x + 1);