about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unconditional_recursion.rs
blob: a51fc567f50c9d666727c6b7b213b5e87bf72466 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//@no-rustfix

#![warn(clippy::unconditional_recursion)]
#![allow(
    clippy::partialeq_ne_impl,
    clippy::default_constructed_unit_structs,
    clippy::only_used_in_recursion
)]

enum Foo {
    A,
    B,
}

impl PartialEq for Foo {
    fn ne(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        self != other
    }
    fn eq(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        self == other
    }
}

enum Foo2 {
    A,
    B,
}

impl PartialEq for Foo2 {
    fn ne(&self, other: &Self) -> bool {
        self != &Foo2::B // no error here
    }
    fn eq(&self, other: &Self) -> bool {
        self == &Foo2::B // no error here
    }
}

enum Foo3 {
    A,
    B,
}

impl PartialEq for Foo3 {
    fn ne(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        self.ne(other)
    }
    fn eq(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        self.eq(other)
    }
}

enum Foo4 {
    A,
    B,
}

impl PartialEq for Foo4 {
    fn ne(&self, other: &Self) -> bool {
        self.eq(other) // no error
    }
    fn eq(&self, other: &Self) -> bool {
        self.ne(other) // no error
    }
}

enum Foo5 {
    A,
    B,
}

impl Foo5 {
    fn a(&self) -> bool {
        true
    }
}

impl PartialEq for Foo5 {
    fn ne(&self, other: &Self) -> bool {
        self.a() // no error
    }
    fn eq(&self, other: &Self) -> bool {
        self.a() // no error
    }
}

struct S;

// Check the order doesn't matter.
impl PartialEq for S {
    fn ne(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        other != self
    }
    fn eq(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        other == self
    }
}

struct S2;

// Check that if the same element is compared, it's also triggering the lint.
impl PartialEq for S2 {
    fn ne(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        other != other
    }
    fn eq(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        other == other
    }
}

struct S3;

impl PartialEq for S3 {
    fn ne(&self, _other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        self != self
    }
    fn eq(&self, _other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        self == self
    }
}

// There should be no warning here!
#[derive(PartialEq)]
enum E {
    A,
    B,
}

#[derive(PartialEq)]
struct Bar<T: PartialEq>(T);

struct S4;

impl PartialEq for S4 {
    fn eq(&self, other: &Self) -> bool {
        // No warning here.
        Bar(self) == Bar(other)
    }
}

macro_rules! impl_partial_eq {
    ($ty:ident) => {
        impl PartialEq for $ty {
            fn eq(&self, other: &Self) -> bool {
                //~^ ERROR: function cannot return without recursing
                self == other
            }
        }
    };
}

struct S5;

impl_partial_eq!(S5);

struct S6 {
    field: String,
}

impl PartialEq for S6 {
    fn eq(&self, other: &Self) -> bool {
        let mine = &self.field;
        let theirs = &other.field;
        mine == theirs // Should not warn!
    }
}

struct S7<'a> {
    field: &'a S7<'a>,
}

impl<'a> PartialEq for S7<'a> {
    fn eq(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        let mine = &self.field;
        let theirs = &other.field;
        mine == theirs
    }
}

struct S8 {
    num: i32,
    field: Option<Box<S8>>,
}

impl PartialEq for S8 {
    fn eq(&self, other: &Self) -> bool {
        if self.num != other.num {
            return false;
        }

        let (this, other) = match (self.field.as_deref(), other.field.as_deref()) {
            (Some(x1), Some(x2)) => (x1, x2),
            (None, None) => return true,
            _ => return false,
        };

        this == other
    }
}

struct S9;

#[allow(clippy::to_string_trait_impl)]
impl std::string::ToString for S9 {
    fn to_string(&self) -> String {
        //~^ ERROR: function cannot return without recursing
        self.to_string()
    }
}

struct S10;

#[allow(clippy::to_string_trait_impl)]
impl std::string::ToString for S10 {
    fn to_string(&self) -> String {
        //~^ ERROR: function cannot return without recursing
        let x = self;
        x.to_string()
    }
}

struct S11;

