skip(0) on an iterator discards nothing

ID

rust.correctness.skip_iterators

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Api Misuse

Language

Rust

Description

Detects skip(0) in an iterator chain, including suffixed spellings of the literal such as skip(0usize).

Rationale

skip(0) yields the same sequence it was given, so the adaptor only adds a layer to the chain and to the type. It reads as if a header or sentinel were being dropped, which makes the chain harder to follow, and it is often a leftover offset that should have been the real count.

The following code illustrates the pattern detected by this rule:

pub fn totals(values: &[u32]) -> (u32, u32) {
    // FLAGGED: skip(0) on an iterator discards nothing
    let all: u32 = values.iter().skip(0).sum();
    // FLAGGED: skip(0) on an iterator discards nothing
    let tail: u32 = values.iter().skip(0usize).map(|v| v * 2).sum();
    (all, tail)
}

Remediation

Delete the skip(0) call. If a variable number of elements really is meant to be dropped, pass that count instead of the literal, and use skip_while when the boundary is decided by the values rather than by a position.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved