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