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