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)
}