]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/seek_to_start_instead_of_rewind.fixed
New upstream version 1.69.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / seek_to_start_instead_of_rewind.fixed
1 // run-rustfix
2 #![allow(unused)]
3 #![warn(clippy::seek_to_start_instead_of_rewind)]
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(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(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.rewind();
54 }
55
56 // This should trigger clippy warning
57 fn owned_seek_to_start<T: Seek>(mut t: T) {
58 t.rewind();
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 // This should NOT trigger clippy warning because
74 // expr is used here
75 fn seek_to_start_in_let<T: Seek>(t: &mut T) {
76 let a = t.seek(SeekFrom::Start(0)).unwrap();
77 }
78
79 fn main() {
80 let mut f = OpenOptions::new()
81 .write(true)
82 .read(true)
83 .create(true)
84 .open("foo.txt")
85 .unwrap();
86
87 let mut my_struct_trait = StructWithSeekTrait {};
88 seek_to_start_false_trait_bound(&mut my_struct_trait);
89
90 let hello = "Hello!\n";
91 write!(f, "{hello}").unwrap();
92 seek_to_5(&mut f);
93 seek_to_end(&mut f);
94 seek_to_start(&mut f);
95
96 let mut buf = String::new();
97 f.read_to_string(&mut buf).unwrap();
98
99 assert_eq!(&buf, hello);
100 }
101
102 #[clippy::msrv = "1.54"]
103 fn msrv_1_54() {
104 let mut f = OpenOptions::new()
105 .write(true)
106 .read(true)
107 .create(true)
108 .open("foo.txt")
109 .unwrap();
110
111 let hello = "Hello!\n";
112 write!(f, "{hello}").unwrap();
113
114 f.seek(SeekFrom::Start(0));
115
116 let mut buf = String::new();
117 f.read_to_string(&mut buf).unwrap();
118
119 assert_eq!(&buf, hello);
120 }
121
122 #[clippy::msrv = "1.55"]
123 fn msrv_1_55() {
124 let mut f = OpenOptions::new()
125 .write(true)
126 .read(true)
127 .create(true)
128 .open("foo.txt")
129 .unwrap();
130
131 let hello = "Hello!\n";
132 write!(f, "{hello}").unwrap();
133
134 f.rewind();
135
136 let mut buf = String::new();
137 f.read_to_string(&mut buf).unwrap();
138
139 assert_eq!(&buf, hello);
140 }