check for matches! macro directly in redundant_guards

This commit is contained in:
y21 2024-05-02 04:58:28 +02:00
parent 272413f458
commit 790fb9396a
4 changed files with 48 additions and 34 deletions

View File

@ -1,15 +1,16 @@
use clippy_config::msrvs::Msrv;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::matching_root_macro_call;
use clippy_utils::source::snippet;
use clippy_utils::visitors::{for_each_expr, is_local_used};
use clippy_utils::{in_constant, path_to_local};
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, UnOp};
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, PatKind, UnOp};
use rustc_lint::LateContext;
use rustc_span::symbol::Ident;
use rustc_span::{Span, Symbol};
use rustc_span::{sym, Span, Symbol};
use std::borrow::Cow;
use std::ops::ControlFlow;
@ -22,21 +23,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>], msrv:
};
// `Some(x) if matches!(x, y)`
if let ExprKind::Match(
scrutinee,
[
arm,
Arm {
pat: Pat {
kind: PatKind::Wild, ..
},
..
},
],
MatchSource::Normal,
) = guard.kind
if let ExprKind::Match(scrutinee, [arm, _], MatchSource::Normal) = guard.kind
&& matching_root_macro_call(cx, guard.span, sym::matches_macro).is_some()
&& let Some(binding) = get_pat_binding(cx, scrutinee, outer_arm)
&& !pat_contains_disallowed_or(&arm.pat, msrv)
&& !pat_contains_disallowed_or(arm.pat, msrv)
{
let pat_span = match (arm.pat.kind, binding.byref_ident) {
(PatKind::Ref(pat, _), Some(_)) => pat.span,
@ -55,7 +45,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>], msrv:
// `Some(x) if let Some(2) = x`
else if let ExprKind::Let(let_expr) = guard.kind
&& let Some(binding) = get_pat_binding(cx, let_expr.init, outer_arm)
&& !pat_contains_disallowed_or(&let_expr.pat, msrv)
&& !pat_contains_disallowed_or(let_expr.pat, msrv)
{
let pat_span = match (let_expr.pat.kind, binding.byref_ident) {
(PatKind::Ref(pat, _), Some(_)) => pat.span,

View File

@ -136,6 +136,18 @@ fn f(s: Option<std::ffi::OsString>) {
}
}
fn not_matches() {
match Some(42) {
// The pattern + guard is not equivalent to `Some(42)` because of the `panic!`
Some(v)
if match v {
42 => true,
_ => panic!(),
} => {},
_ => {},
}
}
struct S {
a: usize,
}

View File

@ -136,6 +136,18 @@ fn f(s: Option<std::ffi::OsString>) {
}
}
fn not_matches() {
match Some(42) {
// The pattern + guard is not equivalent to `Some(42)` because of the `panic!`
Some(v)
if match v {
42 => true,
_ => panic!(),
} => {},
_ => {},
}
}
struct S {
a: usize,
}

View File

@ -132,7 +132,7 @@ LL + 1 => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:174:28
--> tests/ui/redundant_guards.rs:186:28
|
LL | Some(ref x) if x == &1 => {},
| ^^^^^^^
@ -144,7 +144,7 @@ LL + Some(1) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:175:28
--> tests/ui/redundant_guards.rs:187:28
|
LL | Some(ref x) if &1 == x => {},
| ^^^^^^^
@ -156,7 +156,7 @@ LL + Some(1) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:176:28
--> tests/ui/redundant_guards.rs:188:28
|
LL | Some(ref x) if let &2 = x => {},
| ^^^^^^^^^^
@ -168,7 +168,7 @@ LL + Some(2) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:177:28
--> tests/ui/redundant_guards.rs:189:28
|
LL | Some(ref x) if matches!(x, &3) => {},
| ^^^^^^^^^^^^^^^
@ -180,7 +180,7 @@ LL + Some(3) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:197:32
--> tests/ui/redundant_guards.rs:209:32
|
LL | B { ref c, .. } if c == &1 => {},
| ^^^^^^^
@ -192,7 +192,7 @@ LL + B { c: 1, .. } => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:198:32
--> tests/ui/redundant_guards.rs:210:32
|
LL | B { ref c, .. } if &1 == c => {},
| ^^^^^^^
@ -204,7 +204,7 @@ LL + B { c: 1, .. } => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:199:32
--> tests/ui/redundant_guards.rs:211:32
|
LL | B { ref c, .. } if let &1 = c => {},
| ^^^^^^^^^^
@ -216,7 +216,7 @@ LL + B { c: 1, .. } => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:200:32
--> tests/ui/redundant_guards.rs:212:32
|
LL | B { ref c, .. } if matches!(c, &1) => {},
| ^^^^^^^^^^^^^^^
@ -228,7 +228,7 @@ LL + B { c: 1, .. } => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:210:26
--> tests/ui/redundant_guards.rs:222:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
@ -240,7 +240,7 @@ LL + Some(Some("")) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:221:26
--> tests/ui/redundant_guards.rs:233:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
@ -252,7 +252,7 @@ LL + Some(Some([])) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:226:26
--> tests/ui/redundant_guards.rs:238:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
@ -264,7 +264,7 @@ LL + Some(Some([])) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:237:26
--> tests/ui/redundant_guards.rs:249:26
|
LL | Some(Some(x)) if x.starts_with(&[]) => {},
| ^^^^^^^^^^^^^^^^^^
@ -276,7 +276,7 @@ LL + Some(Some([..])) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:242:26
--> tests/ui/redundant_guards.rs:254:26
|
LL | Some(Some(x)) if x.starts_with(&[1]) => {},
| ^^^^^^^^^^^^^^^^^^^
@ -288,7 +288,7 @@ LL + Some(Some([1, ..])) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:247:26
--> tests/ui/redundant_guards.rs:259:26
|
LL | Some(Some(x)) if x.starts_with(&[1, 2]) => {},
| ^^^^^^^^^^^^^^^^^^^^^^
@ -300,7 +300,7 @@ LL + Some(Some([1, 2, ..])) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:252:26
--> tests/ui/redundant_guards.rs:264:26
|
LL | Some(Some(x)) if x.ends_with(&[1, 2]) => {},
| ^^^^^^^^^^^^^^^^^^^^
@ -312,7 +312,7 @@ LL + Some(Some([.., 1, 2])) => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:274:18
--> tests/ui/redundant_guards.rs:286:18
|
LL | y if y.is_empty() => {},
| ^^^^^^^^^^^^
@ -324,7 +324,7 @@ LL + "" => {},
|
error: redundant guard
--> tests/ui/redundant_guards.rs:293:22
--> tests/ui/redundant_guards.rs:305:22
|
LL | y if y.is_empty() => {},
| ^^^^^^^^^^^^