]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_clone.rs
Auto merge of #3527 - phansch:update_readme2, r=matthiaskrgr
[rust.git] / tests / ui / redundant_clone.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::redundant_clone)]
11
12 use std::path::Path;
13 use std::ffi::OsString;
14
15 fn main() {
16     let _ = ["lorem", "ipsum"].join(" ").to_string();
17
18     let s = String::from("foo");
19     let _ = s.clone();
20
21     let s = String::from("foo");
22     let _ = s.to_string();
23
24     let s = String::from("foo");
25     let _ = s.to_owned();
26
27     let _ = Path::new("/a/b/").join("c").to_owned();
28
29     let _ = Path::new("/a/b/").join("c").to_path_buf();
30
31     let _ = OsString::new().to_owned();
32
33     let _ = OsString::new().to_os_string();
34
35     // Check that lint level works
36     #[allow(clippy::redundant_clone)] let _ = String::new().to_string();
37
38     let tup = (String::from("foo"),);
39     let _ = tup.0.clone();
40
41     let tup_ref = &(String::from("foo"),);
42     let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
43 }
44
45 #[derive(Clone)]
46 struct Alpha;
47 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
48     if b {
49         (a.clone(), a.clone())
50     } else {
51         (Alpha, a)
52     }
53 }
54
55 struct TypeWithDrop {
56     x: String,
57 }
58
59 impl Drop for TypeWithDrop {
60     fn drop(&mut self) {}
61 }
62
63 fn cannot_move_from_type_with_drop() -> String {
64     let s = TypeWithDrop {
65         x: String::new()
66     };
67     s.x.clone() // removing this `clone()` summons E0509
68 }