@@ -79,10 +79,11 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
7979if apa. counter <= 1 || !apa. has_expensive_expr_after_last_attr {
8080continue ;
8181}
82+ let first_bind_ident = apa. first_bind_ident . unwrap ( ) ;
8283span_lint_and_then (
8384 cx,
8485SIGNIFICANT_DROP_TIGHTENING ,
85- apa . first_bind_ident . span ,
86+ first_bind_ident. span ,
8687"temporary with significant `Drop` can be early dropped" ,
8788 |diag| {
8889match apa. counter {
@@ -91,13 +92,13 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
9192let indent = " " . repeat ( indent_of ( cx, apa. last_stmt_span ) . unwrap_or ( 0 ) ) ;
9293let init_method = snippet ( cx, apa. first_method_span , ".." ) ;
9394let 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 {
9796format ! (
9897"\n {indent}let {} = {init_method}.{usage_method};" ,
99- snippet( cx, apa . last_bind_ident. span, ".." ) ,
98+ snippet( cx, last_bind_ident. span, ".." ) ,
10099)
100+ } else {
101+ format ! ( "\n {indent}{init_method}.{usage_method};" )
101102} ;
102103
103104 diag. multipart_suggestion_verbose (
@@ -113,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
113114format ! (
114115"\n {}drop({});" ,
115116" " . repeat( indent_of( cx, apa. last_stmt_span) . unwrap_or( 0 ) ) ,
116- apa . first_bind_ident
117+ first_bind_ident
117118) ,
118119Applicability :: MaybeIncorrect ,
119120) ;
@@ -124,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
124125 apa. first_block_span ,
125126format ! (
126127"temporary `{}` is currently being dropped at the end of its contained scope" ,
127- apa . first_bind_ident
128+ first_bind_ident
128129) ,
129130) ;
130131} ,
@@ -283,7 +284,7 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
283284let mut apa = AuxParamsAttr {
284285first_block_hir_id : self . ap . curr_block_hir_id ,
285286first_block_span : self . ap . curr_block_span ,
286- first_bind_ident : ident,
287+ first_bind_ident : Some ( ident) ,
287288first_method_span : {
288289let expr_or_init = expr_or_init ( self . cx , expr) ;
289290if let hir:: ExprKind :: MethodCall ( _, local_expr, _, span) = expr_or_init. kind {
@@ -307,7 +308,7 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
307308match self . ap . curr_stmt . kind {
308309 hir:: StmtKind :: Let ( local) => {
309310if let hir:: PatKind :: Binding ( _, _, ident, _) = local. pat . kind {
310- apa. last_bind_ident = ident;
311+ apa. last_bind_ident = Some ( ident) ;
311312}
312313if let Some ( local_init) = local. init
313314 && let hir:: ExprKind :: MethodCall ( _, _, _, span) = local_init. kind
@@ -373,15 +374,15 @@ struct AuxParamsAttr {
373374first_block_span : Span ,
374375/// The binding or variable that references the initial construction of the type marked with
375376/// `#[has_significant_drop]`.
376- first_bind_ident : Ident ,
377+ first_bind_ident : Option < Ident > ,
377378/// Similar to `init_bind_ident` but encompasses the right-hand method call.
378379first_method_span : Span ,
379380/// Similar to `init_bind_ident` but encompasses the whole contained statement.
380381first_stmt_span : Span ,
381382
382383/// The last visited binding or variable span within a block that had any referenced inner type
383384/// marked with `#[has_significant_drop]`.
384- last_bind_ident : Ident ,
385+ last_bind_ident : Option < Ident > ,
385386/// Similar to `last_bind_span` but encompasses the right-hand method call.
386387last_method_span : Span ,
387388/// Similar to `last_bind_span` but encompasses the whole contained statement.
@@ -395,10 +396,10 @@ impl Default for AuxParamsAttr {
395396has_expensive_expr_after_last_attr : false ,
396397first_block_hir_id : HirId :: INVALID ,
397398first_block_span : DUMMY_SP ,
398- first_bind_ident : Ident :: empty ( ) ,
399+ first_bind_ident : None ,
399400first_method_span : DUMMY_SP ,
400401first_stmt_span : DUMMY_SP ,
401- last_bind_ident : Ident :: empty ( ) ,
402+ last_bind_ident : None ,
402403last_method_span : DUMMY_SP ,
403404last_stmt_span : DUMMY_SP ,
404405}
@@ -413,7 +414,7 @@ fn dummy_stmt_expr<'any>(expr: &'any hir::Expr<'any>) -> hir::Stmt<'any> {
413414}
414415}
415416
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 {
417418if let hir:: ExprKind :: Call ( fun, [ first_arg] ) = expr. kind
418419 && let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, fun_path) ) = & fun. kind
419420 && 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<'_
422423let has_ident = |local_expr : & hir:: Expr < ' _ > | {
423424if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, arg_path) ) = & local_expr. kind
424425 && let [ first_arg_ps, ..] = arg_path. segments
426+ && let Some ( first_bind_ident) = first_bind_ident
425427 && & first_arg_ps. ident == first_bind_ident
426428{
427429true
0 commit comments