Skip to content

Commit 8698c31

Browse files
authored
Don't lint CStr literals, do lint float literals in redundant_guards (#13698)
Two changes to `redundant_guards`: - Lint float literals. We used to do that before but that was changed in #11305 because at the time there was a future compat warning and it was planned to make pattern matching floats a hard error. In rust-lang/rust#116284 it was decided to actually remove the lint and only make matching `NAN` specifically a hard error. The `NAN` part isn't relevant/important here because this PR only changes what literals are warned and `f64::NAN` isn't a literal, but I've added a test anyway to make sure we continue to not lint there. - Don't lint CStr literals because that can't be a pattern right now (fixes #13681) changelog: none
2 parents 5f05ce4 + 8859847 commit 8698c31

File tree

4 files changed

+78
-41
lines changed

4 files changed

+78
-41
lines changed

Diff for: clippy_lints/src/matches/redundant_guards.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,6 @@ fn emit_redundant_guards<'tcx>(
243243
}
244244

245245
/// Checks if the given `Expr` can also be represented as a `Pat`.
246-
///
247-
/// All literals generally also work as patterns, however float literals are special.
248-
/// They are currently (as of 2023/08/08) still allowed in patterns, but that will become
249-
/// an error in the future, and rustc already actively warns against this (see rust#41620),
250-
/// so we don't consider those as usable within patterns for linting purposes.
251246
fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
252247
for_each_expr_without_closures(expr, |expr| {
253248
if match expr.kind {
@@ -267,7 +262,7 @@ fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
267262
| ExprKind::Tup(..)
268263
| ExprKind::Struct(..)
269264
| ExprKind::Unary(UnOp::Neg, _) => true,
270-
ExprKind::Lit(lit) if !matches!(lit.node, LitKind::Float(..)) => true,
265+
ExprKind::Lit(lit) if !matches!(lit.node, LitKind::CStr(..)) => true,
271266
_ => false,
272267
} {
273268
return ControlFlow::Continue(());

Diff for: tests/ui/redundant_guards.fixed

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build:proc_macros.rs
22
#![feature(if_let_guard)]
3-
#![allow(clippy::no_effect, unused, clippy::single_match)]
3+
#![allow(clippy::no_effect, unused, clippy::single_match, invalid_nan_comparisons)]
44
#![warn(clippy::redundant_guards)]
55

66
#[macro_use]
@@ -19,15 +19,24 @@ struct FloatWrapper(f32);
1919

2020
fn issue11304() {
2121
match 0.1 {
22-
x if x == 0.0 => todo!(),
22+
0.0 => todo!(),
23+
// Pattern matching NAN is illegal
24+
x if x == f64::NAN => todo!(),
2325
_ => todo!(),
2426
}
2527
match FloatWrapper(0.1) {
26-
x if x == FloatWrapper(0.0) => todo!(),
28+
FloatWrapper(0.0) => todo!(),
2729
_ => todo!(),
2830
}
2931
}
3032

33+
fn issue13681() {
34+
match c"hi" {
35+
x if x == c"hi" => (),
36+
_ => (),
37+
}
38+
}
39+
3140
fn main() {
3241
let c = C(1, 2);
3342
match c {

Diff for: tests/ui/redundant_guards.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build:proc_macros.rs
22
#![feature(if_let_guard)]
3-
#![allow(clippy::no_effect, unused, clippy::single_match)]
3+
#![allow(clippy::no_effect, unused, clippy::single_match, invalid_nan_comparisons)]
44
#![warn(clippy::redundant_guards)]
55

66
#[macro_use]
@@ -20,6 +20,8 @@ struct FloatWrapper(f32);
2020
fn issue11304() {
2121
match 0.1 {
2222
x if x == 0.0 => todo!(),
23+
// Pattern matching NAN is illegal
24+
x if x == f64::NAN => todo!(),
2325
_ => todo!(),
2426
}
2527
match FloatWrapper(0.1) {
@@ -28,6 +30,13 @@ fn issue11304() {
2830
}
2931
}
3032

33+
fn issue13681() {
34+
match c"hi" {
35+
x if x == c"hi" => (),
36+
_ => (),
37+
}
38+
}
39+
3140
fn main() {
3241
let c = C(1, 2);
3342
match c {

Diff for: tests/ui/redundant_guards.stderr

+55-31
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11
error: redundant guard
2-
--> tests/ui/redundant_guards.rs:34:20
2+
--> tests/ui/redundant_guards.rs:22:14
33
|
4-
LL | C(x, y) if let 1 = y => ..,
5-
| ^^^^^^^^^
4+
LL | x if x == 0.0 => todo!(),
5+
| ^^^^^^^^
66
|
77
= note: `-D clippy::redundant-guards` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::redundant_guards)]`
99
help: try
1010
|
11+
LL - x if x == 0.0 => todo!(),
12+
LL + 0.0 => todo!(),
13+
|
14+
15+
error: redundant guard
16+
--> tests/ui/redundant_guards.rs:28:14
17+
|
18+
LL | x if x == FloatWrapper(0.0) => todo!(),
19+
| ^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: try
22+
|
23+
LL - x if x == FloatWrapper(0.0) => todo!(),
24+
LL + FloatWrapper(0.0) => todo!(),
25+
|
26+
27+
error: redundant guard
28+
--> tests/ui/redundant_guards.rs:43:20
29+
|
30+
LL | C(x, y) if let 1 = y => ..,
31+
| ^^^^^^^^^
32+
|
33+
help: try
34+
|
1135
LL - C(x, y) if let 1 = y => ..,
1236
LL + C(x, 1) => ..,
1337
|
1438

1539
error: redundant guard
16-
--> tests/ui/redundant_guards.rs:40:20
40+
--> tests/ui/redundant_guards.rs:49:20
1741
|
1842
LL | Some(x) if matches!(x, Some(1) if true) => ..,
1943
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -24,7 +48,7 @@ LL | Some(Some(1)) if true => ..,
2448
| ~~~~~~~ ~~~~~~~
2549

2650
error: redundant guard
27-
--> tests/ui/redundant_guards.rs:41:20
51+
--> tests/ui/redundant_guards.rs:50:20
2852
|
2953
LL | Some(x) if matches!(x, Some(1)) => {
3054
| ^^^^^^^^^^^^^^^^^^^^
@@ -36,7 +60,7 @@ LL + Some(Some(1)) => {
3660
|
3761

3862
error: redundant guard
39-
--> tests/ui/redundant_guards.rs:45:20
63+
--> tests/ui/redundant_guards.rs:54:20
4064
|
4165
LL | Some(x) if let Some(1) = x => ..,
4266
| ^^^^^^^^^^^^^^^
@@ -48,7 +72,7 @@ LL + Some(Some(1)) => ..,
4872
|
4973

5074
error: redundant guard
51-
--> tests/ui/redundant_guards.rs:46:20
75+
--> tests/ui/redundant_guards.rs:55:20
5276
|
5377
LL | Some(x) if x == Some(2) => ..,
5478
| ^^^^^^^^^^^^
@@ -60,7 +84,7 @@ LL + Some(Some(2)) => ..,
6084
|
6185

6286
error: redundant guard
63-
--> tests/ui/redundant_guards.rs:47:20
87+
--> tests/ui/redundant_guards.rs:56:20
6488
|
6589
LL | Some(x) if Some(2) == x => ..,
6690
| ^^^^^^^^^^^^
@@ -72,7 +96,7 @@ LL + Some(Some(2)) => ..,
7296
|
7397

7498
error: redundant guard
75-
--> tests/ui/redundant_guards.rs:72:20
99+
--> tests/ui/redundant_guards.rs:81:20
76100
|
77101
LL | B { e } if matches!(e, Some(A(2))) => ..,
78102
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -84,7 +108,7 @@ LL + B { e: Some(A(2)) } => ..,
84108
|
85109

86110
error: redundant guard
87-
--> tests/ui/redundant_guards.rs:109:20
111+
--> tests/ui/redundant_guards.rs:118:20
88112
|
89113
LL | E::A(y) if y == "not from an or pattern" => {},
90114
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -96,7 +120,7 @@ LL + E::A("not from an or pattern") => {},
96120
|
97121

98122
error: redundant guard
99-
--> tests/ui/redundant_guards.rs:116:14
123+
--> tests/ui/redundant_guards.rs:125:14
100124
|
101125
LL | x if matches!(x, Some(0)) => ..,
102126
| ^^^^^^^^^^^^^^^^^^^^
@@ -108,7 +132,7 @@ LL + Some(0) => ..,
108132
|
109133

110134
error: redundant guard
111-
--> tests/ui/redundant_guards.rs:123:14
135+
--> tests/ui/redundant_guards.rs:132:14
112136
|
113137
LL | i if i == -1 => {},
114138
| ^^^^^^^
@@ -120,7 +144,7 @@ LL + -1 => {},
120144
|
121145

122146
error: redundant guard
123-
--> tests/ui/redundant_guards.rs:124:14
147+
--> tests/ui/redundant_guards.rs:133:14
124148
|
125149
LL | i if i == 1 => {},
126150
| ^^^^^^
@@ -132,7 +156,7 @@ LL + 1 => {},
132156
|
133157

134158
error: redundant guard
135-
--> tests/ui/redundant_guards.rs:186:28
159+
--> tests/ui/redundant_guards.rs:195:28
136160
|
137161
LL | Some(ref x) if x == &1 => {},
138162
| ^^^^^^^
@@ -144,7 +168,7 @@ LL + Some(1) => {},
144168
|
145169

146170
error: redundant guard
147-
--> tests/ui/redundant_guards.rs:187:28
171+
--> tests/ui/redundant_guards.rs:196:28
148172
|
149173
LL | Some(ref x) if &1 == x => {},
150174
| ^^^^^^^
@@ -156,7 +180,7 @@ LL + Some(1) => {},
156180
|
157181

158182
error: redundant guard
159-
--> tests/ui/redundant_guards.rs:188:28
183+
--> tests/ui/redundant_guards.rs:197:28
160184
|
161185
LL | Some(ref x) if let &2 = x => {},
162186
| ^^^^^^^^^^
@@ -168,7 +192,7 @@ LL + Some(2) => {},
168192
|
169193

170194
error: redundant guard
171-
--> tests/ui/redundant_guards.rs:189:28
195+
--> tests/ui/redundant_guards.rs:198:28
172196
|
173197
LL | Some(ref x) if matches!(x, &3) => {},
174198
| ^^^^^^^^^^^^^^^
@@ -180,7 +204,7 @@ LL + Some(3) => {},
180204
|
181205

182206
error: redundant guard
183-
--> tests/ui/redundant_guards.rs:209:32
207+
--> tests/ui/redundant_guards.rs:218:32
184208
|
185209
LL | B { ref c, .. } if c == &1 => {},
186210
| ^^^^^^^
@@ -192,7 +216,7 @@ LL + B { c: 1, .. } => {},
192216
|
193217

194218
error: redundant guard
195-
--> tests/ui/redundant_guards.rs:210:32
219+
--> tests/ui/redundant_guards.rs:219:32
196220
|
197221
LL | B { ref c, .. } if &1 == c => {},
198222
| ^^^^^^^
@@ -204,7 +228,7 @@ LL + B { c: 1, .. } => {},
204228
|
205229

206230
error: redundant guard
207-
--> tests/ui/redundant_guards.rs:211:32
231+
--> tests/ui/redundant_guards.rs:220:32
208232
|
209233
LL | B { ref c, .. } if let &1 = c => {},
210234
| ^^^^^^^^^^
@@ -216,7 +240,7 @@ LL + B { c: 1, .. } => {},
216240
|
217241

218242
error: redundant guard
219-
--> tests/ui/redundant_guards.rs:212:32
243+
--> tests/ui/redundant_guards.rs:221:32
220244
|
221245
LL | B { ref c, .. } if matches!(c, &1) => {},
222246
| ^^^^^^^^^^^^^^^
@@ -228,7 +252,7 @@ LL + B { c: 1, .. } => {},
228252
|
229253

230254
error: redundant guard
231-
--> tests/ui/redundant_guards.rs:222:26
255+
--> tests/ui/redundant_guards.rs:231:26
232256
|
233257
LL | Some(Some(x)) if x.is_empty() => {},
234258
| ^^^^^^^^^^^^
@@ -240,7 +264,7 @@ LL + Some(Some("")) => {},
240264
|
241265

242266
error: redundant guard
243-
--> tests/ui/redundant_guards.rs:233:26
267+
--> tests/ui/redundant_guards.rs:242:26
244268
|
245269
LL | Some(Some(x)) if x.is_empty() => {},
246270
| ^^^^^^^^^^^^
@@ -252,7 +276,7 @@ LL + Some(Some([])) => {},
252276
|
253277

254278
error: redundant guard
255-
--> tests/ui/redundant_guards.rs:238:26
279+
--> tests/ui/redundant_guards.rs:247:26
256280
|
257281
LL | Some(Some(x)) if x.is_empty() => {},
258282
| ^^^^^^^^^^^^
@@ -264,7 +288,7 @@ LL + Some(Some([])) => {},
264288
|
265289

266290
error: redundant guard
267-
--> tests/ui/redundant_guards.rs:249:26
291+
--> tests/ui/redundant_guards.rs:258:26
268292
|
269293
LL | Some(Some(x)) if x.starts_with(&[]) => {},
270294
| ^^^^^^^^^^^^^^^^^^
@@ -276,7 +300,7 @@ LL + Some(Some([..])) => {},
276300
|
277301

278302
error: redundant guard
279-
--> tests/ui/redundant_guards.rs:254:26
303+
--> tests/ui/redundant_guards.rs:263:26
280304
|
281305
LL | Some(Some(x)) if x.starts_with(&[1]) => {},
282306
| ^^^^^^^^^^^^^^^^^^^
@@ -288,7 +312,7 @@ LL + Some(Some([1, ..])) => {},
288312
|
289313

290314
error: redundant guard
291-
--> tests/ui/redundant_guards.rs:259:26
315+
--> tests/ui/redundant_guards.rs:268:26
292316
|
293317
LL | Some(Some(x)) if x.starts_with(&[1, 2]) => {},
294318
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -300,7 +324,7 @@ LL + Some(Some([1, 2, ..])) => {},
300324
|
301325

302326
error: redundant guard
303-
--> tests/ui/redundant_guards.rs:264:26
327+
--> tests/ui/redundant_guards.rs:273:26
304328
|
305329
LL | Some(Some(x)) if x.ends_with(&[1, 2]) => {},
306330
| ^^^^^^^^^^^^^^^^^^^^
@@ -312,7 +336,7 @@ LL + Some(Some([.., 1, 2])) => {},
312336
|
313337

314338
error: redundant guard
315-
--> tests/ui/redundant_guards.rs:286:18
339+
--> tests/ui/redundant_guards.rs:295:18
316340
|
317341
LL | y if y.is_empty() => {},
318342
| ^^^^^^^^^^^^
@@ -324,7 +348,7 @@ LL + "" => {},
324348
|
325349

326350
error: redundant guard
327-
--> tests/ui/redundant_guards.rs:305:22
351+
--> tests/ui/redundant_guards.rs:314:22
328352
|
329353
LL | y if y.is_empty() => {},
330354
| ^^^^^^^^^^^^
@@ -335,5 +359,5 @@ LL - y if y.is_empty() => {},
335359
LL + "" => {},
336360
|
337361

338-
error: aborting due to 28 previous errors
362+
error: aborting due to 30 previous errors
339363

0 commit comments

Comments
 (0)