Skip to content

Commit

Permalink
Allow negative literals in redundant_guards
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Oct 7, 2023
1 parent 33f49f3 commit 346d45f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/matches/redundant_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, Guard, MatchSource, Node, Pat, PatKind};
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, Guard, MatchSource, Node, Pat, PatKind, UnOp};
use rustc_lint::LateContext;
use rustc_span::symbol::Ident;
use rustc_span::Span;
Expand Down Expand Up @@ -224,6 +224,7 @@ fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
)
},
ExprKind::AddrOf(..)
| ExprKind::Unary(UnOp::Neg, _)
| ExprKind::Array(..)
| ExprKind::Tup(..)
| ExprKind::Struct(..) => true,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/redundant_guards.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/redundant_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Expand Down
42 changes: 33 additions & 9 deletions tests/ui/redundant_guards.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {},
| ^^^^^^^
Expand All @@ -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 => {},
| ^^^^^^^
Expand All @@ -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 => {},
| ^^^^^^^^^^
Expand All @@ -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) => {},
| ^^^^^^^^^^^^^^^
Expand All @@ -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 => {},
| ^^^^^^^
Expand All @@ -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 => {},
| ^^^^^^^
Expand All @@ -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 => {},
| ^^^^^^^^^^
Expand All @@ -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) => {},
| ^^^^^^^^^^^^^^^
Expand All @@ -203,5 +227,5 @@ LL - B { ref c, .. } if matches!(c, &1) => {},
LL + B { c: 1, .. } => {},
|

error: aborting due to 17 previous errors
error: aborting due to 19 previous errors

0 comments on commit 346d45f

Please sign in to comment.