rust-lang / rust-lang/rust-clippy

needless_range_loop suggestion improvement

Open
#8,012 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I encountered one suggestion from Clippy in context of needless_range_loop for a code as below

for idx in idx1..=idx2 {
// some stuff with Vec<int> type individual
}

it currently suggests using

 for <item> in individual.iter_mut().take(idx2 + 1).skip(idx1)

This is all good and works but in my opinion the iterator can take far lesser values than it's taking now. Am not sure if it has any performance impact which I think it should have given it's taking more values into consideration.

take(n) yields elements until n elements are yielded or the end of the iterator is reached

Based on the above doc I assume that yielding idx2+1 will take more time/memory than `idx2+1-idx1
I recommend using below as suggestion

for <item> in individual.iter_mut().skip(idx1).take(idx2 - idx1 + 1)

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 locating the implementation and tests for the needless_range_loop lint in rust-clippy, then compare the two iterator suggestions in the issue. Verify the intended range semantics and whether the proposed ordering changes behavior or performance, and update the lint's expected suggestion and tests if appropriate.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.