-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4966 - bradsherman:iter-nth-zero, r=flip1995
New Lint: Iter nth zero Check for the use of `iter.nth(0)` and encourage `iter.next()` instead as it is more readable changelog: add new lint when `iter.nth(0)` is used Fixes #4957
- Loading branch information
Showing
9 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::iter_nth_zero)] | ||
use std::collections::HashSet; | ||
|
||
struct Foo {} | ||
|
||
impl Foo { | ||
fn nth(&self, index: usize) -> usize { | ||
index + 1 | ||
} | ||
} | ||
|
||
fn main() { | ||
let f = Foo {}; | ||
f.nth(0); // lint does not apply here | ||
|
||
let mut s = HashSet::new(); | ||
s.insert(1); | ||
let _x = s.iter().next(); | ||
|
||
let mut s2 = HashSet::new(); | ||
s2.insert(2); | ||
let mut iter = s2.iter(); | ||
let _y = iter.next(); | ||
|
||
let mut s3 = HashSet::new(); | ||
s3.insert(3); | ||
let mut iter2 = s3.iter(); | ||
let _unwrapped = iter2.next().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::iter_nth_zero)] | ||
use std::collections::HashSet; | ||
|
||
struct Foo {} | ||
|
||
impl Foo { | ||
fn nth(&self, index: usize) -> usize { | ||
index + 1 | ||
} | ||
} | ||
|
||
fn main() { | ||
let f = Foo {}; | ||
f.nth(0); // lint does not apply here | ||
|
||
let mut s = HashSet::new(); | ||
s.insert(1); | ||
let _x = s.iter().nth(0); | ||
|
||
let mut s2 = HashSet::new(); | ||
s2.insert(2); | ||
let mut iter = s2.iter(); | ||
let _y = iter.nth(0); | ||
|
||
let mut s3 = HashSet::new(); | ||
s3.insert(3); | ||
let mut iter2 = s3.iter(); | ||
let _unwrapped = iter2.nth(0).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: called `.nth(0)` on a `std::iter::Iterator` | ||
--> $DIR/iter_nth_zero.rs:20:14 | ||
| | ||
LL | let _x = s.iter().nth(0); | ||
| ^^^^^^^^^^^^^^^ help: try calling: `s.iter().next()` | ||
| | ||
= note: `-D clippy::iter-nth-zero` implied by `-D warnings` | ||
|
||
error: called `.nth(0)` on a `std::iter::Iterator` | ||
--> $DIR/iter_nth_zero.rs:25:14 | ||
| | ||
LL | let _y = iter.nth(0); | ||
| ^^^^^^^^^^^ help: try calling: `iter.next()` | ||
|
||
error: called `.nth(0)` on a `std::iter::Iterator` | ||
--> $DIR/iter_nth_zero.rs:30:22 | ||
| | ||
LL | let _unwrapped = iter2.nth(0).unwrap(); | ||
| ^^^^^^^^^^^^ help: try calling: `iter2.next()` | ||
|
||
error: aborting due to 3 previous errors | ||
|