about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2024-02-14 13:31:02 +0000
committerAlex Macleod <alex@macleod.io>2024-02-14 13:31:02 +0000
commitb32d7c063106a1ac4a989be7d051b756f007c1f3 (patch)
treefb43e3d1b283dd3714f1a2511ee3d6af64f649f1
parent2c3213f5c5c3ecce5712d7aded3020fe61791179 (diff)
downloadrust-b32d7c063106a1ac4a989be7d051b756f007c1f3.tar.gz
rust-b32d7c063106a1ac4a989be7d051b756f007c1f3.zip
Allow negative literals in `redundant_guards`
-rw-r--r--clippy_lints/src/matches/redundant_guards.rs8
-rw-r--r--tests/ui/redundant_guards.fixed8
-rw-r--r--tests/ui/redundant_guards.rs8
-rw-r--r--tests/ui/redundant_guards.stderr56
4 files changed, 62 insertions, 18 deletions
diff --git a/clippy_lints/src/matches/redundant_guards.rs b/clippy_lints/src/matches/redundant_guards.rs
index dfaaeb14ca3..a4374b28fa1 100644
--- a/clippy_lints/src/matches/redundant_guards.rs
+++ b/clippy_lints/src/matches/redundant_guards.rs
@@ -5,7 +5,7 @@ use clippy_utils::visitors::{for_each_expr, is_local_used};
 use rustc_ast::{BorrowKind, LitKind};
 use rustc_errors::Applicability;
 use rustc_hir::def::{DefKind, Res};
-use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, Pat, PatKind};
+use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, Pat, PatKind, UnOp};
 use rustc_lint::LateContext;
 use rustc_span::symbol::Ident;
 use rustc_span::{Span, Symbol};
@@ -269,7 +269,11 @@ fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
                     Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Ctor(..), ..),
                 )
             },
-            ExprKind::AddrOf(..) | ExprKind::Array(..) | ExprKind::Tup(..) | ExprKind::Struct(..) => true,
+            ExprKind::AddrOf(..)
+            | ExprKind::Array(..)
+            | ExprKind::Tup(..)
+            | ExprKind::Struct(..)
+            | ExprKind::Unary(UnOp::Neg, _) => true,
             ExprKind::Lit(lit) if !matches!(lit.node, LitKind::Float(..)) => true,
             _ => false,
         } {
diff --git a/tests/ui/redundant_guards.fixed b/tests/ui/redundant_guards.fixed
index aef26ef225c..0a2ae911ad4 100644
--- a/tests/ui/redundant_guards.fixed
+++ b/tests/ui/redundant_guards.fixed
@@ -117,6 +117,14 @@ fn h(v: Option<u32>) {
     };
 }
 
+fn negative_literal(i: i32) {
+    match i {
+        -1 => {},
+        1 => {},
+        _ => {},
+    }
+}
+
 // Do not lint
 
 fn f(s: Option<std::ffi::OsString>) {
diff --git a/tests/ui/redundant_guards.rs b/tests/ui/redundant_guards.rs
index 5d476f5b040..c8656f26ef3 100644
--- a/tests/ui/redundant_guards.rs
+++ b/tests/ui/redundant_guards.rs
@@ -117,6 +117,14 @@ fn h(v: Option<u32>) {
     };
 }
 
