fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1// rustfix-only-machine-applicable 2#![feature(lint_reasons)] 3#![warn(clippy::redundant_clone)] 4#![allow( 5 clippy::drop_non_drop, 6 clippy::implicit_clone, 7 clippy::uninlined_format_args, 8 clippy::unnecessary_literal_unwrap 9)] 10 11use std::ffi::OsString; 12use std::path::Path; 13 14fn main() { 15 let _s = ["lorem", "ipsum"].join(" "); 16 17 let s = String::from("foo"); 18 let _s = s; 19 20 let s = String::from("foo"); 21 let _s = s; 22 23 let s = String::from("foo"); 24 let _s = s; 25 26 let _s = Path::new("/a/b/").join("c"); 27 28 let _s = Path::new("/a/b/").join("c"); 29 30 let _s = OsString::new(); 31 32 let _s = OsString::new(); 33 34 // Check that lint level works 35 #[allow(clippy::redundant_clone)] 36 let _s = String::new().to_string(); 37 38 // Check that lint level works 39 #[expect(clippy::redundant_clone)] 40 let _s = String::new().to_string(); 41 42 let tup = (String::from("foo"),); 43 let _t = tup.0; 44 45 let tup_ref = &(String::from("foo"),); 46 let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed 47 48 { 49 let x = String::new(); 50 let y = &x; 51 52 let _x = x.clone(); // ok; `x` is borrowed by `y` 53 54 let _ = y.len(); 55 } 56 57 let x = (String::new(),); 58 let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x` 59 60 with_branch(Alpha, true); 61 cannot_double_move(Alpha); 62 cannot_move_from_type_with_drop(); 63 borrower_propagation(); 64 not_consumed(); 65 issue_5405(); 66 manually_drop(); 67 clone_then_move_cloned(); 68 hashmap_neg(); 69 false_negative_5707(); 70} 71 72#[derive(Clone)] 73struct Alpha; 74fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) { 75 if b { (a.clone(), a) } else { (Alpha, a) } 76} 77 78fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) { 79 (a.clone(), a) 80} 81 82struct TypeWithDrop { 83 x: String, 84} 85 86impl Drop for TypeWithDrop { 87 fn drop(&mut self) {} 88} 89 90fn cannot_move_from_type_with_drop() -> String { 91 let s = TypeWithDrop { x: String::new() }; 92 s.x.clone() // removing this `clone()` summons E0509 93} 94 95fn borrower_propagation() { 96 let s = String::new(); 97 let t = String::new(); 98 99 { 100 fn b() -> bool { 101 unimplemented!() 102 } 103 let _u = if b() { &s } else { &t }; 104 105 // ok; `s` and `t` are possibly borrowed 106 let _s = s.clone(); 107 let _t = t.clone(); 108 } 109 110 { 111 let _u = || s.len(); 112 let _v = [&t; 32]; 113 let _s = s.clone(); // ok 114 let _t = t.clone(); // ok 115 } 116 117 { 118 let _u = { 119 let u = Some(&s); 120 let _ = s.clone(); // ok 121 u 122 }; 123 let _s = s.clone(); // ok 124 } 125 126 { 127 use std::convert::identity as id; 128 let _u = id(id(&s)); 129 let _s = s.clone(); // ok, `u` borrows `s` 130 } 131 132 let _s = s; 133 let _t = t; 134 135 #[derive(Clone)] 136 struct Foo { 137 x: usize, 138 } 139 140 { 141 let f = Foo { x: 123 }; 142 let _x = Some(f.x); 143 let _f = f; 144 } 145 146 { 147 let f = Foo { x: 123 }; 148 let _x = &f.x; 149 let _f = f.clone(); // ok 150 } 151} 152 153fn not_consumed() { 154 let x = std::path::PathBuf::from("home"); 155 let y = x.join("matthias"); 156 // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is 157 // redundant. (It also does not consume the PathBuf) 158 159 println!("x: {:?}, y: {:?}", x, y); 160 161 let mut s = String::new(); 162 s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior. 163 s.push_str("bar"); 164 assert_eq!(s, "bar"); 165 166 let t = Some(s); 167 // OK 168 if let Some(x) = t.clone() { 169 println!("{}", x); 170 } 171 if let Some(x) = t { 172 println!("{}", x); 173 } 174} 175 176#[allow(clippy::clone_on_copy)] 177fn issue_5405() { 178 let a: [String; 1] = [String::from("foo")]; 179 let _b: String = a[0].clone(); 180 181 let c: [usize; 2] = [2, 3]; 182 let _d: usize = c[1].clone(); 183} 184 185fn manually_drop() { 186 use std::mem::ManuallyDrop; 187 use std::sync::Arc; 188 189 let a = ManuallyDrop::new(Arc::new("Hello!".to_owned())); 190 let _ = a.clone(); // OK 191 192 let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a)); 193 unsafe { 194 Arc::from_raw(p); 195 Arc::from_raw(p); 196 } 197} 198 199fn clone_then_move_cloned() { 200 // issue #5973 201 let x = Some(String::new()); 202 // ok, x is moved while the clone is in use. 203 assert_eq!(x.clone(), None, "not equal {}", x.unwrap()); 204 205 // issue #5595 206 fn foo<F: Fn()>(_: &Alpha, _: F) {} 207 let x = Alpha; 208 // ok, data is moved while the clone is in use. 209 foo(&x, move || { 210 let _ = x; 211 }); 212 213 // issue #6998 214 struct S(String); 215 impl S { 216 fn m(&mut self) {} 217 } 218 let mut x = S(String::new()); 219 x.0.clone().chars().for_each(|_| x.m()); 220} 221 222fn hashmap_neg() { 223 // issue 5707 224 use std::collections::HashMap; 225 use std::path::PathBuf; 226 227 let p = PathBuf::from("/"); 228 229 let mut h: HashMap<&str, &str> = HashMap::new(); 230 h.insert("orig-p", p.to_str().unwrap()); 231 232 let mut q = p.clone(); 233 q.push("foo"); 234 235 println!("{:?} {}", h, q.display()); 236} 237 238fn false_negative_5707() { 239 fn foo(_x: &Alpha, _y: &mut Alpha) {} 240 241 let x = Alpha; 242 let mut y = Alpha; 243 foo(&x, &mut y); 244 let _z = x.clone(); // pr 7346 can't lint on `x` 245 drop(y); 246}