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