]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/wrong_self_convention.rs
Rollup merge of #102581 - jyn514:src-detection, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / ui / wrong_self_convention.rs
1 #![warn(clippy::wrong_self_convention)]
2 #![allow(dead_code)]
3
4 fn main() {}
5
6 #[derive(Clone, Copy)]
7 struct Foo;
8
9 impl Foo {
10     fn as_i32(self) {}
11     fn as_u32(&self) {}
12     fn into_i32(self) {}
13     fn is_i32(self) {}
14     fn is_u32(&self) {}
15     fn to_i32(self) {}
16     fn from_i32(self) {}
17
18     pub fn as_i64(self) {}
19     pub fn into_i64(self) {}
20     pub fn is_i64(self) {}
21     pub fn to_i64(self) {}
22     pub fn from_i64(self) {}
23     // check whether the lint can be allowed at the function level
24     #[allow(clippy::wrong_self_convention)]
25     pub fn from_cake(self) {}
26
27     fn as_x<F: AsRef<Self>>(_: F) {}
28     fn as_y<F: AsRef<Foo>>(_: F) {}
29 }
30
31 struct Bar;
32
33 impl Bar {
34     fn as_i32(self) {}
35     fn as_u32(&self) {}
36     fn into_i32(&self) {}
37     fn into_u32(self) {}
38     fn is_i32(self) {}
39     fn is_u32(&self) {}
40     fn to_i32(self) {}
41     fn to_u32(&self) {}
42     fn from_i32(self) {}
43
44     pub fn as_i64(self) {}
45     pub fn into_i64(&self) {}
46     pub fn is_i64(self) {}
47     pub fn to_i64(self) {}
48     pub fn from_i64(self) {}
49
50     // test for false positives
51     fn as_(self) {}
52     fn into_(&self) {}
53     fn is_(self) {}
54     fn to_(self) {}
55     fn from_(self) {}
56     fn to_mut(&mut self) {}
57 }
58
59 // Allow Box<Self>, Rc<Self>, Arc<Self> for methods that take conventionally take Self by value
60 #[allow(clippy::boxed_local)]
61 mod issue4293 {
62     use std::rc::Rc;
63     use std::sync::Arc;
64
65     struct T;
66
67     impl T {
68         fn into_s1(self: Box<Self>) {}
69         fn into_s2(self: Rc<Self>) {}
70         fn into_s3(self: Arc<Self>) {}
71
72         fn into_t1(self: Box<T>) {}
73         fn into_t2(self: Rc<T>) {}
74         fn into_t3(self: Arc<T>) {}
75     }
76 }
77
78 // False positive for async (see #4037)
79 mod issue4037 {
80     pub struct Foo;
81     pub struct Bar;
82
83     impl Foo {
84         pub async fn into_bar(self) -> Bar {
85             Bar
86         }
87     }
88 }
89
90 // Lint also in trait definition (see #6307)
91 mod issue6307 {
92     trait T: Sized {
93         fn as_i32(self) {}
94         fn as_u32(&self) {}
95         fn into_i32(self) {}
96         fn into_i32_ref(&self) {}
97         fn into_u32(self) {}
98         fn is_i32(self) {}
99         fn is_u32(&self) {}
100         fn to_i32(self) {}
101         fn to_u32(&self) {}
102         fn from_i32(self) {}
103         // check whether the lint can be allowed at the function level
104         #[allow(clippy::wrong_self_convention)]
105         fn from_cake(self) {}
106
107         // test for false positives
108         fn as_(self) {}
109         fn into_(&self) {}
110         fn is_(self) {}
111         fn to_(self) {}
112         fn from_(self) {}
113         fn to_mut(&mut self) {}
114     }
115
116     trait U {
117         fn as_i32(self);
118         fn as_u32(&self);
119         fn into_i32(self);
120         fn into_i32_ref(&self);
121         fn into_u32(self);
122         fn is_i32(self);
123         fn is_u32(&self);
124         fn to_i32(self);
125         fn to_u32(&self);
126         fn from_i32(self);
127         // check whether the lint can be allowed at the function level
128         #[allow(clippy::wrong_self_convention)]
129         fn from_cake(self);
130
131         // test for false positives
132         fn as_(self);
133         fn into_(&self);
134         fn is_(self);
135         fn to_(self);
136         fn from_(self);
137         fn to_mut(&mut self);
138     }
139
140     trait C: Copy {
141         fn as_i32(self);
142         fn as_u32(&self);
143         fn into_i32(self);
144         fn into_i32_ref(&self);
145         fn into_u32(self);
146         fn is_i32(self);
147         fn is_u32(&self);
148         fn to_i32(self);
149         fn to_u32(&self);
150         fn from_i32(self);
151         // check whether the lint can be allowed at the function level
152         #[allow(clippy::wrong_self_convention)]
153         fn from_cake(self);
154
155         // test for false positives
156         fn as_(self);
157         fn into_(&self);
158         fn is_(self);
159         fn to_(self);
160         fn from_(self);
161         fn to_mut(&mut self);
162     }
163 }
164
165 mod issue6727 {
166     #[derive(Clone, Copy)]
167     struct FooCopy;
168
169     impl FooCopy {
170         fn to_u64(self) -> u64 {
171             1
172         }
173         // trigger lint
174         fn to_u64_v2(&self) -> u64 {
175             1
176         }
177     }
178
179     struct FooNoCopy;
180
181     impl FooNoCopy {
182         // trigger lint
183         fn to_u64(self) -> u64 {
184             2
185         }
186         fn to_u64_v2(&self) -> u64 {
187             2
188         }
189     }
190 }
191
192 pub mod issue8142 {
193     struct S;
194
195     impl S {
196         // Should not lint: "no self at all" is allowed.
197         fn is_forty_two(x: u32) -> bool {
198             x == 42
199         }
200
201         // Should not lint: &self is allowed.
202         fn is_test_code(&self) -> bool {
203             true
204         }
205     }
206 }