]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.rs
e3133b0a7a1f078233349b0471753bd6d59f2643
[rust.git] / tests / ui / use_self.rs
1
2
3 #![warn(use_self)]
4 #![allow(dead_code)]
5 #![allow(should_implement_trait)]
6 #![allow(boxed_local)]
7
8
9 fn main() {}
10
11 mod use_self {
12     struct Foo {}
13
14     impl Foo {
15         fn new() -> Foo {
16             Foo {}
17         }
18         fn test() -> Foo {
19             Foo::new()
20         }
21     }
22
23     impl Default for Foo {
24         fn default() -> Foo {
25             Foo::new()
26         }
27     }
28 }
29
30 mod better {
31     struct Foo {}
32
33     impl Foo {
34         fn new() -> Self {
35             Self {}
36         }
37         fn test() -> Self {
38             Self::new()
39         }
40     }
41
42     impl Default for Foo {
43         fn default() -> Self {
44             Self::new()
45         }
46     }
47 }
48
49 //todo the lint does not handle lifetimed struct
50 //the following module should trigger the lint on the third method only
51 mod lifetimes {
52     struct Foo<'a>{foo_str: &'a str}
53
54     impl<'a> Foo<'a> {
55         // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) -> Foo<'b>`
56         fn foo(s: &str) -> Foo {
57             Foo { foo_str: s }
58         }
59         // cannot replace with `Self`, because that's `Foo<'a>`
60         fn bar() -> Foo<'static> {
61             Foo { foo_str: "foo"}
62         }
63
64         // `Self` is applicable here
65         fn clone(&self) -> Foo<'a> {
66             Foo {foo_str: self.foo_str}
67         }
68     }
69 }
70
71 mod traits {
72
73     #![cfg_attr(feature = "cargo-clippy", allow(boxed_local))]
74
75     trait SelfTrait {
76         fn refs(p1: &Self) -> &Self;
77         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
78         fn mut_refs(p1: &mut Self) -> &mut Self;
79         fn nested(p1: Box<Self>, p2: (&u8, &Self));
80         fn vals(r: Self) -> Self;
81     }
82
83     #[derive(Default)]
84     struct Bad;
85
86     impl SelfTrait for Bad {
87         fn refs(p1: &Bad) -> &Bad {
88             p1
89         }
90
91         fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad {
92             p1
93         }
94
95         fn mut_refs(p1: &mut Bad) -> &mut Bad {
96             p1
97         }
98
99         fn nested(_p1: Box<Bad>, _p2: (&u8, &Bad)) {
100         }
101
102         fn vals(_: Bad) -> Bad {
103             Bad::default()
104         }
105     }
106
107     #[derive(Default)]
108     struct Good;
109
110     impl SelfTrait for Good {
111         fn refs(p1: &Self) -> &Self {
112             p1
113         }
114
115         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
116             p1
117         }
118
119         fn mut_refs(p1: &mut Self) -> &mut Self {
120             p1
121         }
122
123         fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {
124         }
125
126         fn vals(_: Self) -> Self {
127             Self::default()
128         }
129     }
130
131     trait NameTrait {
132         fn refs(p1: &u8) -> &u8;
133         fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
134         fn mut_refs(p1: &mut u8) -> &mut u8;
135         fn nested(p1: Box<u8>, p2: (&u8, &u8));
136         fn vals(p1: u8) -> u8;
137     }
138
139     // Using `Self` instead of the type name is OK
140     impl NameTrait for u8 {
141         fn refs(p1: &Self) -> &Self {
142             p1
143         }
144
145         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
146             p1
147         }
148
149         fn mut_refs(p1: &mut Self) -> &mut Self {
150             p1
151         }
152
153         fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {
154         }
155
156         fn vals(_: Self) -> Self {
157             Self::default()
158         }
159     }
160
161     // Check that self arg isn't linted
162     impl Clone for Good {
163         fn clone(&self) -> Self {
164             // Note: Not linted and it wouldn't be valid
165             // because "can't use `Self` as a constructor`
166             Good
167         }
168     }
169 }
170
171 mod issue2894 {
172     trait IntoBytes {
173         fn into_bytes(&self) -> Vec<u8>;
174     }
175
176     // This should not be linted
177     impl IntoBytes for u8 {
178         fn into_bytes(&self) -> Vec<u8> {
179             vec![*self]
180         }
181     }
182 }