]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_clone.fixed
Rollup merge of #4509 - sinkuu:redundant_clone_fix, r=llogiq
[rust.git] / tests / ui / redundant_clone.fixed
1 // run-rustfix
2 // rustfix-only-machine-applicable
3 use std::ffi::OsString;
4 use std::path::Path;
5
6 fn main() {
7     let _s = ["lorem", "ipsum"].join(" ");
8
9     let s = String::from("foo");
10     let _s = s;
11
12     let s = String::from("foo");
13     let _s = s;
14
15     let s = String::from("foo");
16     let _s = s;
17
18     let _s = Path::new("/a/b/").join("c");
19
20     let _s = Path::new("/a/b/").join("c");
21
22     let _s = OsString::new();
23
24     let _s = OsString::new();
25
26     // Check that lint level works
27     #[allow(clippy::redundant_clone)]
28     let _s = String::new().to_string();
29
30     let tup = (String::from("foo"),);
31     let _t = tup.0;
32
33     let tup_ref = &(String::from("foo"),);
34     let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
35
36     {
37         let x = String::new();
38         let y = &x;
39
40         let _x = x.clone(); // ok; `x` is borrowed by `y`
41
42         let _ = y.len();
43     }
44
45     let x = (String::new(),);
46     let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
47
48     with_branch(Alpha, true);
49     cannot_move_from_type_with_drop();
50     borrower_propagation();
51 }
52
53 #[derive(Clone)]
54 struct Alpha;
55 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
56     if b {
57         (a.clone(), a)
58     } else {
59         (Alpha, a)
60     }
61 }
62
63 struct TypeWithDrop {
64     x: String,
65 }
66
67 impl Drop for TypeWithDrop {
68     fn drop(&mut self) {}
69 }
70
71 fn cannot_move_from_type_with_drop() -> String {
72     let s = TypeWithDrop { x: String::new() };
73     s.x.clone() // removing this `clone()` summons E0509
74 }
75
76 fn borrower_propagation() {
77     let s = String::new();
78     let t = String::new();
79
80     {
81         fn b() -> bool {
82             unimplemented!()
83         }
84         let _u = if b() { &s } else { &t };
85
86         // ok; `s` and `t` are possibly borrowed
87         let _s = s.clone();
88         let _t = t.clone();
89     }
90
91     {
92         let _u = || s.len();
93         let _v = [&t; 32];
94         let _s = s.clone(); // ok
95         let _t = t.clone(); // ok
96     }
97
98     {
99         let _u = {
100             let u = Some(&s);
101             let _ = s.clone(); // ok
102             u
103         };
104         let _s = s.clone(); // ok
105     }
106
107     {
108         use std::convert::identity as id;
109         let _u = id(id(&s));
110         let _s = s.clone(); // ok, `u` borrows `s`
111     }
112
113     let _s = s;
114     let _t = t;
115
116     #[derive(Clone)]
117     struct Foo {
118         x: usize,
119     }
120
121     {
122         let f = Foo { x: 123 };
123         let _x = Some(f.x);
124         let _f = f;
125     }
126
127     {
128         let f = Foo { x: 123 };
129         let _x = &f.x;
130         let _f = f.clone(); // ok
131     }
132 }