Allow negative literals in redundant_guards

This commit is contained in:
Alex Macleod 2024-02-14 13:31:02 +00:00
parent 2c3213f5c5
commit b32d7c0631
4 changed files with 62 additions and 18 deletions

View File

@ -5,7 +5,7 @@
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,
} {

View File

@ -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>) {

View File

@ -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>) {

View File

@ -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