@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
22use clippy_utils:: is_from_proc_macro;
33use clippy_utils:: ty:: needs_ordered_drop;
44use rustc_ast:: Mutability ;
5- use rustc_hir:: def:: { DefKind , Res } ;
5+ use rustc_hir:: def:: Res ;
66use rustc_hir:: { BindingAnnotation , ByRef , ExprKind , HirId , Local , Node , Pat , PatKind , QPath } ;
77use rustc_hir_typeck:: expr_use_visitor:: PlaceBase ;
88use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
@@ -71,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
7171 // the local is user-controlled
7272 && !in_external_macro ( cx. sess ( ) , local. span )
7373 && !is_from_proc_macro ( cx, expr)
74- && !is_closure_capture ( cx, local. hir_id , binding_id)
74+ && !is_by_value_closure_capture ( cx, local. hir_id , binding_id)
7575 {
7676 span_lint_and_help (
7777 cx,
@@ -98,16 +98,14 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
9898/// };
9999/// assert_static(closure);
100100/// ```
101- fn is_closure_capture ( cx : & LateContext < ' _ > , redefinition : HirId , root_variable : HirId ) -> bool {
102- let body = cx. tcx . hir ( ) . enclosing_body_owner ( redefinition) ;
103- if let DefKind :: Closure = cx. tcx . def_kind ( body) {
104- cx. tcx . closure_captures ( body) . iter ( ) . any ( |c| {
101+ fn is_by_value_closure_capture ( cx : & LateContext < ' _ > , redefinition : HirId , root_variable : HirId ) -> bool {
102+ let closure_def_id = cx. tcx . hir ( ) . enclosing_body_owner ( redefinition) ;
103+
104+ cx. tcx . is_closure_or_coroutine ( closure_def_id. to_def_id ( ) )
105+ && cx. tcx . closure_captures ( closure_def_id) . iter ( ) . any ( |c| {
105106 matches ! ( c. info. capture_kind, UpvarCapture :: ByValue )
106107 && matches ! ( c. place. base, PlaceBase :: Upvar ( upvar) if upvar. var_path. hir_id == root_variable)
107108 } )
108- } else {
109- false
110- }
111109}
112110
113111/// Find the annotation of a binding introduced by a pattern, or `None` if it's not introduced.
0 commit comments