diff options
| author | Icxolu <10486322+Icxolu@users.noreply.github.com> | 2023-05-04 22:44:35 +0200 |
|---|---|---|
| committer | Icxolu <10486322+Icxolu@users.noreply.github.com> | 2023-05-11 22:25:14 +0200 |
| commit | a8834bc46ab948c8978b5fcbe0a7aa4760783338 (patch) | |
| tree | 96f787fe5fceab4e10c393f1eb9709c45dbd743f /tests | |
| parent | c56dd3d4c02bc4be32490988a15fa317dc13a2fa (diff) | |
| download | rust-a8834bc46ab948c8978b5fcbe0a7aa4760783338.tar.gz rust-a8834bc46ab948c8978b5fcbe0a7aa4760783338.zip | |
add lint `manual_next_back`
checks for manual reverse iteration (`.rev().next()`) of a `DoubleEndedIterator`
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/manual_next_back.fixed | 36 | ||||
| -rw-r--r-- | tests/ui/manual_next_back.rs | 36 | ||||
| -rw-r--r-- | tests/ui/manual_next_back.stderr | 16 |
3 files changed, 88 insertions, 0 deletions
diff --git a/tests/ui/manual_next_back.fixed b/tests/ui/manual_next_back.fixed new file mode 100644 index 00000000000..e8a47063ad6 --- /dev/null +++ b/tests/ui/manual_next_back.fixed @@ -0,0 +1,36 @@ +//@run-rustfix + +#![allow(unused)] +#![warn(clippy::manual_next_back)] + +struct FakeIter(std::ops::Range<i32>); + +impl FakeIter { + fn rev(self) -> Self { + self + } + + fn next(&self) {} +} + +impl DoubleEndedIterator for FakeIter { + fn next_back(&mut self) -> Option<Self::Item> { + self.0.next_back() + } +} + +impl Iterator for FakeIter { + type Item = i32; + fn next(&mut self) -> Option<Self::Item> { + self.0.next() + } +} + +fn main() { + // should not lint + FakeIter(0..10).rev().next(); + + // should lint + let _ = (0..10).next_back().unwrap(); + let _ = "something".bytes().next_back(); +} diff --git a/tests/ui/manual_next_back.rs b/tests/ui/manual_next_back.rs new file mode 100644 index 00000000000..9ec89242241 --- /dev/null +++ b/tests/ui/manual_next_back.rs @@ -0,0 +1,36 @@ +//@run-rustfix + +#![allow(unused)] +#![warn(clippy::manual_next_back)] + +struct FakeIter(std::ops::Range<i32>); + +impl FakeIter { + fn rev(self) -> Self { + self + } + + fn next(&self) {} +} + +impl DoubleEndedIterator for FakeIter { + fn next_back(&mut self) -> Option<Self::Item> { + self.0.next_back() + } +} + +impl Iterator for FakeIter { + type Item = i32; + fn next(&mut self) -> Option<Self::Item> { + self.0.next() + } +} + +fn main() { + // should not lint + FakeIter(0..10).rev().next(); + + // should lint + let _ = (0..10).rev().next().unwrap(); + let _ = "something".bytes().rev().next(); +} diff --git a/tests/ui/manual_next_back.stderr b/tests/ui/manual_next_back.stderr new file mode 100644 index 00000000000..94ccaa9e4cc --- /dev/null +++ b/tests/ui/manual_next_back.stderr @@ -0,0 +1,16 @@ +error: manual backwards iteration + --> $DIR/manual_next_back.rs:34:20 + | +LL | let _ = (0..10).rev().next().unwrap(); + | ^^^^^^^^^^^^^ help: use: `.next_back()` + | + = note: `-D clippy::manual-next-back` implied by `-D warnings` + +error: manual backwards iteration + --> $DIR/manual_next_back.rs:35:32 + | +LL | let _ = "something".bytes().rev().next(); + | ^^^^^^^^^^^^^ help: use: `.next_back()` + +error: aborting due to 2 previous errors + |
