Skip to content

Commit 7c595b4

Browse files
committed
Auto merge of #11305 - y21:issue11304, r=Centri3
[`redundant_guards`]: don't lint on float literals Fixes #11304 changelog: [`redundant_guards`]: don't lint on float literals r? `@Centri3` i figured you are probably a good reviewer for this since you implemented the lint ^^
2 parents 84d2896 + b615650 commit 7c595b4

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

clippy_lints/src/matches/redundant_guards.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::path_to_local;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::visitors::{for_each_expr, is_local_used};
5+
use rustc_ast::LitKind;
56
use rustc_errors::Applicability;
67
use rustc_hir::def::{DefKind, Res};
78
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, Guard, MatchSource, Node, Pat, PatKind};
@@ -160,6 +161,11 @@ fn emit_redundant_guards<'tcx>(
160161
}
161162

162163
/// Checks if the given `Expr` can also be represented as a `Pat`.
164+
///
165+
/// All literals generally also work as patterns, however float literals are special.
166+
/// They are currently (as of 2023/08/08) still allowed in patterns, but that will become
167+
/// an error in the future, and rustc already actively warns against this (see rust#41620),
168+
/// so we don't consider those as usable within patterns for linting purposes.
163169
fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
164170
for_each_expr(expr, |expr| {
165171
if match expr.kind {
@@ -177,8 +183,8 @@ fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
177183
ExprKind::AddrOf(..)
178184
| ExprKind::Array(..)
179185
| ExprKind::Tup(..)
180-
| ExprKind::Struct(..)
181-
| ExprKind::Lit(..) => true,
186+
| ExprKind::Struct(..) => true,
187+
ExprKind::Lit(lit) if !matches!(lit.node, LitKind::Float(..)) => true,
182188
_ => false,
183189
} {
184190
return ControlFlow::Continue(());

tests/ui/redundant_guards.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ struct B {
1515

1616
struct C(u32, u32);
1717

18+
#[derive(PartialEq)]
19+
struct FloatWrapper(f32);
20+
fn issue11304() {
21+
match 0.1 {
22+
x if x == 0.0 => todo!(),
23+
_ => todo!(),
24+
}
25+
match FloatWrapper(0.1) {
26+
x if x == FloatWrapper(0.0) => todo!(),
27+
_ => todo!(),
28+
}
29+
}
30+
1831
fn main() {
1932
let c = C(1, 2);
2033
match c {

tests/ui/redundant_guards.rs

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ struct B {
1515

1616
struct C(u32, u32);
1717

18+
#[derive(PartialEq)]
19+
struct FloatWrapper(f32);
20+
fn issue11304() {
21+
match 0.1 {
22+
x if x == 0.0 => todo!(),
23+
_ => todo!(),
24+
}
25+
match FloatWrapper(0.1) {
26+
x if x == FloatWrapper(0.0) => todo!(),
27+
_ => todo!(),
28+
}
29+
}
30+
1831
fn main() {
1932
let c = C(1, 2);
2033
match c {

tests/ui/redundant_guards.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: redundant guard
2-
--> $DIR/redundant_guards.rs:21:20
2+
--> $DIR/redundant_guards.rs:34:20
33
|
44
LL | C(x, y) if let 1 = y => ..,
55
| ^^^^^^^^^
@@ -12,7 +12,7 @@ LL + C(x, 1) => ..,
1212
|
1313

1414
error: redundant guard
15-
--> $DIR/redundant_guards.rs:27:20
15+
--> $DIR/redundant_guards.rs:40:20
1616
|
1717
LL | Some(x) if matches!(x, Some(1) if true) => ..,
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -23,7 +23,7 @@ LL | Some(Some(1)) if true => ..,
2323
| ~~~~~~~ ~~~~~~~
2424

2525
error: redundant guard
26-
--> $DIR/redundant_guards.rs:28:20
26+
--> $DIR/redundant_guards.rs:41:20
2727
|
2828
LL | Some(x) if matches!(x, Some(1)) => {
2929
| ^^^^^^^^^^^^^^^^^^^^
@@ -35,7 +35,7 @@ LL + Some(Some(1)) => {
3535
|
3636

3737
error: redundant guard
38-
--> $DIR/redundant_guards.rs:32:20
38+
--> $DIR/redundant_guards.rs:45:20
3939
|
4040
LL | Some(x) if let Some(1) = x => ..,
4141
| ^^^^^^^^^^^^^^^
@@ -47,7 +47,7 @@ LL + Some(Some(1)) => ..,
4747
|
4848

4949
error: redundant guard
50-
--> $DIR/redundant_guards.rs:33:20
50+
--> $DIR/redundant_guards.rs:46:20
5151
|
5252
LL | Some(x) if x == Some(2) => ..,
5353
| ^^^^^^^^^^^^
@@ -59,7 +59,7 @@ LL + Some(Some(2)) => ..,
5959
|
6060

6161
error: redundant guard
62-
--> $DIR/redundant_guards.rs:56:20
62+
--> $DIR/redundant_guards.rs:69:20
6363
|
6464
LL | B { e } if matches!(e, Some(A(2))) => ..,
6565
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -71,7 +71,7 @@ LL + B { e: Some(A(2)) } => ..,
7171
|
7272

7373
error: redundant guard
74-
--> $DIR/redundant_guards.rs:93:20
74+
--> $DIR/redundant_guards.rs:106:20
7575
|
7676
LL | E::A(y) if y == "not from an or pattern" => {},
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -83,7 +83,7 @@ LL + E::A("not from an or pattern") => {},
8383
|
8484

8585
error: redundant guard
86-
--> $DIR/redundant_guards.rs:100:14
86+
--> $DIR/redundant_guards.rs:113:14
8787
|
8888
LL | x if matches!(x, Some(0)) => ..,
8989
| ^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)