Skip to content

filter_next: after fixes were automatically applied the compiler reported errors #10029

Closed
@DarylPinto

Description

@DarylPinto

Summary

Clippy suggested a lint to replace foo.filter().next() with foo.find(). However, it didn't mention anything about requiring foo to be mutable.

When I ran cargo clippy --fix to automatically apply the lint, it caused a compiler error and told me to file an issue on Github.

You can view the code here, as well as in the Reproducer section below.

Lint Name

filter_next

Reproducer

I tried this code:

fn puzzle(input: &str) -> usize {
    input
        .lines()
        .filter(|line| {
            let assignments = line
                .split(',')
                .map(|assignment| {
                    assignment
                        .split('-')
                        .filter_map(|n| n.parse::<i32>().ok())
                        .collect::<Vec<_>>()
                })
                .collect::<Vec<_>>();

            let assignment_one = assignments[0][0]..=assignments[0][1];
            let assignment_two = assignments[1][0]..=assignments[1][1];

            assignment_one
                .filter(|n| assignment_two.contains(n))
                .next()
                .is_some()
        })
        .count()
}

I saw this happen:

When I ran cargo clippy --fix the following output was written to my terminal:

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

  * day04b/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/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[E0596]: cannot borrow `assignment_one` as mutable, as it is not declared as mutable
  --> day04b/src/main.rs:24:13
   |
21 |             let assignment_one = assignments[0][0]..=assignments[0][1];
   |                 -------------- help: consider changing this to be mutable: `mut assignment_one`
...
24 |             assignment_one.find(|n| assignment_two.contains(n))
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

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

I expected to see one of the following things happen:

  • Clippy to apply the lint without causing a compiler error
  • Clippy to not suggest this lint at all

Version

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: aarch64-apple-darwin
release: 1.65.0
LLVM version: 15.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions