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

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

Closed
narpfel opened this issue Sep 14, 2023 · 0 comments · Fixed by #11506
Closed
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@narpfel
Copy link
Contributor

narpfel commented Sep 14, 2023

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

@narpfel narpfel added the C-bug Category: Clippy is not doing the correct thing label Sep 14, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Sep 14, 2023
@bors bors closed this as completed in eaf640d Sep 15, 2023
bors added a commit that referenced this issue Sep 17, 2023
[`filter_map_bool_then`]: include multiple derefs from adjustments

In #11506 this lint was improved to suggest one deref if the bool is behind references (fixed the FP #11503), however it might need multiple dereferences if the bool is behind multiple layers of references or custom derefs. E.g. `&&&bool` needs `***b`.

changelog: [`filter_map_bool_then`]: suggest as many dereferences as there are needed to get to the bool
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants