@@ -5,7 +5,7 @@ use clippy_utils::visitors::{for_each_expr, is_local_used};
5
5
use rustc_ast:: { BorrowKind , LitKind } ;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: def:: { DefKind , Res } ;
8
- use rustc_hir:: { Arm , BinOpKind , Expr , ExprKind , Guard , MatchSource , Node , Pat , PatKind } ;
8
+ use rustc_hir:: { Arm , BinOpKind , Expr , ExprKind , MatchSource , Node , Pat , PatKind } ;
9
9
use rustc_lint:: LateContext ;
10
10
use rustc_span:: symbol:: Ident ;
11
11
use rustc_span:: { Span , Symbol } ;
@@ -21,20 +21,19 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
21
21
} ;
22
22
23
23
// `Some(x) if matches!(x, y)`
24
- if let Guard :: If ( if_expr) = guard
25
- && let ExprKind :: Match (
26
- scrutinee,
27
- [
28
- arm,
29
- Arm {
30
- pat : Pat {
31
- kind : PatKind :: Wild , ..
32
- } ,
33
- ..
24
+ if let ExprKind :: Match (
25
+ scrutinee,
26
+ [
27
+ arm,
28
+ Arm {
29
+ pat : Pat {
30
+ kind : PatKind :: Wild , ..
34
31
} ,
35
- ] ,
36
- MatchSource :: Normal ,
37
- ) = if_expr. kind
32
+ ..
33
+ } ,
34
+ ] ,
35
+ MatchSource :: Normal ,
36
+ ) = guard. kind
38
37
&& let Some ( binding) = get_pat_binding ( cx, scrutinee, outer_arm)
39
38
{
40
39
let pat_span = match ( arm. pat . kind , binding. byref_ident ) {
@@ -45,14 +44,14 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
45
44
emit_redundant_guards (
46
45
cx,
47
46
outer_arm,
48
- if_expr . span ,
47
+ guard . span ,
49
48
snippet ( cx, pat_span, "<binding>" ) ,
50
49
& binding,
51
50
arm. guard ,
52
51
) ;
53
52
}
54
53
// `Some(x) if let Some(2) = x`
55
- else if let Guard :: IfLet ( let_expr) = guard
54
+ else if let ExprKind :: Let ( let_expr) = guard. kind
56
55
&& let Some ( binding) = get_pat_binding ( cx, let_expr. init , outer_arm)
57
56
{
58
57
let pat_span = match ( let_expr. pat . kind , binding. byref_ident ) {
@@ -71,8 +70,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
71
70
}
72
71
// `Some(x) if x == Some(2)`
73
72
// `Some(x) if Some(2) == x`
74
- else if let Guard :: If ( if_expr) = guard
75
- && let ExprKind :: Binary ( bin_op, local, pat) = if_expr. kind
73
+ else if let ExprKind :: Binary ( bin_op, local, pat) = guard. kind
76
74
&& matches ! ( bin_op. node, BinOpKind :: Eq )
77
75
// Ensure they have the same type. If they don't, we'd need deref coercion which isn't
78
76
// possible (currently) in a pattern. In some cases, you can use something like
@@ -96,16 +94,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
96
94
emit_redundant_guards (
97
95
cx,
98
96
outer_arm,
99
- if_expr . span ,
97
+ guard . span ,
100
98
snippet ( cx, pat_span, "<binding>" ) ,
101
99
& binding,
102
100
None ,
103
101
) ;
104
- } else if let Guard :: If ( if_expr) = guard
105
- && let ExprKind :: MethodCall ( path, recv, args, ..) = if_expr. kind
102
+ } else if let ExprKind :: MethodCall ( path, recv, args, ..) = guard. kind
106
103
&& let Some ( binding) = get_pat_binding ( cx, recv, outer_arm)
107
104
{
108
- check_method_calls ( cx, outer_arm, path. ident . name , recv, args, if_expr , & binding) ;
105
+ check_method_calls ( cx, outer_arm, path. ident . name , recv, args, guard , & binding) ;
109
106
}
110
107
}
111
108
}
@@ -216,7 +213,7 @@ fn emit_redundant_guards<'tcx>(
216
213
guard_span : Span ,
217
214
binding_replacement : Cow < ' static , str > ,
218
215
pat_binding : & PatBindingInfo ,
219
- inner_guard : Option < Guard < ' _ > > ,
216
+ inner_guard : Option < & Expr < ' _ > > ,
220
217
) {
221
218
span_lint_and_then (
222
219
cx,
@@ -242,12 +239,7 @@ fn emit_redundant_guards<'tcx>(
242
239
(
243
240
guard_span. source_callsite( ) . with_lo( outer_arm. pat. span. hi( ) ) ,
244
241
inner_guard. map_or_else( String :: new, |guard| {
245
- let ( prefix, span) = match guard {
246
- Guard :: If ( e) => ( "if" , e. span) ,
247
- Guard :: IfLet ( l) => ( "if let" , l. span) ,
248
- } ;
249
-
250
- format!( " {prefix} {}" , snippet( cx, span, "<guard>" ) )
242
+ format!( " if {}" , snippet( cx, guard. span, "<guard>" ) )
251
243
} ) ,
252
244
) ,
253
245
] ,
0 commit comments