]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_clone.rs
Fix false-positive of redundant_clone and move to clippy::perf
[rust.git] / tests / ui / redundant_clone.rs
1 #![warn(clippy::redundant_clone)]
2
3 use std::ffi::OsString;
4 use std::path::Path;
5
6 fn main() {
7     let _ = ["lorem", "ipsum"].join(" ").to_string();
8
9     let s = String::from("foo");
10     let _ = s.clone();
11
12     let s = String::from("foo");
13     let _ = s.to_string();
14
15     let s = String::from("foo");
16     let _ = s.to_owned();
17
18     let _ = Path::new("/a/b/").join("c").to_owned();
19
20     let _ = Path::new("/a/b/").join("c").to_path_buf();
21
22     let _ = OsString::new().to_owned();
23
24     let _ = OsString::new().to_os_string();
25
26     // Check that lint level works
27     #[allow(clippy::redundant_clone)]
28     let _ = String::new().to_string();
29
30     let tup = (String::from("foo"),);
31     let _ = tup.0.clone();
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.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
49 #[derive(Clone)]
50 struct Alpha;
51 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
52     if b {
53         (a.clone(), a.clone())
54     } else {
55         (Alpha, a)
56     }
57 }
58
59 struct TypeWithDrop {
60     x: String,
61 }
62
63 impl Drop for TypeWithDrop {
64     fn drop(&mut self) {}
65 }
66
67 fn cannot_move_from_type_with_drop() -> String {
68     let s = TypeWithDrop { x: String::new() };
69     s.x.clone() // removing this `clone()` summons E0509
70 }
71
72 fn borrower_propagation() {
73     let s = String::new();
74     let t = String::new();
75
76     {
77         fn b() -> bool {
78             unimplemented!()
79         }
80         let u = if b() { &s } else { &t };
81
82         // ok; `s` and `t` are possibly borrowed
83         let _ = s.clone();
84         let _ = t.clone();
85     }
86
87     {
88         let u = || s.len();
89         let v = [&t; 32];
90         let _ = s.clone(); // ok
91         let _ = t.clone(); // ok
92     }
93
94     {
95         let u = {
96             let u = Some(&s);
97             let _ = s.clone(); // ok
98             u
99         };
100         let _ = s.clone(); // ok
101     }
102
103     {
104         use std::convert::identity as id;
105         let u = id(id(&s));
106         let _ = s.clone(); // ok, `u` borrows `s`
107     }
108
109     let _ = s.clone();
110     let _ = t.clone();
111
112     #[derive(Clone)]
113     struct Foo {
114         x: usize,
115     }
116
117     {
118         let f = Foo { x: 123 };
119         let _x = Some(f.x);
120         let _f = f.clone();
121     }
122
123     {
124         let f = Foo { x: 123 };
125         let _x = &f.x;
126         let _f = f.clone(); // ok
127     }
128 }