fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1//@aux-build:proc_macros.rs 2#![feature(let_chains)] 3#![allow(unused)] 4#![allow( 5 clippy::assign_op_pattern, 6 clippy::blocks_in_if_conditions, 7 clippy::let_and_return, 8 clippy::let_unit_value, 9 clippy::nonminimal_bool, 10 clippy::uninlined_format_args, 11 clippy::useless_vec 12)] 13 14extern crate proc_macros; 15 16use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; 17use std::rc::Rc; 18 19struct SignificantDrop; 20impl std::ops::Drop for SignificantDrop { 21 fn drop(&mut self) { 22 println!("dropped"); 23 } 24} 25 26fn simple() { 27 28 let a = "zero"; 29 30 31 32 let b = 1; 33 let c = 2; 34 35 36 let d: usize = 1; 37 38 39 let e = format!("{}", d); 40} 41 42fn main() { 43 44 let n = 1; 45 let a = match n { 46 1 => "one", 47 _ => { 48 "two" 49 }, 50 }; 51 52 53 let b = if n == 3 { 54 "four" 55 } else { 56 "five" 57 }; 58 59 60 let d = if true { 61 let temp = 5; 62 temp 63 } else { 64 15 65 }; 66 67 68 let e = if true { 69 format!("{} {}", a, b) 70 } else { 71 format!("{}", n) 72 }; 73 74 75 let f = match 1 { 76 1 => "three", 77 _ => return, 78 }; // has semi 79 80 81 let g: usize = if true { 82 5 83 } else { 84 panic!(); 85 }; 86 87 // Drop order only matters if both are significant 88 89 let y = SignificantDrop; 90 let x = 1; 91 92 93 let y = 1; 94 let x = SignificantDrop; 95 96 97 // types that should be considered insignificant 98 let y = 1; 99 let y = "2"; 100 let y = String::new(); 101 let y = vec![3.0]; 102 let y = HashMap::<usize, usize>::new(); 103 let y = BTreeMap::<usize, usize>::new(); 104 let y = HashSet::<usize>::new(); 105 let y = BTreeSet::<usize>::new(); 106 let y = Box::new(4); 107 let x = SignificantDrop; 108} 109 110async fn in_async() -> &'static str { 111 async fn f() -> &'static str { 112 "one" 113 } 114 115 116 let n = 1; 117 let a = match n { 118 1 => f().await, 119 _ => { 120 "two" 121 }, 122 }; 123 124 a 125} 126 127const fn in_const() -> &'static str { 128 const fn f() -> &'static str { 129 "one" 130 } 131 132 133 let n = 1; 134 let a = match n { 135 1 => f(), 136 _ => { 137 "two" 138 }, 139 }; 140 141 a 142} 143 144#[proc_macros::inline_macros] 145fn does_not_lint() { 146 let z; 147 if false { 148 z = 1; 149 } 150 151 let x; 152 let y; 153 if true { 154 x = 1; 155 } else { 156 y = 1; 157 } 158 159 let mut x; 160 if true { 161 x = 5; 162 x = 10 / x; 163 } else { 164 x = 2; 165 } 166 167 let x; 168 let _ = match 1 { 169 1 => x = 10, 170 _ => x = 20, 171 }; 172 173 // using tuples would be possible, but not always preferable 174 let x; 175 let y; 176 if true { 177 x = 1; 178 y = 2; 179 } else { 180 x = 3; 181 y = 4; 182 } 183 184 // could match with a smarter heuristic to avoid multiple assignments 185 let x; 186 if true { 187 let mut y = 5; 188 y = 6; 189 x = y; 190 } else { 191 x = 2; 192 } 193 194 let (x, y); 195 if true { 196 x = 1; 197 } else { 198 x = 2; 199 } 200 y = 3; 201 202 let x; 203 inline!($x = 1;); 204 205 let x; 206 if true { 207 inline!($x = 1;); 208 } else { 209 x = 2; 210 } 211 212 inline!({ 213 let x; 214 x = 1; 215 216 let x; 217 if true { 218 x = 1; 219 } else { 220 x = 2; 221 } 222 }); 223 224 // ignore if-lets - https://github.com/rust-lang/rust-clippy/issues/8613 225 let x; 226 if let Some(n) = Some("v") { 227 x = 1; 228 } else { 229 x = 2; 230 } 231 232 let x; 233 if true && let Some(n) = Some("let chains too") { 234 x = 1; 235 } else { 236 x = 2; 237 } 238 239 // ignore mut bindings 240 // https://github.com/shepmaster/twox-hash/blob/b169c16d86eb8ea4a296b0acb9d00ca7e3c3005f/src/sixty_four.rs#L88-L93 241 // https://github.com/dtolnay/thiserror/blob/21c26903e29cb92ba1a7ff11e82ae2001646b60d/tests/test_generics.rs#L91-L100 242 let mut x: usize; 243 x = 1; 244 x = 2; 245 x = 3; 246 247 // should not move the declaration if `x` has a significant drop, and there 248 // is another binding with a significant drop between it and the first usage 249 let x; 250 let y = SignificantDrop; 251 x = SignificantDrop; 252} 253 254#[rustfmt::skip] 255fn issue8911() -> u32 { 256 let x; 257 match 1 { 258 _ if { x = 1; false } => return 1, 259 _ => return 2, 260 } 261 262 let x; 263 if { x = 1; true } { 264 return 1; 265 } else { 266 return 2; 267 } 268 269 3 270}