about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unnecessary_iter_cloned.fixed
blob: 61f2e3745ad05191168a83984dec5580cc47d1c8 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#![allow(unused_assignments, clippy::uninlined_format_args)]
#![warn(clippy::unnecessary_to_owned)]

#[allow(dead_code)]
#[derive(Clone, Copy)]
enum FileType {
    Account,
    PrivateKey,
    Certificate,
}

fn main() {
    let path = std::path::Path::new("x");

    let _ = check_files(&[(FileType::Account, path)]);
    let _ = check_files_vec(vec![(FileType::Account, path)]);

    // negative tests
    let _ = check_files_ref(&[(FileType::Account, path)]);
    let _ = check_files_mut(&[(FileType::Account, path)]);
    let _ = check_files_ref_mut(&[(FileType::Account, path)]);
    let _ = check_files_self_and_arg(&[(FileType::Account, path)]);
    let _ = check_files_mut_path_buf(&[(FileType::Account, std::path::PathBuf::new())]);

    check_mut_iteratee_and_modify_inner_variable();
}

// `check_files` and its variants are based on:
// https://github.com/breard-r/acmed/blob/1f0dcc32aadbc5e52de6d23b9703554c0f925113/acmed/src/storage.rs#L262
fn check_files(files: &[(FileType, &std::path::Path)]) -> bool {
    for (t, path) in files {
        //~^ unnecessary_to_owned
        let other = match get_file_path(t) {
            Ok(p) => p,
            Err(_) => {
                return false;
            },
        };
        if !path.is_file() || !other.is_file() {
            return false;
        }
    }
    true
}

fn check_files_vec(files: Vec<(FileType, &std::path::Path)>) -> bool {
    for (t, path) in files.iter() {
        //~^ unnecessary_to_owned
        let other = match get_file_path(t) {
            Ok(p) => p,
            Err(_) => {
                return false;
            },
        };
        if !path.is_file() || !other.is_file() {
            return false;
        }
    }
    true
}

fn check_files_ref(files: &[(FileType, &std::path::Path)]) -> bool {
    for (ref t, path) in files.iter().copied() {
        let other = match get_file_path(t) {
            Ok(p) => p,
            Err(_) => {
                return false;
            },
        };
        if !path.is_file() || !other.is_file() {
            return false;
        }
    }
    true
}

#[allow(unused_assignments)]
fn check_files_mut(files: &[(FileType, &std::path::Path)]) -> bool {
    for (mut t, path) in files.iter().copied() {
        t = FileType::PrivateKey;
        let other = match get_file_path(&t) {
            Ok(p) => p,
            Err(_) => {
                return false;
            },
        };
        if !path.is_file() || !other.is_file() {
            return false;
        }
    }
    true
}

fn check_files_ref_mut(files: &[(FileType, &std::path::Path)]) -> bool {
    for (ref mut t, path) in files.iter().copied() {
        *t = FileType::PrivateKey;
        let other = match get_file_path(t) {
            Ok(p) => p,
            Err(_) => {
                return false;
            },
        };
        if !path.is_file() || !other.is_file() {
            return false;
        }
    }
    true
}

fn check_files_self_and_arg(files: &[(FileType, &std::path::Path)]) -> bool {
    for (t, path) in files.iter().copied() {
        let other = match get_file_path(&t) {
            Ok(p) => p,
            Err(_) => {
                return false;
            },
        };
        if !path.join(path).is_file() || !other.is_file() {
            return false;
        }
    }
    true
}

#[allow(unused_assignments)]
fn check_files_mut_path_buf(files: &[(FileType, std::path::PathBuf)]) -> bool {
    for (mut t, path) in files.iter().cloned() {
        t = FileType::PrivateKey;
        let other = match get_file_path(&t) {
            Ok(p) => p,
            Err(_) => {
                return false;
            },
        };
        if !path.is_file() || !other.is_file() {
            return false;
        }
    }
    true
}

fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> {
    Ok(std::path::PathBuf::new())
}

// Issue 12098
// https://github.com/rust-lang/rust-clippy/issues/12098
// no message emits
fn check_mut_iteratee_and_modify_inner_variable() {
    struct Test {
        list: Vec<String>,
        mut_this: bool,
    }

    impl Test {
        fn list(&self) -> &[String] {
            &self.list
        }
    }

    let mut test = Test {
        list: vec![String::from("foo"), String::from("bar")],
        mut_this: false,
    };

    for _item in test.list().to_vec() {
        println!("{}", _item);

        test.mut_this = true;
        {
            test.mut_this = true;
        }
    }
}

mod issue_12821 {
    fn foo() {
        let v: Vec<_> = "hello".chars().collect();
        for c in v.iter() {
            //~^ unnecessary_to_owned

            println!("{c}"); // should not suggest to remove `&`
        }
    }

    fn bar() {
        let v: Vec<_> = "hello".chars().collect();
        for c in v.iter() {
            //~^ unnecessary_to_owned

            let ref_c = c;
            println!("{ref_c}");
        }
    }

    fn baz() {
        let v: Vec<_> = "hello".chars().enumerate().collect();
        for (i, c) in v.iter() {
            //~^ unnecessary_to_owned

            let ref_c = c;
            let ref_i = i;
            println!("{i} {ref_c}"); // should not suggest to remove `&` from `i`
        }
    }
}