Closed
Description
What it does
It warns the user when SplitN is used where n == 1, specifically when used in combination with .next(). When used in this way, splitn is not doing anything and can be omitted altogether. It is more likely that the user meant to only get the first part after splitting, which means n has to be at least 2.
Categories (optional)
- Kind: clippy::correctness
What is the advantage of the recommended code over the original code
In the most likely case it will point the user to undesired behaviour, in the other case - where the written code is actually the user's intent - it will make the code more concise.
Drawbacks
None.
Example
fn main() {
let slice = [10, 40, 30, 20, 60, 50];
let iter = slice.splitn(1, |num| *num % 3 == 0).next();
println!("{:?}", iter);
}
Output:
Some([10, 40, 30, 20, 60, 50])
Could be written as:
fn main() {
let slice = [10, 40, 30, 20, 60, 50];
let iter = Some(slice);
println!("{:?}", iter);
}
Output:
Some([10, 40, 30, 20, 60, 50])
Should (most likely) be written as:
fn main() {
let slice = [10, 40, 30, 20, 60, 50];
let iter = slice.splitn(2, |num| *num % 3 == 0).next();
println!("{:?}", iter);
}
Output:
Some([10, 40])