Skip to content

clippy::double_ended_iterator_last is dangerous to autoapply #14444

@GoldsteinE

Description

@GoldsteinE

Summary

Clippy suggests changing .last() to .next_back() which changes semantics when iterator pipeline is impure.

Reproducer

Consider this (a bit contrived) code:

let mut squares = vec![];
let last_square = [1, 2, 3]
    .into_iter()
    .map(|x| {
        squares.push(x * x);
        Some(x * x)
    })
    .last();
dbg!(squares, last_square);

Clippy suggests changing .last() to .next_back() which dramatically changes the behaviour.

Version

rustc 1.87.0-nightly (3ea711f17 2025-03-09)
binary: rustc
commit-hash: 3ea711f17e3946ac3f4df11691584e2c56b4b0cf
commit-date: 2025-03-09
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

@rustbot label +I-suggestion-causes-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions