2 // rustfix-only-machine-applicable
4 use std::ffi::OsString;
8 let _s = ["lorem", "ipsum"].join(" ");
10 let s = String::from("foo");
13 let s = String::from("foo");
16 let s = String::from("foo");
19 let _s = Path::new("/a/b/").join("c");
21 let _s = Path::new("/a/b/").join("c");
23 let _s = OsString::new();
25 let _s = OsString::new();
27 // Check that lint level works
28 #[allow(clippy::redundant_clone)]
29 let _s = String::new().to_string();
31 let tup = (String::from("foo"),);
34 let tup_ref = &(String::from("foo"),);
35 let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
38 let x = String::new();
41 let _x = x.clone(); // ok; `x` is borrowed by `y`
46 let x = (String::new(),);
47 let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
49 with_branch(Alpha, true);
50 cannot_double_move(Alpha);
51 cannot_move_from_type_with_drop();
52 borrower_propagation();
60 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
68 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
76 impl Drop for TypeWithDrop {
80 fn cannot_move_from_type_with_drop() -> String {
81 let s = TypeWithDrop { x: String::new() };
82 s.x.clone() // removing this `clone()` summons E0509
85 fn borrower_propagation() {
86 let s = String::new();
87 let t = String::new();
93 let _u = if b() { &s } else { &t };
95 // ok; `s` and `t` are possibly borrowed
103 let _s = s.clone(); // ok
104 let _t = t.clone(); // ok
110 let _ = s.clone(); // ok
113 let _s = s.clone(); // ok
117 use std::convert::identity as id;
119 let _s = s.clone(); // ok, `u` borrows `s`
131 let f = Foo { x: 123 };
137 let f = Foo { x: 123 };
139 let _f = f.clone(); // ok
144 let x = std::path::PathBuf::from("home");
145 let y = x.join("matthias");
146 // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
147 // redundant. (It also does not consume the PathBuf)
149 println!("x: {:?}, y: {:?}", x, y);
151 let mut s = String::new();
152 s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
154 assert_eq!(s, "bar");
158 if let Some(x) = t.clone() {
166 #[allow(clippy::clone_on_copy)]
168 let a: [String; 1] = [String::from("foo")];
169 let _b: String = a[0].clone();
171 let c: [usize; 2] = [2, 3];
172 let _d: usize = c[1].clone();
176 use std::mem::ManuallyDrop;
179 let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
180 let _ = a.clone(); // OK
182 let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));