|
| 1 | +// run-rustfix |
| 2 | +#![allow(unused)] |
| 3 | +#![warn(clippy::rewind_instead_of_seek_to_start)] |
| 4 | + |
| 5 | +use std::fs::OpenOptions; |
| 6 | +use std::io::{Read, Seek, SeekFrom, Write}; |
| 7 | + |
| 8 | +struct StructWithSeekMethod {} |
| 9 | + |
| 10 | +impl StructWithSeekMethod { |
| 11 | + fn seek(&mut self, from: SeekFrom) {} |
| 12 | +} |
| 13 | + |
| 14 | +trait MySeekTrait { |
| 15 | + fn seek(&mut self, from: SeekFrom) {} |
| 16 | +} |
| 17 | + |
| 18 | +struct StructWithSeekTrait {} |
| 19 | +impl MySeekTrait for StructWithSeekTrait {} |
| 20 | + |
| 21 | +// This should NOT trigger clippy warning because |
| 22 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 23 | +fn seek_to_start_false_method(t: &mut StructWithSeekMethod) { |
| 24 | + t.seek(SeekFrom::Start(0)); |
| 25 | +} |
| 26 | + |
| 27 | +// This should NOT trigger clippy warning because |
| 28 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 29 | +fn seek_to_start_method_owned_false<T>(mut t: StructWithSeekMethod) { |
| 30 | + t.seek(SeekFrom::Start(0)); |
| 31 | +} |
| 32 | + |
| 33 | +// This should NOT trigger clippy warning because |
| 34 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 35 | +fn seek_to_start_false_trait(t: &mut StructWithSeekTrait) { |
| 36 | + t.seek(SeekFrom::Start(0)); |
| 37 | +} |
| 38 | + |
| 39 | +// This should NOT trigger clippy warning because |
| 40 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 41 | +fn seek_to_start_false_trait_owned<T>(mut t: StructWithSeekTrait) { |
| 42 | + t.seek(SeekFrom::Start(0)); |
| 43 | +} |
| 44 | + |
| 45 | +// This should NOT trigger clippy warning because |
| 46 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 47 | +fn seek_to_start_false_trait_bound<T: MySeekTrait>(t: &mut T) { |
| 48 | + t.seek(SeekFrom::Start(0)); |
| 49 | +} |
| 50 | + |
| 51 | +// This should trigger clippy warning |
| 52 | +fn seek_to_start<T: Seek>(t: &mut T) { |
| 53 | + t.seek(SeekFrom::Start(0)); |
| 54 | +} |
| 55 | + |
| 56 | +// This should trigger clippy warning |
| 57 | +fn owned_seek_to_start<T: Seek>(mut t: T) { |
| 58 | + t.seek(SeekFrom::Start(0)); |
| 59 | +} |
| 60 | + |
| 61 | +// This should NOT trigger clippy warning because |
| 62 | +// it does not seek to start |
| 63 | +fn seek_to_5<T: Seek>(t: &mut T) { |
| 64 | + t.seek(SeekFrom::Start(5)); |
| 65 | +} |
| 66 | + |
| 67 | +// This should NOT trigger clippy warning because |
| 68 | +// it does not seek to start |
| 69 | +fn seek_to_end<T: Seek>(t: &mut T) { |
| 70 | + t.seek(SeekFrom::End(0)); |
| 71 | +} |
| 72 | + |
| 73 | +fn main() { |
| 74 | + let mut f = OpenOptions::new() |
| 75 | + .write(true) |
| 76 | + .read(true) |
| 77 | + .create(true) |
| 78 | + .open("foo.txt") |
| 79 | + .unwrap(); |
| 80 | + |
| 81 | + let mut my_struct_trait = StructWithSeekTrait {}; |
| 82 | + seek_to_start_false_trait_bound(&mut my_struct_trait); |
| 83 | + |
| 84 | + let hello = "Hello!\n"; |
| 85 | + write!(f, "{hello}").unwrap(); |
| 86 | + seek_to_5(&mut f); |
| 87 | + seek_to_end(&mut f); |
| 88 | + seek_to_start(&mut f); |
| 89 | + |
| 90 | + let mut buf = String::new(); |
| 91 | + f.read_to_string(&mut buf).unwrap(); |
| 92 | + |
| 93 | + assert_eq!(&buf, hello); |
| 94 | +} |
0 commit comments