rust-lang / rust-lang/rust-clippy

`double_ended_iterator_last` should not emit on move FnMut closure

Open
#17,646 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

This lint is not emitted on a non-move closure, but it fails to see the order dependence when the closure is marked as move.

Lint Name

double_ended_iterator_last

Reproducer

I tried this code: (playground)

(this is a little contrived, but it's a simplified version of the code I found it on)

#[deny(clippy::double_ended_iterator_last)]
fn find_best<'a>(a: &'a [usize], b: &[i32]) -> Option<(usize, &'a usize)> {
    let mut best_val = 0;
    a
        .iter()
        .enumerate()
        .rev()
        .filter(move |&(i, &val)| {
            match b[val].cmp(&best_val) {
                Ordering::Less => false,
                Ordering::Equal if i < val => false,
                Ordering::Equal | Ordering::Greater => {
                    best_val = b[val];
                    true
                }
            }
        })
        .last()
}

I saw this happen:

error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
  --> src/main.rs:6:5
   |
 6 | /     a
 7 | |         .iter()
 8 | |         .enumerate()
 9 | |         .rev()
...  |
19 | |         })
20 | |         .last()
   | |_______________^
   |

I expected to see this happen:

No lint to be emitted, as what happens if the closure is made not move

Version
rustc 1.98.0 (88d9e12ae 2026-08-18)
binary: rustc
commit-hash: 88d9e12ae178fab0fb5cc050a94da85685d449ea
commit-date: 2026-08-18
host: x86_64-unknown-linux-gnu
release: 1.98.0
LLVM version: 22.1.8
Additional Labels

@rustbot label +I-suggestion-causes-bug

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproducer in src/main.rs and inspect the double_ended_iterator_last lint's handling of the move FnMut closure around filter and last. Compare it with the non-move closure behavior. Done means the lint is not emitted for this order-dependent case while existing applicable cases remain covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.