Skip to content

Allow negative literals in redundant_guards #11641

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

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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
8 changes: 6 additions & 2 deletions clippy_lints/src/matches/redundant_guards.rs
Original file line number Diff line number Diff line change
@@ -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,
} {
8 changes: 8 additions & 0 deletions tests/ui/redundant_guards.fixed
Original file line number Diff line number Diff line change
@@ -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>) {
8 changes: 8 additions & 0 deletions tests/ui/redundant_guards.rs
Original file line number Diff line number Diff line change
@@ -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>) {
56 changes: 40 additions & 16 deletions tests/ui/redundant_guards.stderr
Original file line number Diff line number Diff line change
@@ -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