+fn negative_literal(i: i32) {
+    match i {
+        i if i == -1 => {},
+        i if i == 1 => {},
+        _ => {},
+    }
+}
+
 // Do not lint
 
 fn f(s: Option<std::ffi::OsString>) {
diff --git a/tests/ui/redundant_guards.stderr b/tests/ui/redundant_guards.stderr
index f78d2a814f9..27bd8bfabb1 100644
--- a/tests/ui/redundant_guards.stderr
+++ b/tests/ui/redundant_guards.stderr
@@ -108,7 +108,31 @@ LL +         Some(0) => ..,
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:165:28
+  --> $DIR/redundant_guards.rs:122:14
+   |
+LL |         i if i == -1 => {},
+   |              ^^^^^^^
+   |
+help: try
+   |
+LL -         i if i == -1 => {},
+LL +         -1 => {},
+   |
+
+error: redundant guard
+  --> $DIR/redundant_guards.rs:123:14
+   |
+LL |         i if i == 1 => {},
+   |              ^^^^^^
+   |
+help: try
+   |
+LL -         i if i == 1 => {},
+LL +         1 => {},
+   |
+
+error: redundant guard
+  --> $DIR/redundant_guards.rs:173:28
    |
 LL |             Some(ref x) if x == &1 => {},
    |                            ^^^^^^^
@@ -120,7 +144,7 @@ LL +             Some(1) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:166:28
+  --> $DIR/redundant_guards.rs:174:28
    |
 LL |             Some(ref x) if &1 == x => {},
    |                            ^^^^^^^
@@ -132,7 +156,7 @@ LL +             Some(1) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:167:28
+  --> $DIR/redundant_guards.rs:175:28
    |
 LL |             Some(ref x) if let &2 = x => {},
    |                            ^^^^^^^^^^
@@ -144,7 +168,7 @@ LL +             Some(2) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:168:28
+  --> $DIR/redundant_guards.rs:176:28
    |
 LL |             Some(ref x) if matches!(x, &3) => {},
    |                            ^^^^^^^^^^^^^^^
@@ -156,7 +180,7 @@ LL +             Some(3) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:188:32
+  --> $DIR/redundant_guards.rs:196:32
    |
 LL |             B { ref c, .. } if c == &1 => {},
    |                                ^^^^^^^
@@ -168,7 +192,7 @@ LL +             B { c: 1, .. } => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:189:32
+  --> $DIR/redundant_guards.rs:197:32
    |
 LL |             B { ref c, .. } if &1 == c => {},
    |                                ^^^^^^^
@@ -180,7 +204,7 @@ LL +             B { c: 1, .. } => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:190:32
+  --> $DIR/redundant_guards.rs:198:32
    |
 LL |             B { ref c, .. } if let &1 = c => {},
    |                                ^^^^^^^^^^
@@ -192,7 +216,7 @@ LL +             B { c: 1, .. } => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:191:32
+  --> $DIR/redundant_guards.rs:199:32
    |
 LL |             B { ref c, .. } if matches!(c, &1) => {},
    |                                ^^^^^^^^^^^^^^^
@@ -204,7 +228,7 @@ LL +             B { c: 1, .. } => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:201:26
+  --> $DIR/redundant_guards.rs:209:26
    |
 LL |         Some(Some(x)) if x.is_empty() => {},
    |                          ^^^^^^^^^^^^
@@ -216,7 +240,7 @@ LL +         Some(Some("")) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:212:26
+  --> $DIR/redundant_guards.rs:220:26
    |
 LL |         Some(Some(x)) if x.is_empty() => {},
    |                          ^^^^^^^^^^^^
@@ -228,7 +252,7 @@ LL +         Some(Some([])) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:217:26
+  --> $DIR/redundant_guards.rs:225:26
    |
 LL |         Some(Some(x)) if x.is_empty() => {},
    |                          ^^^^^^^^^^^^
@@ -240,7 +264,7 @@ LL +         Some(Some([])) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:228:26
+  --> $DIR/redundant_guards.rs:236:26
    |
 LL |         Some(Some(x)) if x.starts_with(&[]) => {},
    |                          ^^^^^^^^^^^^^^^^^^
@@ -252,7 +276,7 @@ LL +         Some(Some([..])) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:233:26
+  --> $DIR/redundant_guards.rs:241:26
    |
 LL |         Some(Some(x)) if x.starts_with(&[1]) => {},
    |                          ^^^^^^^^^^^^^^^^^^^
@@ -264,7 +288,7 @@ LL +         Some(Some([1, ..])) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:238:26
+  --> $DIR/redundant_guards.rs:246:26
    |
 LL |         Some(Some(x)) if x.starts_with(&[1, 2]) => {},
    |                          ^^^^^^^^^^^^^^^^^^^^^^
@@ -276,7 +300,7 @@ LL +         Some(Some([1, 2, ..])) => {},
    |
 
 error: redundant guard
-  --> $DIR/redundant_guards.rs:243:26
+  --> $DIR/redundant_guards.rs:251:26
    |
 LL |         Some(Some(x)) if x.ends_with(&[1, 2]) => {},
    |                          ^^^^^^^^^^^^^^^^^^^^
@@ -287,5 +311,5 @@ LL -         Some(Some(x)) if x.ends_with(&[1, 2]) => {},
 LL +         Some(Some([.., 1, 2])) => {},
    |
 
-error: aborting due to 24 previous errors
+error: aborting due to 26 previous errors