|
| 1 | +#![allow(unused_assignments)] |
| 2 | +#![warn(clippy::unnecessary_to_owned)] |
| 3 | + |
| 4 | +#[allow(dead_code)] |
| 5 | +#[derive(Clone, Copy)] |
| 6 | +enum FileType { |
| 7 | + Account, |
| 8 | + PrivateKey, |
| 9 | + Certificate, |
| 10 | +} |
| 11 | + |
| 12 | +fn main() { |
| 13 | + let path = std::path::Path::new("x"); |
| 14 | + |
| 15 | + let _ = check_files(&[(FileType::Account, path)]); |
| 16 | + let _ = check_files_vec(vec![(FileType::Account, path)]); |
| 17 | + |
| 18 | + // negative tests |
| 19 | + let _ = check_files_ref(&[(FileType::Account, path)]); |
| 20 | + let _ = check_files_mut(&[(FileType::Account, path)]); |
| 21 | + let _ = check_files_ref_mut(&[(FileType::Account, path)]); |
| 22 | + let _ = check_files_self_and_arg(&[(FileType::Account, path)]); |
| 23 | + let _ = check_files_mut_path_buf(&[(FileType::Account, std::path::PathBuf::new())]); |
| 24 | + |
| 25 | + check_mut_iteratee_and_modify_inner_variable(); |
| 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 | +} |
| 143 | + |
| 144 | +// Issue 12098 |
| 145 | +// https://github.com/rust-lang/rust-clippy/issues/12098 |
| 146 | +// no message emits |
| 147 | +fn check_mut_iteratee_and_modify_inner_variable() { |
| 148 | + struct Test { |
| 149 | + list: Vec<String>, |
| 150 | + mut_this: bool, |
| 151 | + } |
| 152 | + |
| 153 | + impl Test { |
| 154 | + fn list(&self) -> &[String] { |
| 155 | + &self.list |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + let mut test = Test { |
| 160 | + list: vec![String::from("foo"), String::from("bar")], |
| 161 | + mut_this: false, |
| 162 | + }; |
| 163 | + |
| 164 | + for _item in test.list().to_vec() { |
| 165 | + println!("{}", _item); |
| 166 | + |
| 167 | + test.mut_this = true; |
| 168 | + { |
| 169 | + test.mut_this = true; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +mod issue_12821 { |
| 175 | + fn foo() { |
| 176 | + let v: Vec<_> = "hello".chars().collect(); |
| 177 | + for c in v.iter() { |
| 178 | + //~^ ERROR: unnecessary use of `cloned` |
| 179 | + println!("{c}"); // should not suggest to remove `&` |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + fn bar() { |
| 184 | + let v: Vec<_> = "hello".chars().collect(); |
| 185 | + for c in v.iter() { |
| 186 | + //~^ ERROR: unnecessary use of `cloned` |
| 187 | + let ref_c = c; //~ HELP: remove any references to the binding |
| 188 | + println!("{ref_c}"); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + fn baz() { |
| 193 | + let v: Vec<_> = "hello".chars().enumerate().collect(); |
| 194 | + for (i, c) in v.iter() { |
| 195 | + //~^ ERROR: unnecessary use of `cloned` |
| 196 | + let ref_c = c; //~ HELP: remove any references to the binding |
| 197 | + let ref_i = i; |
| 198 | + println!("{i} {ref_c}"); // should not suggest to remove `&` from `i` |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments