]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/unnecessary_iter_cloned.fixed
Rollup merge of #90498 - joshtriplett:target-tier-policy-draft-updates, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / ui / unnecessary_iter_cloned.fixed
1 // run-rustfix
2
3 #![allow(unused_assignments)]
4 #![warn(clippy::unnecessary_to_owned)]
5
6 #[allow(dead_code)]
7 #[derive(Clone, Copy)]
8 enum FileType {
9     Account,
10     PrivateKey,
11     Certificate,
12 }
13
14 fn main() {
15     let path = std::path::Path::new("x");
16
17     let _ = check_files(&[(FileType::Account, path)]);
18     let _ = check_files_vec(vec![(FileType::Account, path)]);
19
20     // negative tests
21     let _ = check_files_ref(&[(FileType::Account, path)]);
22     let _ = check_files_mut(&[(FileType::Account, path)]);
23     let _ = check_files_ref_mut(&[(FileType::Account, path)]);
24     let _ = check_files_self_and_arg(&[(FileType::Account, path)]);
25     let _ = check_files_mut_path_buf(&[(FileType::Account, std::path::PathBuf::new())]);
26 }
27
28 // `check_files` and its variants are based on:
29 // https://github.com/breard-r/acmed/blob/1f0dcc32aadbc5e52de6d23b9703554c0f925113/acmed/src/storage.rs#L262
30 fn check_files(files: &[(FileType, &std::path::Path)]) -> bool {
31     for (t, path) in files {
32         let other = match get_file_path(t) {
33             Ok(p) => p,
34             Err(_) => {
35                 return false;
36             },
37         };
38         if !path.is_file() || !other.is_file() {
39             return false;
40         }
41     }
42     true
43 }
44
45 fn check_files_vec(files: Vec<(FileType, &std::path::Path)>) -> bool {
46     for (t, path) in files.iter() {
47         let other = match get_file_path(t) {
48             Ok(p) => p,
49             Err(_) => {
50                 return false;
51             },
52         };
53         if !path.is_file() || !other.is_file() {
54             return false;
55         }
56     }
57     true
58 }
59
60 fn check_files_ref(files: &[(FileType, &std::path::Path)]) -> bool {
61     for (ref t, path) in files.iter().copied() {
62         let other = match get_file_path(t) {
63             Ok(p) => p,
64             Err(_) => {
65                 return false;
66             },
67         };
68         if !path.is_file() || !other.is_file() {
69             return false;
70         }
71     }
72     true
73 }
74
75 #[allow(unused_assignments)]
76 fn check_files_mut(files: &[(FileType, &std::path::Path)]) -> bool {
77     for (mut t, path) in files.iter().copied() {
78         t = FileType::PrivateKey;
79         let other = match get_file_path(&t) {
80             Ok(p) => p,
81             Err(_) => {
82                 return false;
83             },
84         };
85         if !path.is_file() || !other.is_file() {
86             return false;
87         }
88     }
89     true
90 }
91
92 fn check_files_ref_mut(files: &[(FileType, &std::path::Path)]) -> bool {
93     for (ref mut t, path) in files.iter().copied() {
94         *t = FileType::PrivateKey;
95         let other = match get_file_path(t) {
96             Ok(p) => p,
97             Err(_) => {
98                 return false;
99             },
100         };
101         if !path.is_file() || !other.is_file() {
102             return false;
103         }
104     }
105     true
106 }
107
108 fn check_files_self_and_arg(files: &[(FileType, &std::path::Path)]) -> bool {
109     for (t, path) in files.iter().copied() {
110         let other = match get_file_path(&t) {
111             Ok(p) => p,
112             Err(_) => {
113                 return false;
114             },
115         };
116         if !path.join(path).is_file() || !other.is_file() {
117             return false;
118         }
119     }
120     true
121 }
122
123 #[allow(unused_assignments)]
124 fn check_files_mut_path_buf(files: &[(FileType, std::path::PathBuf)]) -> bool {
125     for (mut t, path) in files.iter().cloned() {
126         t = FileType::PrivateKey;
127         let other = match get_file_path(&t) {
128             Ok(p) => p,
129             Err(_) => {
130                 return false;
131             },
132         };
133         if !path.is_file() || !other.is_file() {
134             return false;
135         }
136     }
137     true
138 }
139
140 fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> {
141     Ok(std::path::PathBuf::new())
142 }