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

Commit 8f40d09

Browse files
committed
Add tests for const comparisons that compare two different types
1 parent 08e1333 commit 8f40d09

File tree

2 files changed

+115
-26
lines changed

2 files changed

+115
-26
lines changed

tests/ui/double_const_comparisons.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,37 @@
88
const STATUS_BAD_REQUEST: u16 = 400;
99
const STATUS_SERVER_ERROR: u16 = 500;
1010

11+
struct Status {
12+
code: u16,
13+
}
14+
15+
impl PartialEq<u16> for Status {
16+
fn eq(&self, other: &u16) -> bool {
17+
self.code == *other
18+
}
19+
}
20+
21+
impl PartialOrd<u16> for Status {
22+
fn partial_cmp(&self, other: &u16) -> Option<std::cmp::Ordering> {
23+
self.code.partial_cmp(other)
24+
}
25+
}
26+
27+
impl PartialEq<Status> for u16 {
28+
fn eq(&self, other: &Status) -> bool {
29+
*self == other.code
30+
}
31+
}
32+
33+
impl PartialOrd<Status> for u16 {
34+
fn partial_cmp(&self, other: &Status) -> Option<std::cmp::Ordering> {
35+
self.partial_cmp(&other.code)
36+
}
37+
}
38+
1139
fn main() {
1240
let status_code = 500; // Value doesn't matter for the lint
41+
let status = Status { code: status_code };
1342

1443
status_code >= 400 && status_code < 500; // Correct
1544
status_code <= 400 && status_code > 500;
@@ -22,12 +51,24 @@ fn main() {
2251
status_code <= u16::MIN + 1 && status_code > STATUS_SERVER_ERROR;
2352
status_code < STATUS_SERVER_ERROR && status_code > STATUS_SERVER_ERROR;
2453

54+
// Comparing two different types, via the `impl PartialOrd<u16> for Status`
55+
status < { 400 } && status > { 500 };
56+
status < STATUS_BAD_REQUEST && status > STATUS_SERVER_ERROR;
57+
status <= u16::MIN + 1 && status > STATUS_SERVER_ERROR;
58+
status < STATUS_SERVER_ERROR && status > STATUS_SERVER_ERROR;
59+
2560
// Yoda conditions
2661
500 <= status_code && 600 > status_code; // Correct
2762
500 <= status_code && status_code <= 600; // Correct
2863
500 >= status_code && 600 < status_code; // Incorrect
2964
500 >= status_code && status_code > 600; // Incorrect
3065

66+
// Yoda conditions, comparing two different types
67+
500 <= status && 600 > status; // Correct
68+
500 <= status && status <= 600; // Correct
69+
500 >= status && 600 < status; // Incorrect
70+
500 >= status && status > 600; // Incorrect
71+
3172
// Expressions where one of the sides has no effect
3273
status_code < 200 && status_code <= 299;
3374
status_code > 200 && status_code >= 299;
Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: boolean expression will never evaluate to 'true'
2-
--> $DIR/double_const_comparisons.rs:15:5
2+
--> $DIR/double_const_comparisons.rs:44:5
33
|
44
LL | status_code <= 400 && status_code > 500;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,173 +8,221 @@ LL | status_code <= 400 && status_code > 500;
88
= note: `-D clippy::impossible-double-const-comparisons` implied by `-D warnings`
99

1010
error: boolean expression will never evaluate to 'true'
11-
--> $DIR/double_const_comparisons.rs:16:5
11+
--> $DIR/double_const_comparisons.rs:45:5
1212
|
1313
LL | status_code > 500 && status_code < 400;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= note: since `500` > `400`, the expression evaluates to false for any value of `status_code`
1717

1818
error: boolean expression will never evaluate to 'true'
19-
--> $DIR/double_const_comparisons.rs:17:5
19+
--> $DIR/double_const_comparisons.rs:46:5
2020
|
2121
LL | status_code < 500 && status_code > 500;
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= note: `status_code` cannot simultaneously be greater than and less than `500`
2525

2626
error: boolean expression will never evaluate to 'true'
27-
--> $DIR/double_const_comparisons.rs:20:5
27+
--> $DIR/double_const_comparisons.rs:49:5
2828
|
2929
LL | status_code < { 400 } && status_code > { 500 };
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3131
|
3232
= note: since `{ 400 }` < `{ 500 }`, the expression evaluates to false for any value of `status_code`
3333

3434
error: boolean expression will never evaluate to 'true'
35-
--> $DIR/double_const_comparisons.rs:21:5
35+
--> $DIR/double_const_comparisons.rs:50:5
3636
|
3737
LL | status_code < STATUS_BAD_REQUEST && status_code > STATUS_SERVER_ERROR;
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939
|
4040
= note: since `STATUS_BAD_REQUEST` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status_code`
4141

4242
error: boolean expression will never evaluate to 'true'
43-
--> $DIR/double_const_comparisons.rs:22:5
43+
--> $DIR/double_const_comparisons.rs:51:5
4444
|
4545
LL | status_code <= u16::MIN + 1 && status_code > STATUS_SERVER_ERROR;
4646
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4747
|
4848
= note: since `u16::MIN + 1` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status_code`
4949

5050
error: boolean expression will never evaluate to 'true'
51-
--> $DIR/double_const_comparisons.rs:23:5
51+
--> $DIR/double_const_comparisons.rs:52:5
5252
|
5353
LL | status_code < STATUS_SERVER_ERROR && status_code > STATUS_SERVER_ERROR;
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5555
|
5656
= note: `status_code` cannot simultaneously be greater than and less than `STATUS_SERVER_ERROR`
5757

5858
error: boolean expression will never evaluate to 'true'
59-
--> $DIR/double_const_comparisons.rs:28:5
59+
--> $DIR/double_const_comparisons.rs:55:5
60+
|
61+
LL | status < { 400 } && status > { 500 };
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
|
64+
= note: since `{ 400 }` < `{ 500 }`, the expression evaluates to false for any value of `status`
65+
66+
error: boolean expression will never evaluate to 'true'
67+
--> $DIR/double_const_comparisons.rs:56:5
68+
|
69+
LL | status < STATUS_BAD_REQUEST && status > STATUS_SERVER_ERROR;
70+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
|
72+
= note: since `STATUS_BAD_REQUEST` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status`
73+
74+
error: boolean expression will never evaluate to 'true'
75+
--> $DIR/double_const_comparisons.rs:57:5
76+
|
77+
LL | status <= u16::MIN + 1 && status > STATUS_SERVER_ERROR;
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
|
80+
= note: since `u16::MIN + 1` < `STATUS_SERVER_ERROR`, the expression evaluates to false for any value of `status`
81+
82+
error: boolean expression will never evaluate to 'true'
83+
--> $DIR/double_const_comparisons.rs:58:5
84+
|
85+
LL | status < STATUS_SERVER_ERROR && status > STATUS_SERVER_ERROR;
86+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87+
|
88+
= note: `status` cannot simultaneously be greater than and less than `STATUS_SERVER_ERROR`
89+
90+
error: boolean expression will never evaluate to 'true'
91+
--> $DIR/double_const_comparisons.rs:63:5
6092
|
6193
LL | 500 >= status_code && 600 < status_code; // Incorrect
6294
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6395
|
6496
= note: since `500` < `600`, the expression evaluates to false for any value of `status_code`
6597

6698
error: boolean expression will never evaluate to 'true'
67-
--> $DIR/double_const_comparisons.rs:29:5
99+
--> $DIR/double_const_comparisons.rs:64:5
68100
|
69101
LL | 500 >= status_code && status_code > 600; // Incorrect
70102
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71103
|
72104
= note: since `500` < `600`, the expression evaluates to false for any value of `status_code`
73105

106+
error: boolean expression will never evaluate to 'true'
107+
--> $DIR/double_const_comparisons.rs:69:5
108+
|
109+
LL | 500 >= status && 600 < status; // Incorrect
110+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111+
|
112+
= note: since `500` < `600`, the expression evaluates to false for any value of `status`
113+
114+
error: boolean expression will never evaluate to 'true'
115+
--> $DIR/double_const_comparisons.rs:70:5
116+
|
117+
LL | 500 >= status && status > 600; // Incorrect
118+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119+
|
120+
= note: since `500` < `600`, the expression evaluates to false for any value of `status`
121+
74122
error: right-hand side of `&&` operator has no effect
75-
--> $DIR/double_const_comparisons.rs:32:5
123+
--> $DIR/double_const_comparisons.rs:73:5
76124
|
77125
LL | status_code < 200 && status_code <= 299;
78126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79127
|
80128
note: `if `status_code < 200` evaluates to true, status_code <= 299` will always evaluate to true as well
81-
--> $DIR/double_const_comparisons.rs:32:23
129+
--> $DIR/double_const_comparisons.rs:73:23
82130
|
83131
LL | status_code < 200 && status_code <= 299;
84132
| ^^^^^^^^^^^^^^^^^^^^^
85133
= note: `-D clippy::ineffective-double-const-comparisons` implied by `-D warnings`
86134

87135
error: left-hand side of `&&` operator has no effect
88-
--> $DIR/double_const_comparisons.rs:33:5
136+
--> $DIR/double_const_comparisons.rs:74:5
89137
|
90138
LL | status_code > 200 && status_code >= 299;
91139
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92140
|
93141
note: `if `status_code >= 299` evaluates to true, status_code > 200` will always evaluate to true as well
94-
--> $DIR/double_const_comparisons.rs:33:5
142+
--> $DIR/double_const_comparisons.rs:74:5
95143
|
96144
LL | status_code > 200 && status_code >= 299;
97145
| ^^^^^^^^^^^^^^^^^^^^^
98146

99147
error: left-hand side of `&&` operator has no effect
100-
--> $DIR/double_const_comparisons.rs:35:5
148+
--> $DIR/double_const_comparisons.rs:76:5
101149
|
102150
LL | status_code >= 500 && status_code > 500; // Useless left
103151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104152
|
105153
note: `if `status_code > 500` evaluates to true, status_code >= 500` will always evaluate to true as well
106-
--> $DIR/double_const_comparisons.rs:35:5
154+
--> $DIR/double_const_comparisons.rs:76:5
107155
|
108156
LL | status_code >= 500 && status_code > 500; // Useless left
109157
| ^^^^^^^^^^^^^^^^^^^^^^
110158

111159
error: right-hand side of `&&` operator has no effect
112-
--> $DIR/double_const_comparisons.rs:36:5
160+
--> $DIR/double_const_comparisons.rs:77:5
113161
|
114162
LL | status_code > 500 && status_code >= 500; // Useless right
115163
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116164
|
117165
note: `if `status_code > 500` evaluates to true, status_code >= 500` will always evaluate to true as well
118-
--> $DIR/double_const_comparisons.rs:36:23
166+
--> $DIR/double_const_comparisons.rs:77:23
119167
|
120168
LL | status_code > 500 && status_code >= 500; // Useless right
121169
| ^^^^^^^^^^^^^^^^^^^^^
122170

123171
error: left-hand side of `&&` operator has no effect
124-
--> $DIR/double_const_comparisons.rs:37:5
172+
--> $DIR/double_const_comparisons.rs:78:5
125173
|
126174
LL | status_code <= 500 && status_code < 500; // Useless left
127175
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128176
|
129177
note: `if `status_code < 500` evaluates to true, status_code <= 500` will always evaluate to true as well
130-
--> $DIR/double_const_comparisons.rs:37:5
178+
--> $DIR/double_const_comparisons.rs:78:5
131179
|
132180
LL | status_code <= 500 && status_code < 500; // Useless left
133181
| ^^^^^^^^^^^^^^^^^^^^^^
134182

135183
error: right-hand side of `&&` operator has no effect
136-
--> $DIR/double_const_comparisons.rs:38:5
184+
--> $DIR/double_const_comparisons.rs:79:5
137185
|
138186
LL | status_code < 500 && status_code <= 500; // Useless right
139187
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140188
|
141189
note: `if `status_code < 500` evaluates to true, status_code <= 500` will always evaluate to true as well
142-
--> $DIR/double_const_comparisons.rs:38:23
190+
--> $DIR/double_const_comparisons.rs:79:23
143191
|
144192
LL | status_code < 500 && status_code <= 500; // Useless right
145193
| ^^^^^^^^^^^^^^^^^^^^^
146194

147195
error: boolean expression will never evaluate to 'true'
148-
--> $DIR/double_const_comparisons.rs:42:5
196+
--> $DIR/double_const_comparisons.rs:83:5
149197
|
150198
LL | name < "Jennifer" && name > "Shannon";
151199
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152200
|
153201
= note: since `"Jennifer"` < `"Shannon"`, the expression evaluates to false for any value of `name`
154202

155203
error: boolean expression will never evaluate to 'true'
156-
--> $DIR/double_const_comparisons.rs:45:5
204+
--> $DIR/double_const_comparisons.rs:86:5
157205
|
158206
LL | numbers < [3, 4] && numbers > [5, 6];
159207
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
160208
|
161209
= note: since `[3, 4]` < `[5, 6]`, the expression evaluates to false for any value of `numbers`
162210

163211
error: boolean expression will never evaluate to 'true'
164-
--> $DIR/double_const_comparisons.rs:48:5
212+
--> $DIR/double_const_comparisons.rs:89:5
165213
|
166214
LL | letter < 'b' && letter > 'c';
167215
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168216
|
169217
= note: since `'b'` < `'c'`, the expression evaluates to false for any value of `letter`
170218

171219
error: boolean expression will never evaluate to 'true'
172-
--> $DIR/double_const_comparisons.rs:51:5
220+
--> $DIR/double_const_comparisons.rs:92:5
173221
|
174222
LL | area < std::f32::consts::E && area > std::f32::consts::PI;
175223
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
176224
|
177225
= note: since `std::f32::consts::E` < `std::f32::consts::PI`, the expression evaluates to false for any value of `area`
178226

179-
error: aborting due to 19 previous errors
227+
error: aborting due to 25 previous errors
180228

0 commit comments

Comments
 (0)