summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/seek_to_start_instead_of_rewind.fixed
blob: 713cff604a1d7d6028c367f5efcf2e4c2de552fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// run-rustfix
#![allow(unused)]
#![warn(clippy::seek_to_start_instead_of_rewind)]

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));
}

// This should NOT trigger clippy warning because
// expr is used here
fn seek_to_start_in_let<T: Seek>(t: &mut T) {
    let a = t.seek(SeekFrom::Start(0)).unwrap();
}

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);
}

#[clippy::msrv = "1.54"]
fn msrv_1_54() {
    let mut f = OpenOptions::new()
        .write(true)
        .read(true)
        .create(true)
        .open("foo.txt")
        .unwrap();

    let hello = "Hello!\n";
    write!(f, "{hello}").unwrap();

    f.seek(SeekFrom::Start(0));

    let mut buf = String::new();
    f.read_to_string(&mut buf).unwrap();

    assert_eq!(&buf, hello);
}

#[clippy::msrv = "1.55"]
fn msrv_1_55() {
    let mut f = OpenOptions::new()
        .write(true)
        .read(true)
        .create(true)
        .open("foo.txt")
        .unwrap();

    let hello = "Hello!\n";
    write!(f, "{hello}").unwrap();

    f.rewind();

    let mut buf = String::new();
    f.read_to_string(&mut buf).unwrap();

    assert_eq!(&buf, hello);
}