summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/wrong_self_convention.rs
blob: e3cc90ee222ada2f2e2ca21742f13a03b10ba50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#![warn(clippy::wrong_self_convention)]
#![allow(dead_code)]

fn main() {}

#[derive(Clone, Copy)]
struct Foo;

impl Foo {
    fn as_i32(self) {}
    fn as_u32(&self) {}
    fn into_i32(self) {}
    fn is_i32(self) {}
    fn is_u32(&self) {}
    fn to_i32(self) {}
    fn from_i32(self) {}

    pub fn as_i64(self) {}
    pub fn into_i64(self) {}
    pub fn is_i64(self) {}
    pub fn to_i64(self) {}
    pub fn from_i64(self) {}
    // check whether the lint can be allowed at the function level
    #[allow(clippy::wrong_self_convention)]
    pub fn from_cake(self) {}

    fn as_x<F: AsRef<Self>>(_: F) {}
    fn as_y<F: AsRef<Foo>>(_: F) {}
}

struct Bar;

impl Bar {
    fn as_i32(self) {}
    fn as_u32(&self) {}
    fn into_i32(&self) {}
    fn into_u32(self) {}
    fn is_i32(self) {}
    fn is_u32(&self) {}
    fn to_i32(self) {}
    fn to_u32(&self) {}
    fn from_i32(self) {}

    pub fn as_i64(self) {}
    pub fn into_i64(&self) {}
    pub fn is_i64(self) {}
    pub fn to_i64(self) {}
    pub fn from_i64(self) {}

    // test for false positives
    fn as_(self) {}
    fn into_(&self) {}
    fn is_(self) {}
    fn to_(self) {}
    fn from_(self) {}
    fn to_mut(&mut self) {}
}

// Allow Box<Self>, Rc<Self>, Arc<Self> for methods that take conventionally take Self by value
#[allow(clippy::boxed_local)]
mod issue4293 {
    use std::rc::Rc;
    use std::sync::Arc;

    struct T;

    impl T {
        fn into_s1(self: Box<Self>) {}
        fn into_s2(self: Rc<Self>) {}
        fn into_s3(self: Arc<Self>) {}

        fn into_t1(self: Box<T>) {}
        fn into_t2(self: Rc<T>) {}
        fn into_t3(self: Arc<T>) {}
    }
}

// False positive for async (see #4037)
mod issue4037 {
    pub struct Foo;
    pub struct Bar;

    impl Foo {
        pub async fn into_bar(self) -> Bar {
            Bar
        }
    }
}

// Lint also in trait definition (see #6307)
mod issue6307 {
    trait T: Sized {
        fn as_i32(self) {}
        fn as_u32(&self) {}
        fn into_i32(self) {}
        fn into_i32_ref(&self) {}
        fn into_u32(self) {}
        fn is_i32(self) {}
        fn is_u32(&self) {}
        fn to_i32(self) {}
        fn to_u32(&self) {}
        fn from_i32(self) {}
        // check whether the lint can be allowed at the function level
        #[allow(clippy::wrong_self_convention)]
        fn from_cake(self) {}

        // test for false positives
        fn as_(self) {}
        fn into_(&self) {}
        fn is_(self) {}
        fn to_(self) {}
        fn from_(self) {}
        fn to_mut(&mut self) {}
    }

    trait U {
        fn as_i32(self);
        fn as_u32(&self);
        fn into_i32(self);
        fn into_i32_ref(&self);
        fn into_u32(self);
        fn is_i32(self);
        fn is_u32(&self);
        fn to_i32(self);
        fn to_u32(&self);
        fn from_i32(self);
        // check whether the lint can be allowed at the function level
        #[allow(clippy::wrong_self_convention)]
        fn from_cake(self);

        // test for false positives
        fn as_(self);
        fn into_(&self);
        fn is_(self);
        fn to_(self);
        fn from_(self);
        fn to_mut(&mut self);
    }

    trait C: Copy {
        fn as_i32(self);
        fn as_u32(&self);
        fn into_i32(self);
        fn into_i32_ref(&self);
        fn into_u32(self);
        fn is_i32(self);
        fn is_u32(&self);
        fn to_i32(self);
        fn to_u32(&self);
        fn from_i32(self);
        // check whether the lint can be allowed at the function level
        #[allow(clippy::wrong_self_convention)]
        fn from_cake(self);

        // test for false positives
        fn as_(self);
        fn into_(&self);
        fn is_(self);
        fn to_(self);
        fn from_(self);
        fn to_mut(&mut self);
    }
}

mod issue6727 {
    #[derive(Clone, Copy)]
    struct FooCopy;

    impl FooCopy {
        fn to_u64(self) -> u64 {
            1
        }
        // trigger lint
        fn to_u64_v2(&self) -> u64 {
            1
        }
    }

    struct FooNoCopy;

    impl FooNoCopy {
        // trigger lint
        fn to_u64(self) -> u64 {
            2
        }
        fn to_u64_v2(&self) -> u64 {
            2
        }
    }
}

pub mod issue8142 {
    struct S;

    impl S {
        // Should not lint: "no self at all" is allowed.
        fn is_forty_two(x: u32) -> bool {
            x == 42
        }

        // Should not lint: &self is allowed.
        fn is_test_code(&self) -> bool {
            true
        }
    }
}