]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.rs
Merge pull request #2065 from rust-lang-nursery/cargo_clippy
[rust.git] / tests / ui / use_self.rs
1
2
3 #![warn(use_self)]
4 #![allow(dead_code)]
5 #![allow(should_implement_trait)]
6
7
8 fn main() {}
9
10 mod use_self {
11     struct Foo {}
12
13     impl Foo {
14         fn new() -> Foo {
15             Foo {}
16         }
17         fn test() -> Foo {
18             Foo::new()
19         }
20     }
21
22     impl Default for Foo {
23         fn default() -> Foo {
24             Foo::new()
25         }
26     }
27 }
28
29 mod better {
30     struct Foo {}
31
32     impl Foo {
33         fn new() -> Self {
34             Self {}
35         }
36         fn test() -> Self {
37             Self::new()
38         }
39     }
40
41     impl Default for Foo {
42         fn default() -> Self {
43             Self::new()
44         }
45     }
46 }
47
48 //todo the lint does not handle lifetimed struct
49 //the following module should trigger the lint on the third method only
50 mod lifetimes {
51     struct Foo<'a>{foo_str: &'a str}
52
53     impl<'a> Foo<'a> {
54         // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) -> Foo<'b>`
55         fn foo(s: &str) -> Foo {
56             Foo { foo_str: s }
57         }
58         // cannot replace with `Self`, because that's `Foo<'a>`
59         fn bar() -> Foo<'static> {
60             Foo { foo_str: "foo"}
61         }
62
63         // `Self` is applicable here
64         fn clone(&self) -> Foo<'a> {
65             Foo {foo_str: self.foo_str}
66         }
67     }
68 }