Auto merge of #10993 - Centri3:iter_nth_zero, r=Manishearth

Don't lint [`iter_nth_zero`] in `next`

Closes #9820
This also *slightlyy* modifies the output of `iter_nth`, as I noticed the types' names weren't in backticks

changelog: [`iter_nth_zero`]: No longer lints in implementations of `Iterator::next`
This commit is contained in:
bors 2023-06-21 22:06:12 +00:00
commit 9fa4089410
5 changed files with 54 additions and 23 deletions

View file

@ -1,4 +1,4 @@
error: called `.iter().nth()` on a Vec
error: called `.iter().nth()` on a `Vec`
--> $DIR/iter_nth.rs:34:23
|
LL | let bad_vec = some_vec.iter().nth(3);
@ -23,7 +23,7 @@ LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
|
= help: calling `.get()` is both faster and more readable
error: called `.iter().nth()` on a VecDeque
error: called `.iter().nth()` on a `VecDeque`
--> $DIR/iter_nth.rs:37:29
|
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
@ -31,7 +31,7 @@ LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
|
= help: calling `.get()` is both faster and more readable
error: called `.iter_mut().nth()` on a Vec
error: called `.iter_mut().nth()` on a `Vec`
--> $DIR/iter_nth.rs:42:23
|
LL | let bad_vec = some_vec.iter_mut().nth(3);
@ -47,7 +47,7 @@ LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
|
= help: calling `.get_mut()` is both faster and more readable
error: called `.iter_mut().nth()` on a VecDeque
error: called `.iter_mut().nth()` on a `VecDeque`
--> $DIR/iter_nth.rs:48:29
|
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);

View file

@ -29,3 +29,18 @@ fn main() {
let mut iter2 = s3.iter();
let _unwrapped = iter2.next().unwrap();
}
struct Issue9820;
impl Iterator for Issue9820 {
type Item = ();
fn nth(&mut self, _n: usize) -> Option<Self::Item> {
todo!()
}
// Don't lint in implementations of `next`, as calling `next` in `next` is incorrect
fn next(&mut self) -> Option<Self::Item> {
self.nth(0)
}
}

View file

@ -29,3 +29,18 @@ fn main() {
let mut iter2 = s3.iter();
let _unwrapped = iter2.nth(0).unwrap();
}
struct Issue9820;
impl Iterator for Issue9820 {
type Item = ();
fn nth(&mut self, _n: usize) -> Option<Self::Item> {
todo!()
}
// Don't lint in implementations of `next`, as calling `next` in `next` is incorrect
fn next(&mut self) -> Option<Self::Item> {
self.nth(0)
}
}