#[allow(clippy::to_string_trait_impl)]
impl std::string::ToString for S11 {
    fn to_string(&self) -> String {
        //~^ ERROR: function cannot return without recursing
        (self as &Self).to_string()
    }
}

struct S12;

impl std::default::Default for S12 {
    fn default() -> Self {
        Self::new()
    }
}

impl S12 {
    fn new() -> Self {
        //~^ ERROR: function cannot return without recursing
        Self::default()
    }

    fn bar() -> Self {
        // Should not warn!
        Self::default()
    }
}

#[derive(Default)]
struct S13 {
    f: u32,
}

impl S13 {
    fn new() -> Self {
        // Should not warn!
        Self::default()
    }
}

struct S14 {
    field: String,
}

impl PartialEq for S14 {
    fn eq(&self, other: &Self) -> bool {
        // Should not warn!
        self.field.eq(&other.field)
    }
}

struct S15<'a> {
    field: &'a S15<'a>,
}

impl PartialEq for S15<'_> {
    fn eq(&self, other: &Self) -> bool {
        //~^ ERROR: function cannot return without recursing
        let mine = &self.field;
        let theirs = &other.field;
        mine.eq(theirs)
    }
}

mod issue12154 {
    struct MyBox<T>(T);

    impl<T> std::ops::Deref for MyBox<T> {
        type Target = T;
        fn deref(&self) -> &T {
            &self.0
        }
    }

    impl<T: PartialEq> PartialEq for MyBox<T> {
        fn eq(&self, other: &Self) -> bool {
            (**self).eq(&**other)
        }
    }

    // Not necessarily related to the issue but another FP from the http crate that was fixed with it:
    // https://docs.rs/http/latest/src/http/header/name.rs.html#1424
    // We used to simply peel refs from the LHS and RHS, so we couldn't differentiate
    // between `PartialEq<T> for &T` and `PartialEq<&T> for T` impls.
    #[derive(PartialEq)]
    struct HeaderName;
    impl<'a> PartialEq<&'a HeaderName> for HeaderName {
        fn eq(&self, other: &&'a HeaderName) -> bool {
            *self == **other
        }
    }

    impl<'a> PartialEq<HeaderName> for &'a HeaderName {
        fn eq(&self, other: &HeaderName) -> bool {
            *other == *self
        }
    }

    // Issue #12181 but also fixed by the same PR
    struct Foo;

    impl Foo {
        fn as_str(&self) -> &str {
            "Foo"
        }
    }

    impl PartialEq for Foo {
        fn eq(&self, other: &Self) -> bool {
            self.as_str().eq(other.as_str())
        }
    }

    impl<T> PartialEq<T> for Foo
    where
        for<'a> &'a str: PartialEq<T>,
    {
        fn eq(&self, other: &T) -> bool {
            (&self.as_str()).eq(other)
        }
    }
}

// From::from -> Into::into -> From::from
struct BadFromTy1<'a>(&'a ());
struct BadIntoTy1<'b>(&'b ());
impl<'a> From<BadFromTy1<'a>> for BadIntoTy1<'static> {
    fn from(f: BadFromTy1<'a>) -> Self {
        f.into()
    }
}

// Using UFCS syntax
struct BadFromTy2<'a>(&'a ());
struct BadIntoTy2<'b>(&'b ());
impl<'a> From<BadFromTy2<'a>> for BadIntoTy2<'static> {
    fn from(f: BadFromTy2<'a>) -> Self {
        Into::into(f)
    }
}

// Different Into impl (<i16 as Into<i32>>), so no infinite recursion
struct BadFromTy3;
impl From<BadFromTy3> for i32 {
    fn from(f: BadFromTy3) -> Self {
        Into::into(1i16)
    }
}

// A conditional return that ends the recursion
struct BadFromTy4;
impl From<BadFromTy4> for i32 {
    fn from(f: BadFromTy4) -> Self {
        if true {
            return 42;
        }
        f.into()
    }
}

// Types differ in refs, don't lint
impl From<&BadFromTy4> for i32 {
    fn from(f: &BadFromTy4) -> Self {
        BadFromTy4.into()
    }
}

fn main() {}