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