diff options
| author | Doru-Florin Blanzeanu <blanzeanu.doru@protonmail.com> | 2022-10-16 19:31:03 +0000 |
|---|---|---|
| committer | Doru-Florin Blanzeanu <blanzeanu.doru@protonmail.com> | 2022-10-24 10:43:48 +0000 |
| commit | 8d6ce3177bd5d4d05723dec1cdd869effc926fc3 (patch) | |
| tree | 697ae96dd3b5c5de362dcf0b6c60b2823cb2d848 /tests | |
| parent | 5b09d4e1f7082aff024faf27263f78e7fc7190a2 (diff) | |
| download | rust-8d6ce3177bd5d4d05723dec1cdd869effc926fc3.tar.gz rust-8d6ce3177bd5d4d05723dec1cdd869effc926fc3.zip | |
Add new lint `rewind_instead_of_seek_to_start`
Signed-off-by: Doru-Florin Blanzeanu <blanzeanu.doru@protonmail.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/rewind_instead_of_seek_to_start.fixed | 94 | ||||
| -rw-r--r-- | tests/ui/rewind_instead_of_seek_to_start.rs | 94 | ||||
| -rw-r--r-- | tests/ui/rewind_instead_of_seek_to_start.stderr | 16 |
3 files changed, 204 insertions, 0 deletions
diff --git a/tests/ui/rewind_instead_of_seek_to_start.fixed b/tests/ui/rewind_instead_of_seek_to_start.fixed new file mode 100644 index 00000000000..037a288b69b --- /dev/null +++ b/tests/ui/rewind_instead_of_seek_to_start.fixed @@ -0,0 +1,94 @@ +// run-rustfix +#![allow(unused)] +#![warn(clippy::rewind_instead_of_seek_to_start)] + +use std::fs::OpenOptions; +use std::io::{Read, Seek, SeekFrom, Write}; + +struct StructWithSeekMethod {} + +impl StructWithSeekMethod { + fn seek(&mut self, from: SeekFrom) {} +} + +trait MySeekTrait { + fn seek(&mut self, from: SeekFrom) {} +} + +struct StructWithSeekTrait {} +impl MySeekTrait for StructWithSeekTrait {} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_method(t: &mut StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_method_owned_false<T>(mut t: StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait(t: &mut StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_owned<T>(mut t: StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_bound<T: MySeekTrait>(t: &mut T) { + t.seek(SeekFrom::Start(0)); +} + +// This should trigger clippy warning +fn seek_to_start<T: Seek>(t: &mut T) { + t.rewind(); +} + +// This should trigger clippy warning +fn owned_seek_to_start<T: Seek>(mut t: T) { + t.rewind(); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_5<T: Seek>(t: &mut T) { + t.seek(SeekFrom::Start(5)); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_end<T: Seek>(t: &mut T) { + t.seek(SeekFrom::End(0)); +} + +fn main() { + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let mut my_struct_trait = StructWithSeekTrait {}; + seek_to_start_false_trait_bound(&mut my_struct_trait); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + seek_to_5(&mut f); + seek_to_end(&mut f); + seek_to_start(&mut f); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} diff --git a/tests/ui/rewind_instead_of_seek_to_start.rs b/tests/ui/rewind_instead_of_seek_to_start.rs new file mode 100644 index 00000000000..2622425128d --- /dev/null +++ b/tests/ui/rewind_instead_of_seek_to_start.rs @@ -0,0 +1,94 @@ +// run-rustfix +#![allow(unused)] +#![warn(clippy::rewind_instead_of_seek_to_start)] + +use std::fs::OpenOptions; +use std::io::{Read, Seek, SeekFrom, Write}; + +struct StructWithSeekMethod {} + +impl StructWithSeekMethod { + fn seek(&mut self, from: SeekFrom) {} +} + +trait MySeekTrait { + fn seek(&mut self, from: SeekFrom) {} +} + +struct StructWithSeekTrait {} +impl MySeekTrait for StructWithSeekTrait {} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_method(t: &mut StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_method_owned_false<T>(mut t: StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait(t: &mut StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_owned<T>(mut t: StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_bound<T: MySeekTrait>(t: &mut T) { + t.seek(SeekFrom::Start(0)); +} + +// This should trigger clippy warning +fn seek_to_start<T: Seek>(t: &mut T) { + t.seek(SeekFrom::Start(0)); +} + +// This should trigger clippy warning +fn owned_seek_to_start<T: Seek>(mut t: T) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_5<T: Seek>(t: &mut T) { + t.seek(SeekFrom::Start(5)); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_end<T: Seek>(t: &mut T) { + t.seek(SeekFrom::End(0)); +} + +fn main() { + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let mut my_struct_trait = StructWithSeekTrait {}; + seek_to_start_false_trait_bound(&mut my_struct_trait); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + seek_to_5(&mut f); + seek_to_end(&mut f); + seek_to_start(&mut f); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} diff --git a/tests/ui/rewind_instead_of_seek_to_start.stderr b/tests/ui/rewind_instead_of_seek_to_start.stderr new file mode 100644 index 00000000000..f985471ac05 --- /dev/null +++ b/tests/ui/rewind_instead_of_seek_to_start.stderr @@ -0,0 +1,16 @@ +error: used `seek` to go to the start of the stream + --> $DIR/rewind_instead_of_seek_to_start.rs:53:7 + | +LL | t.seek(SeekFrom::Start(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` + | + = note: `-D clippy::rewind-instead-of-seek-to-start` implied by `-D warnings` + +error: used `seek` to go to the start of the stream + --> $DIR/rewind_instead_of_seek_to_start.rs:58:7 + | +LL | t.seek(SeekFrom::Start(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` + +error: aborting due to 2 previous errors + |
