rust-lang / rust-lang/rust-clippy
`filter_map_bool_then` borrow check error
Open
@kennywillbe is already working on this.
Since Aug 31, 2026.
C-bug
I-false-positive
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
If the predicate and the operation inside the then() both need access to the same object, at least one needing exclusive access, then the filter_map(... then(...)) cannot be split up as .filter(...).map(...). However the lint still triggers.
Lint Name
filter_map_bool_then
Reproducer
I tried this (minimized) code (playground link):
#![deny(clippy::filter_map_bool_then)]
pub fn main() {
let mut last = None;
let dedup: Vec<u32> = [1, 2, 2, 3]
.into_iter()
.filter_map(|val| {
(last != Some(val)).then(|| {
last = Some(val);
val
})
})
.collect();
println!("{:?}", dedup);
}
I saw this happen:
error: usage of `bool::then` in `filter_map`
--> src/main.rs:7:10
|
7 | .filter_map(|val| {
| __________^
8 | | (last != Some(val)).then(|| {
9 | | last = Some(val);
10 | | val
11 | | })
12 | | })
| |__________^
|
= help: consider using `filter` then `map` instead
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.98.0/index.html#filter_map_bool_then
note: the lint level is defined here
--> src/main.rs:1:9
|
1 | #![deny(clippy::filter_map_bool_then)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I expected to see this happen: Nothing, since this operation can't be split into a filter() followed by a map().
Version
Additional Labels
@rustbot label +I-suggestion-causes-error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.