rust-lang / rust-lang/rust-clippy
Catch double reverses on the same iterator
@willwang-io is already working on this.
Since Aug 25, 2026.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Warn on code like this:
.rev().map(...).rev()
This looks like it will run the map function on the values in reverse order, then return the outputs in the original order. However, it actually just calls map() in forwards order and the two rev()s cancel each other out.
The lint should suggest changing it to .map(...), deleting the rev()s, since that is what the code actually does. It should also suggest some other solution if the user wanted to map in reverse order (such as .rev().map(...).collect() followed by .reverse()).
Advantage
Avoids confusing readers. In other languages (for example Javascript with reverse or toReverse instead of rev) the behavior is the opposite of the behavior in Rust.
Drawbacks
None
Example
let mut total: i32 = 0;
let data = [1, 2, 3, 7];
let sums_to_end: Vec<i32> = data.iter().rev().map(|x| {
total += x;
total
}).rev().collect();
Could be written as:
let mut total: i32 = 0;
let data = [1, 2, 3, 7];
let sums_to_end: Vec<i32> = data.iter().map(|x| {
total += x;
total
}).collect();
Comparison with existing lints
N/A
Additional Context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.