Skip to content

Commit

Permalink
Auto merge of rust-lang#11641 - Alexendoo:negative-redundant-guards, …
Browse files Browse the repository at this point in the history
…r=Jarcho

Allow negative literals in `redundant_guards`

changelog: none
  • Loading branch information
bors committed Feb 16, 2024
2 parents 32c006c + b32d7c0 commit 4fb9e12
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
8 changes: 6 additions & 2 deletions 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, 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};
Expand Down Expand Up @@ -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,
} {
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
56 changes: 40 additions & 16 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 @@ -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() => {},
| ^^^^^^^^^^^^
Expand All @@ -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() => {},
| ^^^^^^^^^^^^
Expand All @@ -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() => {},
| ^^^^^^^^^^^^
Expand All @@ -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(&[]) => {},
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -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]) => {},
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -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]) => {},
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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]) => {},
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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

0 comments on commit 4fb9e12

Please sign in to comment.