@@ -5,7 +5,7 @@ use rustc_errors::Applicability;
5
5
use rustc_hir:: { Arm , ExprKind } ;
6
6
use rustc_lint:: { LateContext , LateLintPass } ;
7
7
use rustc_session:: declare_lint_pass;
8
- use rustc_span:: { BytePos , Span } ;
8
+ use rustc_span:: BytePos ;
9
9
10
10
declare_clippy_lint ! {
11
11
/// ### What it does
@@ -73,18 +73,24 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMatchGuards {
73
73
if let ExprKind :: If ( cond, then, None ) = arm_body_expr. kind
74
74
&& eq_expr_value ( cx, guard, cond. peel_drop_temps ( ) )
75
75
{
76
- let ExprKind :: Block ( then_without_curlies, _) = then. kind else {
77
- unreachable ! ( "the `then` expr in `ExprKind::If` is always `ExprKind::Block`" )
78
- } ;
76
+ // make sure that we won't swallow any comments. be extra conservative and bail out on _any_ comment
77
+ // outside of `then`:
78
+ //
79
+ // <pat> if <guard> => { if <cond> <then> }
80
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
81
+ let sm = cx. sess ( ) . source_map ( ) ;
82
+ if span_contains_comment ( sm, arm. span . with_hi ( then. span . lo ( ) ) )
83
+ || span_contains_comment ( sm, arm. span . with_lo ( then. span . hi ( ) ) )
84
+ {
85
+ return ;
86
+ }
79
87
80
88
// the two expressions may be syntactically different, even if identical
81
89
// semantically -- the user might want to replace the condition in the guard
82
90
// with the one in the body
83
91
let mut applicability = Applicability :: MaybeIncorrect ;
84
92
85
- let sugg = snippet_with_applicability ( cx, then_without_curlies. span , ".." , & mut applicability) ;
86
-
87
- if body_has_block {
93
+ let sugg_span = if body_has_block {
88
94
// the common case:
89
95
// ```
90
96
// match 0u32 {
@@ -96,59 +102,34 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMatchGuards {
96
102
// }
97
103
// ```
98
104
//
99
- // suggest removing the `if` _and_ the curlies of the inner brace,
100
- // since the arm body already has braces
101
-
102
- // make sure that we won't swallow any comments. be extra conservative and bail out on _any_ comment
103
- // outside of `then`:
104
- //
105
- // <pat> if <guard> => { if <cond> <then> }
106
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
107
- let sm = cx. sess ( ) . source_map ( ) ;
108
- if span_contains_comment ( sm, arm. span . with_hi ( then. span . lo ( ) ) )
109
- || span_contains_comment ( sm, arm. span . with_lo ( then. span . hi ( ) ) )
110
- {
111
- return ;
112
- }
113
-
114
- let sugg = snippet_with_applicability (
115
- cx,
116
- then. span
117
- . with_lo ( then. span . lo ( ) + BytePos ( 1 ) )
118
- . with_hi ( then. span . hi ( ) - BytePos ( 1 ) ) ,
119
- ".." ,
120
- & mut applicability,
121
- ) ;
122
-
123
- span_lint_and_sugg (
124
- cx,
125
- DUPLICATE_MATCH_GUARDS ,
126
- arm_body_expr. span ,
127
- "condition duplicates match guard" ,
128
- "remove the condition" ,
129
- sugg. to_string ( ) ,
130
- applicability,
131
- ) ;
105
+ // the arm body already has curlies, so we can remove the ones around `then`
106
+ then. span
107
+ . with_lo ( then. span . lo ( ) + BytePos ( 1 ) )
108
+ . with_hi ( then. span . hi ( ) - BytePos ( 1 ) )
132
109
} else {
133
- // the uncommon case (rusfmt would add the braces here automatically)
110
+ // the uncommon case (rusfmt would add the curlies here automatically)
134
111
// ```
135
112
// match 0u32 {
136
113
// 0 if true => if true { return; }
137
114
// }
138
115
// ```
139
116
//
140
- // suggest removing the `if` but _not_ the curlies of the inner brace,
141
- // since there are no outer braces coming from the arm body
142
- span_lint_and_sugg (
143
- cx,
144
- DUPLICATE_MATCH_GUARDS ,
145
- arm_body_expr. span ,
146
- "condition duplicates match guard" ,
147
- "remove the condition" ,
148
- sugg. to_string ( ) ,
149
- applicability,
150
- ) ;
151
- }
117
+ // the arm body doesn't have its own curlies,
118
+ // so we need to retain the ones around `then`
119
+ then. span
120
+ } ;
121
+
122
+ let sugg = snippet_with_applicability ( cx, sugg_span, ".." , & mut applicability) ;
123
+
124
+ span_lint_and_sugg (
125
+ cx,
126
+ DUPLICATE_MATCH_GUARDS ,
127
+ arm_body_expr. span ,
128
+ "condition duplicates match guard" ,
129
+ "remove the condition" ,
130
+ sugg. to_string ( ) ,
131
+ applicability,
132
+ ) ;
152
133
}
153
134
}
154
135
}
0 commit comments