Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2c867ce

Browse files
Add ui tests for unconditional_recursion lint
1 parent bd38ff3 commit 2c867ce

File tree

2 files changed

+430
-0
lines changed

2 files changed

+430
-0
lines changed

tests/ui/unconditional_recursion.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//@no-rustfix
2+
3+
#![warn(clippy::unconditional_recursion)]
4+
5+
enum Foo {
6+
A,
7+
B,
8+
}
9+
10+
impl PartialEq for Foo {
11+
fn ne(&self, other: &Self) -> bool {
12+
//~^ ERROR: function cannot return without recursing
13+
self != other
14+
}
15+
fn eq(&self, other: &Self) -> bool {
16+
//~^ ERROR: function cannot return without recursing
17+
self == other
18+
}
19+
}
20+
21+
enum Foo2 {
22+
A,
23+
B,
24+
}
25+
26+
impl PartialEq for Foo2 {
27+
fn ne(&self, other: &Self) -> bool {
28+
self != &Foo2::B // no error here
29+
}
30+
fn eq(&self, other: &Self) -> bool {
31+
self == &Foo2::B // no error here
32+
}
33+
}
34+
35+
enum Foo3 {
36+
A,
37+
B,
38+
}
39+
40+
impl PartialEq for Foo3 {
41+
fn ne(&self, other: &Self) -> bool {
42+
//~^ ERROR: function cannot return without recursing
43+
self.ne(other)
44+
}
45+
fn eq(&self, other: &Self) -> bool {
46+
//~^ ERROR: function cannot return without recursing
47+
self.eq(other)
48+
}
49+
}
50+
51+
enum Foo4 {
52+
A,
53+
B,
54+
}
55+
56+
impl PartialEq for Foo4 {
57+
fn ne(&self, other: &Self) -> bool {
58+
self.eq(other) // no error
59+
}
60+
fn eq(&self, other: &Self) -> bool {
61+
self.ne(other) // no error
62+
}
63+
}
64+
65+
enum Foo5 {
66+
A,
67+
B,
68+
}
69+
70+
impl Foo5 {
71+
fn a(&self) -> bool {
72+
true
73+
}
74+
}
75+
76+
impl PartialEq for Foo5 {
77+
fn ne(&self, other: &Self) -> bool {
78+
self.a() // no error
79+
}
80+
fn eq(&self, other: &Self) -> bool {
81+
self.a() // no error
82+
}
83+
}
84+
85+
struct S;
86+
87+
// Check the order doesn't matter.
88+
impl PartialEq for S {
89+
fn ne(&self, other: &Self) -> bool {
90+
//~^ ERROR: function cannot return without recursing
91+
other != self
92+
}
93+
fn eq(&self, other: &Self) -> bool {
94+
//~^ ERROR: function cannot return without recursing
95+
other == self
96+
}
97+
}
98+
99+
struct S2;
100+
101+
// Check that if the same element is compared, it's also triggering the lint.
102+
impl PartialEq for S2 {
103+
fn ne(&self, other: &Self) -> bool {
104+
//~^ ERROR: function cannot return without recursing
105+
other != other
106+
}
107+
fn eq(&self, other: &Self) -> bool {
108+
//~^ ERROR: function cannot return without recursing
109+
other == other
110+
}
111+
}
112+
113+
struct S3;
114+
115+
impl PartialEq for S3 {
116+
fn ne(&self, _other: &Self) -> bool {
117+
//~^ ERROR: function cannot return without recursing
118+
self != self
119+
}
120+
fn eq(&self, _other: &Self) -> bool {
121+
//~^ ERROR: function cannot return without recursing
122+
self == self
123+
}
124+
}
125+
126+
fn main() {
127+
// test code goes here
128+
}

0 commit comments

Comments
 (0)