@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
2
2
use clippy_utils:: is_from_proc_macro;
3
3
use clippy_utils:: ty:: needs_ordered_drop;
4
4
use rustc_ast:: Mutability ;
5
- use rustc_hir:: def:: { DefKind , Res } ;
5
+ use rustc_hir:: def:: Res ;
6
6
use rustc_hir:: { BindingAnnotation , ByRef , ExprKind , HirId , Local , Node , Pat , PatKind , QPath } ;
7
7
use rustc_hir_typeck:: expr_use_visitor:: PlaceBase ;
8
8
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
@@ -71,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
71
71
// the local is user-controlled
72
72
&& !in_external_macro ( cx. sess ( ) , local. span )
73
73
&& !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)
75
75
{
76
76
span_lint_and_help (
77
77
cx,
@@ -98,16 +98,14 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
98
98
/// };
99
99
/// assert_static(closure);
100
100
/// ```
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| {
105
106
matches ! ( c. info. capture_kind, UpvarCapture :: ByValue )
106
107
&& matches ! ( c. place. base, PlaceBase :: Upvar ( upvar) if upvar. var_path. hir_id == root_variable)
107
108
} )
108
- } else {
109
- false
110
- }
111
109
}
112
110
113
111
/// Find the annotation of a binding introduced by a pattern, or `None` if it's not introduced.
0 commit comments