3 #![warn(clippy::seek_to_start_instead_of_rewind)]
5 use std::fs::OpenOptions;
6 use std::io::{Read, Seek, SeekFrom, Write};
8 struct StructWithSeekMethod {}
10 impl StructWithSeekMethod {
11 fn seek(&mut self, from: SeekFrom) {}
15 fn seek(&mut self, from: SeekFrom) {}
18 struct StructWithSeekTrait {}
19 impl MySeekTrait for StructWithSeekTrait {}
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));
27 // This should NOT trigger clippy warning because
28 // StructWithSeekMethod does not implement std::io::Seek;
29 fn seek_to_start_method_owned_false(mut t: StructWithSeekMethod) {
30 t.seek(SeekFrom::Start(0));
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));
39 // This should NOT trigger clippy warning because
40 // StructWithSeekMethod does not implement std::io::Seek;
41 fn seek_to_start_false_trait_owned(mut t: StructWithSeekTrait) {
42 t.seek(SeekFrom::Start(0));
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));
51 // This should trigger clippy warning
52 fn seek_to_start<T: Seek>(t: &mut T) {
56 // This should trigger clippy warning
57 fn owned_seek_to_start<T: Seek>(mut t: T) {
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));
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));
73 // This should NOT trigger clippy warning because
75 fn seek_to_start_in_let<T: Seek>(t: &mut T) {
76 let a = t.seek(SeekFrom::Start(0)).unwrap();
80 let mut f = OpenOptions::new()
87 let mut my_struct_trait = StructWithSeekTrait {};
88 seek_to_start_false_trait_bound(&mut my_struct_trait);
90 let hello = "Hello!\n";
91 write!(f, "{hello}").unwrap();
94 seek_to_start(&mut f);
96 let mut buf = String::new();
97 f.read_to_string(&mut buf).unwrap();
99 assert_eq!(&buf, hello);
102 #[clippy::msrv = "1.54"]
104 let mut f = OpenOptions::new()
111 let hello = "Hello!\n";
112 write!(f, "{hello}").unwrap();
114 f.seek(SeekFrom::Start(0));
116 let mut buf = String::new();
117 f.read_to_string(&mut buf).unwrap();
119 assert_eq!(&buf, hello);
122 #[clippy::msrv = "1.55"]
124 let mut f = OpenOptions::new()
131 let hello = "Hello!\n";
132 write!(f, "{hello}").unwrap();
136 let mut buf = String::new();
137 f.read_to_string(&mut buf).unwrap();
139 assert_eq!(&buf, hello);