]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_clone.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[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 #[derive(Clone)]
38 struct Alpha;
39 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
40     if b {
41         (a.clone(), a.clone())
42     } else {
43         (Alpha, a)
44     }
45 }
46
47 struct TypeWithDrop {
48     x: String,
49 }
50
51 impl Drop for TypeWithDrop {
52     fn drop(&mut self) {}
53 }
54
55 fn cannot_move_from_type_with_drop() -> String {
56     let s = TypeWithDrop { x: String::new() };
57     s.x.clone() // removing this `clone()` summons E0509
58 }