]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/redundant_clone.rs
Auto merge of #74772 - JohnTitor:add-tests, r=lcnr
[rust.git] / src / tools / clippy / tests / ui / redundant_clone.rs
1 // run-rustfix
2 // rustfix-only-machine-applicable
3
4 use std::ffi::OsString;
5 use std::path::Path;
6
7 fn main() {
8     let _s = ["lorem", "ipsum"].join(" ").to_string();
9
10     let s = String::from("foo");
11     let _s = s.clone();
12
13     let s = String::from("foo");
14     let _s = s.to_string();
15
16     let s = String::from("foo");
17     let _s = s.to_owned();
18
19     let _s = Path::new("/a/b/").join("c").to_owned();
20
21     let _s = Path::new("/a/b/").join("c").to_path_buf();
22
23     let _s = OsString::new().to_owned();
24
25     let _s = OsString::new().to_os_string();
26
27     // Check that lint level works
28     #[allow(clippy::redundant_clone)]
29     let _s = String::new().to_string();
30
31     let tup = (String::from("foo"),);
32     let _t = tup.0.clone();
33
34     let tup_ref = &(String::from("foo"),);
35     let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
36
37     {
38         let x = String::new();
39         let y = &x;
40
41         let _x = x.clone(); // ok; `x` is borrowed by `y`
42
43         let _ = y.len();
44     }
45
46     let x = (String::new(),);
47     let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
48
49     with_branch(Alpha, true);
50     cannot_double_move(Alpha);
51     cannot_move_from_type_with_drop();
52     borrower_propagation();
53     not_consumed();
54     issue_5405();
55 }
56
57 #[derive(Clone)]
58 struct Alpha;
59 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
60     if b {
61         (a.clone(), a.clone())
62     } else {
63         (Alpha, a)
64     }
65 }
66
67 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
68     (a.clone(), a)
69 }
70
71 struct TypeWithDrop {
72     x: String,
73 }
74
75 impl Drop for TypeWithDrop {
76     fn drop(&mut self) {}
77 }
78
79 fn cannot_move_from_type_with_drop() -> String {
80     let s = TypeWithDrop { x: String::new() };
81     s.x.clone() // removing this `clone()` summons E0509
82 }
83
84 fn borrower_propagation() {
85     let s = String::new();
86     let t = String::new();
87
88     {
89         fn b() -> bool {
90             unimplemented!()
91         }
92         let _u = if b() { &s } else { &t };
93
94         // ok; `s` and `t` are possibly borrowed
95         let _s = s.clone();
96         let _t = t.clone();
97     }
98
99     {
100         let _u = || s.len();
101         let _v = [&t; 32];
102         let _s = s.clone(); // ok
103         let _t = t.clone(); // ok
104     }
105
106     {
107         let _u = {
108             let u = Some(&s);
109             let _ = s.clone(); // ok
110             u
111         };
112         let _s = s.clone(); // ok
113     }
114
115     {
116         use std::convert::identity as id;
117         let _u = id(id(&s));
118         let _s = s.clone(); // ok, `u` borrows `s`
119     }
120
121     let _s = s.clone();
122     let _t = t.clone();
123
124     #[derive(Clone)]
125     struct Foo {
126         x: usize,
127     }
128
129     {
130         let f = Foo { x: 123 };
131         let _x = Some(f.x);
132         let _f = f.clone();
133     }
134
135     {
136         let f = Foo { x: 123 };
137         let _x = &f.x;
138         let _f = f.clone(); // ok
139     }
140 }
141
142 fn not_consumed() {
143     let x = std::path::PathBuf::from("home");
144     let y = x.clone().join("matthias");
145     // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
146     // redundant. (It also does not consume the PathBuf)
147
148     println!("x: {:?}, y: {:?}", x, y);
149
150     let mut s = String::new();
151     s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
152     s.push_str("bar");
153     assert_eq!(s, "bar");
154
155     let t = Some(s);
156     // OK
157     if let Some(x) = t.clone() {
158         println!("{}", x);
159     }
160     if let Some(x) = t {
161         println!("{}", x);
162     }
163 }
164
165 #[allow(clippy::clone_on_copy)]
166 fn issue_5405() {
167     let a: [String; 1] = [String::from("foo")];
168     let _b: String = a[0].clone();
169
170     let c: [usize; 2] = [2, 3];
171     let _d: usize = c[1].clone();
172 }