]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_clone.rs
Auto merge of #6802 - camsteffen:dogfood-fix, r=llogiq
[rust.git] / tests / ui / redundant_clone.rs
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(" ").to_string();
10
11     let s = String::from("foo");
12     let _s = s.clone();
13
14     let s = String::from("foo");
15     let _s = s.to_string();
16
17     let s = String::from("foo");
18     let _s = s.to_owned();
19
20     let _s = Path::new("/a/b/").join("c").to_owned();
21
22     let _s = Path::new("/a/b/").join("c").to_path_buf();
23
24     let _s = OsString::new().to_owned();
25
26     let _s = OsString::new().to_os_string();
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.clone();
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 }
58
59 #[derive(Clone)]
60 struct Alpha;
61 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
62     if b {
63         (a.clone(), a.clone())
64     } else {
65         (Alpha, a)
66     }
67 }
68
69 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
70     (a.clone(), a)
71 }
72
73 struct TypeWithDrop {
74     x: String,
75 }
76
77 impl Drop for TypeWithDrop {
78     fn drop(&mut self) {}
79 }
80
81 fn cannot_move_from_type_with_drop() -> String {
82     let s = TypeWithDrop { x: String::new() };
83     s.x.clone() // removing this `clone()` summons E0509
84 }
85
86 fn borrower_propagation() {
87     let s = String::new();
88     let t = String::new();
89
90     {
91         fn b() -> bool {
92             unimplemented!()
93         }
94         let _u = if b() { &s } else { &t };
95
96         // ok; `s` and `t` are possibly borrowed
97         let _s = s.clone();
98         let _t = t.clone();
99     }
100
101     {
102         let _u = || s.len();
103         let _v = [&t; 32];
104         let _s = s.clone(); // ok
105         let _t = t.clone(); // ok
106     }
107
108     {
109         let _u = {
110             let u = Some(&s);
111             let _ = s.clone(); // ok
112             u
113         };
114         let _s = s.clone(); // ok
115     }
116
117     {
118         use std::convert::identity as id;
119         let _u = id(id(&s));
120         let _s = s.clone(); // ok, `u` borrows `s`
121     }
122
123     let _s = s.clone();
124     let _t = t.clone();
125
126     #[derive(Clone)]
127     struct Foo {
128         x: usize,
129     }
130
131     {
132         let f = Foo { x: 123 };
133         let _x = Some(f.x);
134         let _f = f.clone();
135     }
136
137     {
138         let f = Foo { x: 123 };
139         let _x = &f.x;
140         let _f = f.clone(); // ok
141     }
142 }
143
144 fn not_consumed() {
145     let x = std::path::PathBuf::from("home");
146     let y = x.clone().join("matthias");
147     // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
148     // redundant. (It also does not consume the PathBuf)
149
150     println!("x: {:?}, y: {:?}", x, y);
151
152     let mut s = String::new();
153     s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
154     s.push_str("bar");
155     assert_eq!(s, "bar");
156
157     let t = Some(s);
158     // OK
159     if let Some(x) = t.clone() {
160         println!("{}", x);
161     }
162     if let Some(x) = t {
163         println!("{}", x);
164     }
165 }
166
167 #[allow(clippy::clone_on_copy)]
168 fn issue_5405() {
169     let a: [String; 1] = [String::from("foo")];
170     let _b: String = a[0].clone();
171
172     let c: [usize; 2] = [2, 3];
173     let _d: usize = c[1].clone();
174 }
175
176 fn manually_drop() {
177     use std::mem::ManuallyDrop;
178     use std::sync::Arc;
179
180     let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
181     let _ = a.clone(); // OK
182
183     let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
184     unsafe {
185         Arc::from_raw(p);
186         Arc::from_raw(p);
187     }
188 }