Skip to content
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
7 changes: 1 addition & 6 deletions clippy_lints/src/matches/redundant_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,6 @@ 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_without_closures(expr, |expr| {
if match expr.kind {
Expand All @@ -267,7 +262,7 @@ fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
| ExprKind::Tup(..)
| ExprKind::Struct(..)
| ExprKind::Unary(UnOp::Neg, _) => true,
ExprKind::Lit(lit) if !matches!(lit.node, LitKind::Float(..)) => true,
ExprKind::Lit(lit) if !matches!(lit.node, LitKind::CStr(..)) => true,
_ => false,
} {
return ControlFlow::Continue(());
Expand Down
15 changes: 12 additions & 3 deletions tests/ui/redundant_guards.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macros.rs
#![feature(if_let_guard)]
#![allow(clippy::no_effect, unused, clippy::single_match)]
#![allow(clippy::no_effect, unused, clippy::single_match, invalid_nan_comparisons)]
#![warn(clippy::redundant_guards)]

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

fn issue11304() {
match 0.1 {
x if x == 0.0 => todo!(),
0.0 => todo!(),
// Pattern matching NAN is illegal
x if x == f64::NAN => todo!(),
_ => todo!(),
}
match FloatWrapper(0.1) {
x if x == FloatWrapper(0.0) => todo!(),
FloatWrapper(0.0) => todo!(),
_ => todo!(),
}
}

fn issue13681() {
match c"hi" {
x if x == c"hi" => (),
_ => (),
}
}

fn main() {
let c = C(1, 2);
match c {
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/redundant_guards.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macros.rs
#![feature(if_let_guard)]
#![allow(clippy::no_effect, unused, clippy::single_match)]
#![allow(clippy::no_effect, unused, clippy::single_match, invalid_nan_comparisons)]
#![warn(clippy::redundant_guards)]

#[macro_use]
Expand All @@ -20,6 +20,8 @@ struct FloatWrapper(f32);
fn issue11304() {
match 0.1 {
x if x == 0.0 => todo!(),
// Pattern matching NAN is illegal
x if x == f64::NAN => todo!(),
_ => todo!(),
}
match FloatWrapper(0.1) {
Expand All @@ -28,6 +30,13 @@ fn issue11304() {
}
}

fn issue13681() {
match c"hi" {
x if x == c"hi" => (),
_ => (),
}
}

fn main() {
let c = C(1, 2);
match c {
Expand Down
86 changes: 55 additions & 31 deletions tests/ui/redundant_guards.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
error: redundant guard
--> tests/ui/redundant_guards.rs:34:20
--> tests/ui/redundant_guards.rs:22:14
|
LL | C(x, y) if let 1 = y => ..,
| ^^^^^^^^^
LL | x if x == 0.0 => todo!(),
| ^^^^^^^^
|
= note: `-D clippy::redundant-guards` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::redundant_guards)]`
help: try
|
LL - x if x == 0.0 => todo!(),
LL + 0.0 => todo!(),
|

error: redundant guard
--> tests/ui/redundant_guards.rs:28:14
|
LL | x if x == FloatWrapper(0.0) => todo!(),
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL - x if x == FloatWrapper(0.0) => todo!(),
LL + FloatWrapper(0.0) => todo!(),
|

error: redundant guard
--> tests/ui/redundant_guards.rs:43:20
|
LL | C(x, y) if let 1 = y => ..,
| ^^^^^^^^^
|
help: try
|
LL - C(x, y) if let 1 = y => ..,
LL + C(x, 1) => ..,
|

error: redundant guard
--> tests/ui/redundant_guards.rs:40:20
--> tests/ui/redundant_guards.rs:49:20
|
LL | Some(x) if matches!(x, Some(1) if true) => ..,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -24,7 +48,7 @@ LL | Some(Some(1)) if true => ..,
| ~~~~~~~ ~~~~~~~

error: redundant guard
--> tests/ui/redundant_guards.rs:41:20
--> tests/ui/redundant_guards.rs:50:20
|
LL | Some(x) if matches!(x, Some(1)) => {
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -36,7 +60,7 @@ LL + Some(Some(1)) => {
|

error: redundant guard
--> tests/ui/redundant_guards.rs:45:20
--> tests/ui/redundant_guards.rs:54:20
|
LL | Some(x) if let Some(1) = x => ..,
| ^^^^^^^^^^^^^^^
Expand All @@ -48,7 +72,7 @@ LL + Some(Some(1)) => ..,
|

error: redundant guard
--> tests/ui/redundant_guards.rs:46:20
--> tests/ui/redundant_guards.rs:55:20
|
LL | Some(x) if x == Some(2) => ..,
| ^^^^^^^^^^^^
Expand All @@ -60,7 +84,7 @@ LL + Some(Some(2)) => ..,
|

error: redundant guard
--> tests/ui/redundant_guards.rs:47:20
--> tests/ui/redundant_guards.rs:56:20
|
LL | Some(x) if Some(2) == x => ..,
| ^^^^^^^^^^^^
Expand All @@ -72,7 +96,7 @@ LL + Some(Some(2)) => ..,
|

error: redundant guard
--> tests/ui/redundant_guards.rs:72:20
--> tests/ui/redundant_guards.rs:81:20
|
LL | B { e } if matches!(e, Some(A(2))) => ..,
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -84,7 +108,7 @@ LL + B { e: Some(A(2)) } => ..,
|

error: redundant guard
--> tests/ui/redundant_guards.rs:109:20
--> tests/ui/redundant_guards.rs:118:20
|
LL | E::A(y) if y == "not from an or pattern" => {},
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -96,7 +120,7 @@ LL + E::A("not from an or pattern") => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:116:14
--> tests/ui/redundant_guards.rs:125:14
|
LL | x if matches!(x, Some(0)) => ..,
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -108,7 +132,7 @@ LL + Some(0) => ..,
|

error: redundant guard
--> tests/ui/redundant_guards.rs:123:14
--> tests/ui/redundant_guards.rs:132:14
|
LL | i if i == -1 => {},
| ^^^^^^^
Expand All @@ -120,7 +144,7 @@ LL + -1 => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:124:14
--> tests/ui/redundant_guards.rs:133:14
|
LL | i if i == 1 => {},
| ^^^^^^
Expand All @@ -132,7 +156,7 @@ LL + 1 => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:186:28
--> tests/ui/redundant_guards.rs:195:28
|
LL | Some(ref x) if x == &1 => {},
| ^^^^^^^
Expand All @@ -144,7 +168,7 @@ LL + Some(1) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:187:28
--> tests/ui/redundant_guards.rs:196:28
|
LL | Some(ref x) if &1 == x => {},
| ^^^^^^^
Expand All @@ -156,7 +180,7 @@ LL + Some(1) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:188:28
--> tests/ui/redundant_guards.rs:197:28
|
LL | Some(ref x) if let &2 = x => {},
| ^^^^^^^^^^
Expand All @@ -168,7 +192,7 @@ LL + Some(2) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:189:28
--> tests/ui/redundant_guards.rs:198:28
|
LL | Some(ref x) if matches!(x, &3) => {},
| ^^^^^^^^^^^^^^^
Expand All @@ -180,7 +204,7 @@ LL + Some(3) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:209:32
--> tests/ui/redundant_guards.rs:218:32
|
LL | B { ref c, .. } if c == &1 => {},
| ^^^^^^^
Expand All @@ -192,7 +216,7 @@ LL + B { c: 1, .. } => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:210:32
--> tests/ui/redundant_guards.rs:219:32
|
LL | B { ref c, .. } if &1 == c => {},
| ^^^^^^^
Expand All @@ -204,7 +228,7 @@ LL + B { c: 1, .. } => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:211:32
--> tests/ui/redundant_guards.rs:220:32
|
LL | B { ref c, .. } if let &1 = c => {},
| ^^^^^^^^^^
Expand All @@ -216,7 +240,7 @@ LL + B { c: 1, .. } => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:212:32
--> tests/ui/redundant_guards.rs:221:32
|
LL | B { ref c, .. } if matches!(c, &1) => {},
| ^^^^^^^^^^^^^^^
Expand All @@ -228,7 +252,7 @@ LL + B { c: 1, .. } => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:222:26
--> tests/ui/redundant_guards.rs:231:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
Expand All @@ -240,7 +264,7 @@ LL + Some(Some("")) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:233:26
--> tests/ui/redundant_guards.rs:242:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
Expand All @@ -252,7 +276,7 @@ LL + Some(Some([])) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:238:26
--> tests/ui/redundant_guards.rs:247:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
Expand All @@ -264,7 +288,7 @@ LL + Some(Some([])) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:249:26
--> tests/ui/redundant_guards.rs:258:26
|
LL | Some(Some(x)) if x.starts_with(&[]) => {},
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -276,7 +300,7 @@ LL + Some(Some([..])) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:254:26
--> tests/ui/redundant_guards.rs:263:26
|
LL | Some(Some(x)) if x.starts_with(&[1]) => {},
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -288,7 +312,7 @@ LL + Some(Some([1, ..])) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:259:26
--> tests/ui/redundant_guards.rs:268:26
|
LL | Some(Some(x)) if x.starts_with(&[1, 2]) => {},
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -300,7 +324,7 @@ LL + Some(Some([1, 2, ..])) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:264:26
--> tests/ui/redundant_guards.rs:273:26
|
LL | Some(Some(x)) if x.ends_with(&[1, 2]) => {},
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -312,7 +336,7 @@ LL + Some(Some([.., 1, 2])) => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:286:18
--> tests/ui/redundant_guards.rs:295:18
|
LL | y if y.is_empty() => {},
| ^^^^^^^^^^^^
Expand All @@ -324,7 +348,7 @@ LL + "" => {},
|

error: redundant guard
--> tests/ui/redundant_guards.rs:305:22
--> tests/ui/redundant_guards.rs:314:22
|
LL | y if y.is_empty() => {},
| ^^^^^^^^^^^^
Expand All @@ -335,5 +359,5 @@ LL - y if y.is_empty() => {},
LL + "" => {},
|

error: aborting due to 28 previous errors
error: aborting due to 30 previous errors