|
| 1 | +// run-rustfix |
| 2 | +#![allow(unused)] |
| 3 | +#![feature(custom_inner_attributes)] |
| 4 | +#![warn(clippy::seek_to_start_instead_of_rewind)] |
| 5 | + |
| 6 | +use std::fs::OpenOptions; |
| 7 | +use std::io::{Read, Seek, SeekFrom, Write}; |
| 8 | + |
| 9 | +struct StructWithSeekMethod {} |
| 10 | + |
| 11 | +impl StructWithSeekMethod { |
| 12 | + fn seek(&mut self, from: SeekFrom) {} |
| 13 | +} |
| 14 | + |
| 15 | +trait MySeekTrait { |
| 16 | + fn seek(&mut self, from: SeekFrom) {} |
| 17 | +} |
| 18 | + |
| 19 | +struct StructWithSeekTrait {} |
| 20 | +impl MySeekTrait for StructWithSeekTrait {} |
| 21 | + |
| 22 | +// This should NOT trigger clippy warning because |
| 23 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 24 | +fn seek_to_start_false_method(t: &mut StructWithSeekMethod) { |
| 25 | + t.seek(SeekFrom::Start(0)); |
| 26 | +} |
| 27 | + |
| 28 | +// This should NOT trigger clippy warning because |
| 29 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 30 | +fn seek_to_start_method_owned_false<T>(mut t: StructWithSeekMethod) { |
| 31 | + t.seek(SeekFrom::Start(0)); |
| 32 | +} |
| 33 | + |
| 34 | +// This should NOT trigger clippy warning because |
| 35 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 36 | +fn seek_to_start_false_trait(t: &mut StructWithSeekTrait) { |
| 37 | + t.seek(SeekFrom::Start(0)); |
| 38 | +} |
| 39 | + |
| 40 | +// This should NOT trigger clippy warning because |
| 41 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 42 | +fn seek_to_start_false_trait_owned<T>(mut t: StructWithSeekTrait) { |
| 43 | + t.seek(SeekFrom::Start(0)); |
| 44 | +} |
| 45 | + |
| 46 | +// This should NOT trigger clippy warning because |
| 47 | +// StructWithSeekMethod does not implement std::io::Seek; |
| 48 | +fn seek_to_start_false_trait_bound<T: MySeekTrait>(t: &mut T) { |
| 49 | + t.seek(SeekFrom::Start(0)); |
| 50 | +} |
| 51 | + |
| 52 | +// This should trigger clippy warning |
| 53 | +fn seek_to_start<T: Seek>(t: &mut T) { |
| 54 | + t.rewind(); |
| 55 | +} |
| 56 | + |
| 57 | +// This should trigger clippy warning |
| 58 | +fn owned_seek_to_start<T: Seek>(mut t: T) { |
| 59 | + t.rewind(); |
| 60 | +} |
| 61 | + |
| 62 | +// This should NOT trigger clippy warning because |
| 63 | +// it does not seek to start |
| 64 | +fn seek_to_5<T: Seek>(t: &mut T) { |
| 65 | + t.seek(SeekFrom::Start(5)); |
| 66 | +} |
| 67 | + |
| 68 | +// This should NOT trigger clippy warning because |
| 69 | +// it does not seek to start |
| 70 | +fn seek_to_end<T: Seek>(t: &mut T) { |
| 71 | + t.seek(SeekFrom::End(0)); |
| 72 | +} |
| 73 | + |
| 74 | +fn main() { |
| 75 | + let mut f = OpenOptions::new() |
| 76 | + .write(true) |
| 77 | + .read(true) |
| 78 | + .create(true) |
| 79 | + .open("foo.txt") |
| 80 | + .unwrap(); |
| 81 | + |
| 82 | + let mut my_struct_trait = StructWithSeekTrait {}; |
| 83 | + seek_to_start_false_trait_bound(&mut my_struct_trait); |
| 84 | + |
| 85 | + let hello = "Hello!\n"; |
| 86 | + write!(f, "{hello}").unwrap(); |
| 87 | + seek_to_5(&mut f); |
| 88 | + seek_to_end(&mut f); |
| 89 | + seek_to_start(&mut f); |
| 90 | + |
| 91 | + let mut buf = String::new(); |
| 92 | + f.read_to_string(&mut buf).unwrap(); |
| 93 | + |
| 94 | + assert_eq!(&buf, hello); |
| 95 | +} |
| 96 | + |
| 97 | +fn msrv_1_54() { |
| 98 | + #![clippy::msrv = "1.54"] |
| 99 | + |
| 100 | + let mut f = OpenOptions::new() |
| 101 | + .write(true) |
| 102 | + .read(true) |
| 103 | + .create(true) |
| 104 | + .open("foo.txt") |
| 105 | + .unwrap(); |
| 106 | + |
| 107 | + let hello = "Hello!\n"; |
| 108 | + write!(f, "{hello}").unwrap(); |
| 109 | + |
| 110 | + f.seek(SeekFrom::Start(0)); |
| 111 | + |
| 112 | + let mut buf = String::new(); |
| 113 | + f.read_to_string(&mut buf).unwrap(); |
| 114 | + |
| 115 | + assert_eq!(&buf, hello); |
| 116 | +} |
| 117 | + |
| 118 | +fn msrv_1_55() { |
| 119 | + #![clippy::msrv = "1.55"] |
| 120 | + |
| 121 | + let mut f = OpenOptions::new() |
| 122 | + .write(true) |
| 123 | + .read(true) |
| 124 | + .create(true) |
| 125 | + .open("foo.txt") |
| 126 | + .unwrap(); |
| 127 | + |
| 128 | + let hello = "Hello!\n"; |
| 129 | + write!(f, "{hello}").unwrap(); |
| 130 | + |
| 131 | + f.rewind(); |
| 132 | + |
| 133 | + let mut buf = String::new(); |
| 134 | + f.read_to_string(&mut buf).unwrap(); |
| 135 | + |
| 136 | + assert_eq!(&buf, hello); |
| 137 | +} |
0 commit comments