2 // rustfix-only-machine-applicable
3 #![feature(lint_reasons)]
4 #![allow(clippy::drop_non_drop, clippy::implicit_clone, clippy::uninlined_format_args)]
6 use std::ffi::OsString;
10 let _s = ["lorem", "ipsum"].join(" ");
12 let s = String::from("foo");
15 let s = String::from("foo");
18 let s = String::from("foo");
21 let _s = Path::new("/a/b/").join("c");
23 let _s = Path::new("/a/b/").join("c");
25 let _s = OsString::new();
27 let _s = OsString::new();
29 // Check that lint level works
30 #[allow(clippy::redundant_clone)]
31 let _s = String::new().to_string();
33 // Check that lint level works
34 #[expect(clippy::redundant_clone)]
35 let _s = String::new().to_string();
37 let tup = (String::from("foo"),);
40 let tup_ref = &(String::from("foo"),);
41 let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
44 let x = String::new();
47 let _x = x.clone(); // ok; `x` is borrowed by `y`
52 let x = (String::new(),);
53 let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
55 with_branch(Alpha, true);
56 cannot_double_move(Alpha);
57 cannot_move_from_type_with_drop();
58 borrower_propagation();
62 clone_then_move_cloned();
64 false_negative_5707();
69 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
70 if b { (a.clone(), a) } else { (Alpha, a) }
73 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
81 impl Drop for TypeWithDrop {
85 fn cannot_move_from_type_with_drop() -> String {
86 let s = TypeWithDrop { x: String::new() };
87 s.x.clone() // removing this `clone()` summons E0509
90 fn borrower_propagation() {
91 let s = String::new();
92 let t = String::new();
98 let _u = if b() { &s } else { &t };
100 // ok; `s` and `t` are possibly borrowed
108 let _s = s.clone(); // ok
109 let _t = t.clone(); // ok
115 let _ = s.clone(); // ok
118 let _s = s.clone(); // ok
122 use std::convert::identity as id;
124 let _s = s.clone(); // ok, `u` borrows `s`
136 let f = Foo { x: 123 };
142 let f = Foo { x: 123 };
144 let _f = f.clone(); // ok
149 let x = std::path::PathBuf::from("home");
150 let y = x.join("matthias");
151 // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
152 // redundant. (It also does not consume the PathBuf)
154 println!("x: {:?}, y: {:?}", x, y);
156 let mut s = String::new();
157 s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
159 assert_eq!(s, "bar");
163 if let Some(x) = t.clone() {
171 #[allow(clippy::clone_on_copy)]
173 let a: [String; 1] = [String::from("foo")];
174 let _b: String = a[0].clone();
176 let c: [usize; 2] = [2, 3];
177 let _d: usize = c[1].clone();
181 use std::mem::ManuallyDrop;
184 let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
185 let _ = a.clone(); // OK
187 let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
194 fn clone_then_move_cloned() {
196 let x = Some(String::new());
197 // ok, x is moved while the clone is in use.
198 assert_eq!(x.clone(), None, "not equal {}", x.unwrap());
201 fn foo<F: Fn()>(_: &Alpha, _: F) {}
203 // ok, data is moved while the clone is in use.
213 let mut x = S(String::new());
214 x.0.clone().chars().for_each(|_| x.m());
219 use std::collections::HashMap;
220 use std::path::PathBuf;
222 let p = PathBuf::from("/");
224 let mut h: HashMap<&str, &str> = HashMap::new();
225 h.insert("orig-p", p.to_str().unwrap());
227 let mut q = p.clone();
230 println!("{:?} {}", h, q.display());
233 fn false_negative_5707() {
234 fn foo(_x: &Alpha, _y: &mut Alpha) {}
239 let _z = x.clone(); // pr 7346 can't lint on `x`