@@ -126,14 +126,12 @@ impl LateLintPass<'_> for SemicolonBlock {
126
126
..
127
127
} = stmt else { return } ;
128
128
semicolon_outside_block ( cx, block, expr, span) ;
129
- semicolon_outside_block_if_singleline_check_outside ( cx, block, expr, stmt. span ) ;
130
129
} ,
131
130
StmtKind :: Semi ( Expr {
132
131
kind : ExprKind :: Block ( block @ Block { expr : Some ( tail) , .. } , _) ,
133
132
..
134
133
} ) if !block. span . from_expansion ( ) => {
135
134
semicolon_inside_block ( cx, block, tail, stmt. span ) ;
136
- semicolon_outside_block_if_singleline_check_inside ( cx, block, tail, stmt. span ) ;
137
135
} ,
138
136
_ => ( ) ,
139
137
}
@@ -144,6 +142,8 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'
144
142
let insert_span = tail. span . source_callsite ( ) . shrink_to_hi ( ) ;
145
143
let remove_span = semi_span. with_lo ( block. span . hi ( ) ) ;
146
144
145
+ semicolon_outside_block_if_singleline ( cx, block, remove_span, insert_span, true , "inside" ) ;
146
+
147
147
span_lint_and_then (
148
148
cx,
149
149
SEMICOLON_INSIDE_BLOCK ,
@@ -166,6 +166,8 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
166
166
let semi_span = cx. sess ( ) . source_map ( ) . stmt_span ( semi_span, block. span ) ;
167
167
let remove_span = semi_span. with_lo ( tail_stmt_expr. span . source_callsite ( ) . hi ( ) ) ;
168
168
169
+ semicolon_outside_block_if_singleline ( cx, block, remove_span, insert_span, false , "outside" ) ;
170
+
169
171
span_lint_and_then (
170
172
cx,
171
173
SEMICOLON_OUTSIDE_BLOCK ,
@@ -182,23 +184,28 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
182
184
) ;
183
185
}
184
186
185
- fn semicolon_outside_block_if_singleline_check_inside (
187
+ fn semicolon_outside_block_if_singleline (
186
188
cx : & LateContext < ' _ > ,
187
189
block : & Block < ' _ > ,
188
- tail : & Expr < ' _ > ,
189
- semi_span : Span ,
190
+ remove_span : Span ,
191
+ insert_span : Span ,
192
+ inequality : bool ,
193
+ ty : & str ,
190
194
) {
191
- let insert_span = tail. span . source_callsite ( ) . shrink_to_hi ( ) ;
192
- let remove_span = semi_span. with_lo ( block. span . hi ( ) ) ;
195
+ let ( remove_line, insert_line) = ( get_line ( cx, remove_span) , get_line ( cx, insert_span) ) ;
193
196
194
- let ( remove_line, insert_line) = get_line ( cx, remove_span, insert_span) ;
197
+ let eq = if inequality {
198
+ remove_line != insert_line
199
+ } else {
200
+ remove_line == insert_line
201
+ } ;
195
202
196
- if insert_line != remove_line {
203
+ if eq {
197
204
span_lint_and_then (
198
205
cx,
199
206
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE ,
200
207
block. span ,
201
- "consider moving the `;` inside the block for consistent formatting" ,
208
+ & format ! ( "consider moving the `;` {ty} the block for consistent formatting" ) ,
202
209
|diag| {
203
210
multispan_sugg_with_applicability (
204
211
diag,
@@ -211,50 +218,10 @@ fn semicolon_outside_block_if_singleline_check_inside(
211
218
}
212
219
}
213
220
214
- fn semicolon_outside_block_if_singleline_check_outside (
215
- cx : & LateContext < ' _ > ,
216
- block : & Block < ' _ > ,
217
- tail_stmt_expr : & Expr < ' _ > ,
218
- semi_span : Span ,
219
- ) {
220
- let insert_span = block. span . with_lo ( block. span . hi ( ) ) ;
221
- // account for macro calls
222
- let semi_span = cx. sess ( ) . source_map ( ) . stmt_span ( semi_span, block. span ) ;
223
- let remove_span = semi_span. with_lo ( tail_stmt_expr. span . source_callsite ( ) . hi ( ) ) ;
224
-
225
- let ( remove_line, insert_line) = get_line ( cx, remove_span, insert_span) ;
226
-
227
- if remove_line == insert_line {
228
- span_lint_and_then (
229
- cx,
230
- SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE ,
231
- block. span ,
232
- "consider moving the `;` outside the block for consistent formatting" ,
233
- |diag| {
234
- multispan_sugg_with_applicability (
235
- diag,
236
- "put the `;` here" ,
237
- Applicability :: MachineApplicable ,
238
- [ ( remove_span, String :: new ( ) ) , ( insert_span, ";" . to_owned ( ) ) ] ,
239
- ) ;
240
- } ,
241
- ) ;
242
- }
243
- }
244
-
245
- fn get_line ( cx : & LateContext < ' _ > , remove_span : Span , insert_span : Span ) -> ( usize , usize ) {
246
- let remove_line = cx
247
- . sess ( )
248
- . source_map ( )
249
- . lookup_line ( remove_span. lo ( ) )
250
- . expect ( "failed to get `remove_span`'s line" )
251
- . line ;
252
- let insert_line = cx
253
- . sess ( )
221
+ fn get_line ( cx : & LateContext < ' _ > , span : Span ) -> usize {
222
+ cx. sess ( )
254
223
. source_map ( )
255
- . lookup_line ( insert_span. lo ( ) )
256
- . expect ( "failed to get `insert_span`'s line" )
257
- . line ;
258
-
259
- ( remove_line, insert_line)
224
+ . lookup_line ( span. lo ( ) )
225
+ . expect ( "failed to get span's line" )
226
+ . line
260
227
}
0 commit comments