rust-lang / rust-lang/rust-clippy

while let on reverse iterator

Open
#16,202 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Checks for while let expressions on reverse iterators.

Advantage

Readability. Even the reverse loop is shorter and conveys the intent better.

Drawbacks

No response

Example
use std::str::Chars;

fn do_print(mut chars: Chars) {
    while let Some(c) = chars.next_back() {
        print!("{c}");
    }
}

fn main() {
    let s = String::from("\n!dlrow olleH");
    do_print(s.chars());
}

Could be written as:

use std::str::Chars;

fn do_print(mut chars: Chars) {
    for c in chars.by_ref().rev() {
        print!("{c}");
    }
}

fn main() {
    let s = String::from("\n!dlrow olleH");
    do_print(s.chars());
}
Comparison with existing lints

Very much like clippy::while_let_on_iterator this cleans up code.

Additional Context

No response

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 reading the existing clippy::while_let_on_iterator lint, which this proposal closely compares against. Determine how the reverse-iterator pattern should be recognized and what equivalent for-loop transformation the examples require. Done means the new lint is implemented with appropriate coverage for the shown pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.