Skip to content

filter_map_bool_then triggers in some cases when variable is borrowed in predicate and transformation. #11617

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

Closed
dsemi opened this issue Oct 5, 2023 · 0 comments · Fixed by #14370
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@dsemi
Copy link

dsemi commented Oct 5, 2023

Summary

In cases where a variable needs to be borrowed by both the predicate function in filter and the transformation function in map, and one of those borrows is mutable, the filter_map -> filter + map transformation is invalid.

Lint Name

filter_map_bool_then

Reproducer

I tried this code:

fn test() {
    let mut x: Vec<usize> = vec![0; 10];
    let _ = (0..x.len()).filter_map(|i| {
        (x[i] != i).then(|| {
            x[i] = i;
            i
        })
    });
}

I expect this to not trigger a lint warning because the transformation is invalid. The transformed code looks like:

fn test() {
    let mut x: Vec<usize> = vec![0; 10];
    let _ = (0..x.len()).filter(|&i| x[i] != i).map(|i| {
        x[i] = i;
        i
    });
}

This fails to compile because x is borrowed in filter and mutably borrowed in map:

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
  --> src/main.rs:42:53
   |
42 |     let _ = (0..x.len()).filter(|&i| x[i] != i).map(|i| {
   |                                 ---- -          --- ^^^ mutable borrow occurs here
   |                                 |    |          |
   |                                 |    |          immutable borrow later used by call
   |                                 |    first borrow occurs due to use of `x` in closure
   |                                 immutable borrow occurs here
43 |         x[i] = i;
   |         - second borrow occurs due to use of `x` in closure

For more information about this error, try `rustc --explain E0502`.

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: aarch64-apple-darwin
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

@rustbot label +I-suggestion-causes-error

@dsemi dsemi added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 5, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 5, 2023
github-merge-queue bot pushed a commit that referenced this issue Mar 21, 2025
…e decompose directly (#14370)

Closes #11617
Closes #14368

Clippy gives wrong suggestions when the filter and then cannot be put
into closure directly. Since trying to transform these can be too
complicated, Clippy will simply warn but don't try to fix.

changelog: [`filter_map_bool_then`]: fix wrong suggestions when the
closure cannot be decompose directly
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-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
2 participants