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