@@ -83,11 +83,11 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
83
83
let emit_sugg = |diag : & mut Diag < ' _ , _ > | match apa. counter {
84
84
0 | 1 => { } ,
85
85
2 => {
86
- if let Some ( last_stmt_span ) = apa. last_stmt_span {
87
- if let Some ( last_method_span ) = apa. last_method_span {
88
- let indent = " " . repeat ( indent_of ( cx, last_stmt_span) . unwrap_or ( 0 ) ) ;
86
+ if ! apa. last_stmt_span . is_dummy ( ) {
87
+ if ! apa. last_method_span . is_dummy ( ) {
88
+ let indent = " " . repeat ( indent_of ( cx, apa . last_stmt_span ) . unwrap_or ( 0 ) ) ;
89
89
let init_method = snippet ( cx, apa. first_method_span , ".." ) ;
90
- let usage_method = snippet ( cx, last_method_span, ".." ) ;
90
+ let usage_method = snippet ( cx, apa . last_method_span , ".." ) ;
91
91
let stmt = if let Some ( last_bind_ident) = apa. last_bind_ident {
92
92
format ! (
93
93
"\n {indent}let {} = {init_method}.{usage_method};" ,
@@ -98,23 +98,23 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
98
98
} ;
99
99
diag. multipart_suggestion_verbose (
100
100
"merge the temporary construction with its single usage" ,
101
- vec ! [ ( apa. first_stmt_span, stmt) , ( last_stmt_span, String :: new( ) ) ] ,
101
+ vec ! [ ( apa. first_stmt_span, stmt) , ( apa . last_stmt_span, String :: new( ) ) ] ,
102
102
Applicability :: MaybeIncorrect ,
103
103
) ;
104
104
} else {
105
105
diag. help ( "merge the temporary construction with its single usage" ) ;
106
- diag. span_note ( last_stmt_span, "single usage here" ) ;
106
+ diag. span_note ( apa . last_stmt_span , "single usage here" ) ;
107
107
}
108
108
}
109
109
} ,
110
110
_ => {
111
- if let Some ( last_stmt_span ) = apa. last_stmt_span {
111
+ if ! apa. last_stmt_span . is_dummy ( ) {
112
112
diag. span_suggestion (
113
- last_stmt_span. shrink_to_hi ( ) ,
113
+ apa . last_stmt_span . shrink_to_hi ( ) ,
114
114
"drop the temporary after the end of its last usage" ,
115
115
format ! (
116
116
"\n {}drop({});" ,
117
- " " . repeat( indent_of( cx, last_stmt_span) . unwrap_or( 0 ) ) ,
117
+ " " . repeat( indent_of( cx, apa . last_stmt_span) . unwrap_or( 0 ) ) ,
118
118
first_bind_ident
119
119
) ,
120
120
Applicability :: MaybeIncorrect ,
@@ -238,19 +238,15 @@ impl<'ap, 'lc, 'others, 'stmt, 'tcx> StmtsChecker<'ap, 'lc, 'others, 'stmt, 'tcx
238
238
} ;
239
239
if has_expensive_stmt {
240
240
for apa in self . ap . apas . values_mut ( ) {
241
+ let last_stmt_is_not_dummy = apa. last_stmt_span != DUMMY_SP ;
242
+ let last_stmt_is_not_curr = self . ap . curr_stmt . span != apa. last_stmt_span ;
241
243
let block_equals_curr = self . ap . curr_block_hir_id == apa. first_block_hir_id ;
242
244
let block_is_ancestor = self
243
245
. cx
244
246
. tcx
245
247
. hir_parent_iter ( self . ap . curr_block_hir_id )
246
248
. any ( |( id, _) | id == apa. first_block_hir_id ) ;
247
- // last stmt is not dummy
248
- if let Some ( last_stmt_span) = apa. last_stmt_span
249
- && !last_stmt_span. is_dummy ( )
250
- // last stmt is not current
251
- && last_stmt_span != self . ap . curr_stmt . span
252
- && ( block_equals_curr || block_is_ancestor)
253
- {
249
+ if last_stmt_is_not_dummy && last_stmt_is_not_curr && ( block_equals_curr || block_is_ancestor) {
254
250
apa. has_expensive_expr_after_last_attr = true ;
255
251
}
256
252
}
@@ -290,10 +286,6 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
290
286
&& !self . ap . apas . contains_key ( & hir_id)
291
287
&& path_to_local ( expr) . is_none_or ( |local_hir_id| local_hir_id == hir_id)
292
288
{
293
- #[ expect(
294
- clippy:: inconsistent_struct_constructor,
295
- reason = "grouping fields with default values at the end"
296
- ) ]
297
289
let mut apa = AuxParamsAttr {
298
290
first_block_hir_id : self . ap . curr_block_hir_id ,
299
291
first_block_span : self . ap . curr_block_span ,
@@ -307,12 +299,7 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
307
299
}
308
300
} ,
309
301
first_stmt_span : self . ap . curr_stmt . span ,
310
- // default values
311
- counter : 0 ,
312
- has_expensive_expr_after_last_attr : false ,
313
- last_bind_ident : None ,
314
- last_method_span : None ,
315
- last_stmt_span : None ,
302
+ ..Default :: default ( )
316
303
} ;
317
304
modify_apa_params ( & mut apa) ;
318
305
let _ = self . ap . apas . insert ( hir_id, apa) ;
@@ -331,22 +318,22 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
331
318
if let Some ( local_init) = local. init
332
319
&& let hir:: ExprKind :: MethodCall ( _, _, _, span) = local_init. kind
333
320
{
334
- apa. last_method_span = Some ( span) ;
321
+ apa. last_method_span = span;
335
322
}
336
323
} ,
337
324
hir:: StmtKind :: Semi ( semi_expr) => {
338
325
if has_drop ( semi_expr, apa. first_bind_ident , self . cx ) {
339
326
apa. has_expensive_expr_after_last_attr = false ;
340
- apa. last_stmt_span = None ;
327
+ apa. last_stmt_span = DUMMY_SP ;
341
328
return ;
342
329
}
343
330
if let hir:: ExprKind :: MethodCall ( _, _, _, span) = semi_expr. kind {
344
- apa. last_method_span = Some ( span) ;
331
+ apa. last_method_span = span;
345
332
}
346
333
} ,
347
334
_ => { } ,
348
335
}
349
- apa. last_stmt_span = Some ( self . ap . curr_stmt . span ) ;
336
+ apa. last_stmt_span = self . ap . curr_stmt . span ;
350
337
modify_apa_params ( apa) ;
351
338
}
352
339
}
@@ -402,9 +389,26 @@ struct AuxParamsAttr {
402
389
/// marked with `#[has_significant_drop]`.
403
390
last_bind_ident : Option < Ident > ,
404
391
/// Similar to `last_bind_span` but encompasses the right-hand method call.
405
- last_method_span : Option < Span > ,
392
+ last_method_span : Span ,
406
393
/// Similar to `last_bind_span` but encompasses the whole contained statement.
407
- last_stmt_span : Option < Span > ,
394
+ last_stmt_span : Span ,
395
+ }
396
+
397
+ impl Default for AuxParamsAttr {
398
+ fn default ( ) -> Self {
399
+ Self {
400
+ counter : 0 ,
401
+ has_expensive_expr_after_last_attr : false ,
402
+ first_block_hir_id : HirId :: INVALID ,
403
+ first_block_span : DUMMY_SP ,
404
+ first_bind_ident : None ,
405
+ first_method_span : DUMMY_SP ,
406
+ first_stmt_span : DUMMY_SP ,
407
+ last_bind_ident : None ,
408
+ last_method_span : DUMMY_SP ,
409
+ last_stmt_span : DUMMY_SP ,
410
+ }
411
+ }
408
412
}
409
413
410
414
fn dummy_stmt_expr < ' any > ( expr : & ' any hir:: Expr < ' any > ) -> hir:: Stmt < ' any > {
0 commit comments