@@ -79,10 +79,11 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
79
79
if apa. counter <= 1 || !apa. has_expensive_expr_after_last_attr {
80
80
continue ;
81
81
}
82
+ let first_bind_ident = apa. first_bind_ident . unwrap ( ) ;
82
83
span_lint_and_then (
83
84
cx,
84
85
SIGNIFICANT_DROP_TIGHTENING ,
85
- apa . first_bind_ident . span ,
86
+ first_bind_ident. span ,
86
87
"temporary with significant `Drop` can be early dropped" ,
87
88
|diag| {
88
89
match apa. counter {
@@ -91,13 +92,13 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
91
92
let indent = " " . repeat ( indent_of ( cx, apa. last_stmt_span ) . unwrap_or ( 0 ) ) ;
92
93
let init_method = snippet ( cx, apa. first_method_span , ".." ) ;
93
94
let usage_method = snippet ( cx, apa. last_method_span , ".." ) ;
94
- let stmt = if apa. last_bind_ident == Ident :: empty ( ) {
95
- format ! ( "\n {indent}{init_method}.{usage_method};" )
96
- } else {
95
+ let stmt = if let Some ( last_bind_ident) = apa. last_bind_ident {
97
96
format ! (
98
97
"\n {indent}let {} = {init_method}.{usage_method};" ,
99
- snippet( cx, apa . last_bind_ident. span, ".." ) ,
98
+ snippet( cx, last_bind_ident. span, ".." ) ,
100
99
)
100
+ } else {
101
+ format ! ( "\n {indent}{init_method}.{usage_method};" )
101
102
} ;
102
103
103
104
diag. multipart_suggestion_verbose (
@@ -113,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
113
114
format ! (
114
115
"\n {}drop({});" ,
115
116
" " . repeat( indent_of( cx, apa. last_stmt_span) . unwrap_or( 0 ) ) ,
116
- apa . first_bind_ident
117
+ first_bind_ident
117
118
) ,
118
119
Applicability :: MaybeIncorrect ,
119
120
) ;
@@ -124,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
124
125
apa. first_block_span ,
125
126
format ! (
126
127
"temporary `{}` is currently being dropped at the end of its contained scope" ,
127
- apa . first_bind_ident
128
+ first_bind_ident
128
129
) ,
129
130
) ;
130
131
} ,
@@ -283,7 +284,7 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
283
284
let mut apa = AuxParamsAttr {
284
285
first_block_hir_id : self . ap . curr_block_hir_id ,
285
286
first_block_span : self . ap . curr_block_span ,
286
- first_bind_ident : ident,
287
+ first_bind_ident : Some ( ident) ,
287
288
first_method_span : {
288
289
let expr_or_init = expr_or_init ( self . cx , expr) ;
289
290
if let hir:: ExprKind :: MethodCall ( _, local_expr, _, span) = expr_or_init. kind {
@@ -307,7 +308,7 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
307
308
match self . ap . curr_stmt . kind {
308
309
hir:: StmtKind :: Let ( local) => {
309
310
if let hir:: PatKind :: Binding ( _, _, ident, _) = local. pat . kind {
310
- apa. last_bind_ident = ident;
311
+ apa. last_bind_ident = Some ( ident) ;
311
312
}
312
313
if let Some ( local_init) = local. init
313
314
&& let hir:: ExprKind :: MethodCall ( _, _, _, span) = local_init. kind
@@ -373,15 +374,15 @@ struct AuxParamsAttr {
373
374
first_block_span : Span ,
374
375
/// The binding or variable that references the initial construction of the type marked with
375
376
/// `#[has_significant_drop]`.
376
- first_bind_ident : Ident ,
377
+ first_bind_ident : Option < Ident > ,
377
378
/// Similar to `init_bind_ident` but encompasses the right-hand method call.
378
379
first_method_span : Span ,
379
380
/// Similar to `init_bind_ident` but encompasses the whole contained statement.
380
381
first_stmt_span : Span ,
381
382
382
383
/// The last visited binding or variable span within a block that had any referenced inner type
383
384
/// marked with `#[has_significant_drop]`.
384
- last_bind_ident : Ident ,
385
+ last_bind_ident : Option < Ident > ,
385
386
/// Similar to `last_bind_span` but encompasses the right-hand method call.
386
387
last_method_span : Span ,
387
388
/// Similar to `last_bind_span` but encompasses the whole contained statement.
@@ -395,10 +396,10 @@ impl Default for AuxParamsAttr {
395
396
has_expensive_expr_after_last_attr : false ,
396
397
first_block_hir_id : HirId :: INVALID ,
397
398
first_block_span : DUMMY_SP ,
398
- first_bind_ident : Ident :: empty ( ) ,
399
+ first_bind_ident : None ,
399
400
first_method_span : DUMMY_SP ,
400
401
first_stmt_span : DUMMY_SP ,
401
- last_bind_ident : Ident :: empty ( ) ,
402
+ last_bind_ident : None ,
402
403
last_method_span : DUMMY_SP ,
403
404
last_stmt_span : DUMMY_SP ,
404
405
}
@@ -413,7 +414,7 @@ fn dummy_stmt_expr<'any>(expr: &'any hir::Expr<'any>) -> hir::Stmt<'any> {
413
414
}
414
415
}
415
416
416
- fn has_drop ( expr : & hir:: Expr < ' _ > , first_bind_ident : & Ident , lcx : & LateContext < ' _ > ) -> bool {
417
+ fn has_drop ( expr : & hir:: Expr < ' _ > , first_bind_ident : & Option < Ident > , lcx : & LateContext < ' _ > ) -> bool {
417
418
if let hir:: ExprKind :: Call ( fun, [ first_arg] ) = expr. kind
418
419
&& let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, fun_path) ) = & fun. kind
419
420
&& let Res :: Def ( DefKind :: Fn , did) = fun_path. res
@@ -422,6 +423,7 @@ fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_
422
423
let has_ident = |local_expr : & hir:: Expr < ' _ > | {
423
424
if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, arg_path) ) = & local_expr. kind
424
425
&& let [ first_arg_ps, ..] = arg_path. segments
426
+ && let Some ( first_bind_ident) = first_bind_ident
425
427
&& & first_arg_ps. ident == first_bind_ident
426
428
{
427
429
true
0 commit comments