Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[redundant_guards]: don't lint on float literals #11305

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions clippy_lints/src/matches/redundant_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::path_to_local;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::visitors::{for_each_expr, is_local_used};
use rustc_ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, Guard, MatchSource, Node, Pat, PatKind};
Expand Down Expand Up @@ -160,6 +161,11 @@ fn emit_redundant_guards<'tcx>(
}

/// Checks if the given `Expr` can also be represented as a `Pat`.
///
/// All literals generally also work as patterns, however float literals are special.
/// They are currently (as of 2023/08/08) still allowed in patterns, but that will become
/// an error in the future, and rustc already actively warns against this (see rust#41620),
/// so we don't consider those as usable within patterns for linting purposes.
fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
for_each_expr(expr, |expr| {
if match expr.kind {
Expand All @@ -177,8 +183,8 @@ fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
ExprKind::AddrOf(..)
| ExprKind::Array(..)
| ExprKind::Tup(..)
| ExprKind::Struct(..)
| ExprKind::Lit(..) => true,
| ExprKind::Struct(..) => true,
ExprKind::Lit(lit) if !matches!(lit.node, LitKind::Float(..)) => true,
y21 marked this conversation as resolved.
Show resolved Hide resolved
_ => false,
} {
return ControlFlow::Continue(());
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/redundant_guards.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ struct B {

struct C(u32, u32);

#[derive(PartialEq)]
struct FloatWrapper(f32);
fn issue11304() {
match 0.1 {
x if x == 0.0 => todo!(),
_ => todo!(),
}
match FloatWrapper(0.1) {
x if x == FloatWrapper(0.0) => todo!(),
_ => todo!(),
}
}

fn main() {
let c = C(1, 2);
match c {
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/redundant_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ struct B {

struct C(u32, u32);

#[derive(PartialEq)]
struct FloatWrapper(f32);
fn issue11304() {
match 0.1 {
x if x == 0.0 => todo!(),
_ => todo!(),
}
match FloatWrapper(0.1) {
x if x == FloatWrapper(0.0) => todo!(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd ideally suggest FloatWrapper(f) if f == 0.0 here, but that can be done in the future.

_ => todo!(),
}
}

fn main() {
let c = C(1, 2);
match c {
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/redundant_guards.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: redundant guard
--> $DIR/redundant_guards.rs:21:20
--> $DIR/redundant_guards.rs:34:20
|
LL | C(x, y) if let 1 = y => ..,
| ^^^^^^^^^
Expand All @@ -12,7 +12,7 @@ LL + C(x, 1) => ..,
|

error: redundant guard
--> $DIR/redundant_guards.rs:27:20
--> $DIR/redundant_guards.rs:40:20
|
LL | Some(x) if matches!(x, Some(1) if true) => ..,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -23,7 +23,7 @@ LL | Some(Some(1)) if true => ..,
| ~~~~~~~ ~~~~~~~

error: redundant guard
--> $DIR/redundant_guards.rs:28:20
--> $DIR/redundant_guards.rs:41:20
|
LL | Some(x) if matches!(x, Some(1)) => {
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -35,7 +35,7 @@ LL + Some(Some(1)) => {
|

error: redundant guard
--> $DIR/redundant_guards.rs:32:20
--> $DIR/redundant_guards.rs:45:20
|
LL | Some(x) if let Some(1) = x => ..,
| ^^^^^^^^^^^^^^^
Expand All @@ -47,7 +47,7 @@ LL + Some(Some(1)) => ..,
|

error: redundant guard
--> $DIR/redundant_guards.rs:33:20
--> $DIR/redundant_guards.rs:46:20
|
LL | Some(x) if x == Some(2) => ..,
| ^^^^^^^^^^^^
Expand All @@ -59,7 +59,7 @@ LL + Some(Some(2)) => ..,
|

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

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

error: redundant guard
--> $DIR/redundant_guards.rs:100:14
--> $DIR/redundant_guards.rs:113:14
|
LL | x if matches!(x, Some(0)) => ..,
| ^^^^^^^^^^^^^^^^^^^^
Expand Down