Skip to content

filter_map_bool_then generates incorrect autofix when the bool in question is a reference #11503

@narpfel

Description

@narpfel

Summary

Method calls implicitly dereference self, so then can be called on (possibly nested) references to bool. The autofix for filter_map_bool_then doesn’t take this into account and tries to return references to bool from filter.

Reproducer

I tried this code:

fn main() {
    let bools: &[bool] = &[true, false, false, true];
    let result: Vec<usize> = bools
        .iter()
        .enumerate()
        .filter_map(|(i, b)| b.then(|| i))
        .collect();
    dbg!(result);
}

I expected to see this happen:
cargo clippy --fix should generate code like this:

fn main() {
    let bools: &[bool] = &[true, false, false, true];
    let result: Vec<usize> = bools
        .iter()
        .enumerate()
        .filter(|&(i, b)| *b)
        .map(|(i, b)| i)
        .collect();
    dbg!(result);
}

Instead, this happened:

warning: failed to automatically apply fixes suggested by rustc to crate `project`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0308]: mismatched types
 --> src/main.rs:6:27
  |
6 |         .filter(|&(i, b)| b).map(|(i, b)| i)
  |                           ^ expected `bool`, found `&bool`
  |
help: consider dereferencing the borrow
  |
6 |         .filter(|&(i, b)| *b).map(|(i, b)| i)
  |                           +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: usage of `bool::then` in `filter_map`
 --> src/main.rs:6:10
  |
6 |         .filter_map(|(i, b)| b.then(|| i))
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| b).map(|(i, b)| i)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#filter_map_bool_then
  = note: `#[warn(clippy::filter_map_bool_then)]` on by default

Version

rustc 1.74.0-nightly (8142a319e 2023-09-13)
binary: rustc
commit-hash: 8142a319ed5c1d1f96e5a1881a6546e463b77c8f
commit-date: 2023-09-13
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    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