@@ -49,48 +49,6 @@ declare_clippy_lint! {
4949 "Inside an async function, holding a MutexGuard while calling await"
5050}
5151
52- declare_lint_pass ! ( AwaitHoldingLock => [ AWAIT_HOLDING_LOCK ] ) ;
53-
54- impl LateLintPass < ' _ > for AwaitHoldingLock {
55- fn check_body ( & mut self , cx : & LateContext < ' _ > , body : & ' _ Body < ' _ > ) {
56- use AsyncGeneratorKind :: { Block , Closure , Fn } ;
57- if let Some ( GeneratorKind :: Async ( Block | Closure | Fn ) ) = body. generator_kind {
58- let body_id = BodyId {
59- hir_id : body. value . hir_id ,
60- } ;
61- let def_id = cx. tcx . hir ( ) . body_owner_def_id ( body_id) ;
62- let typeck_results = cx. tcx . typeck ( def_id) ;
63- check_interior_types_lock ( cx, & typeck_results. generator_interior_types , body. value . span ) ;
64- }
65- }
66- }
67-
68- fn check_interior_types_lock ( cx : & LateContext < ' _ > , ty_causes : & [ GeneratorInteriorTypeCause < ' _ > ] , span : Span ) {
69- for ty_cause in ty_causes {
70- if let rustc_middle:: ty:: Adt ( adt, _) = ty_cause. ty . kind ( ) {
71- if is_mutex_guard ( cx, adt. did ) {
72- span_lint_and_note (
73- cx,
74- AWAIT_HOLDING_LOCK ,
75- ty_cause. span ,
76- "this MutexGuard is held across an 'await' point. Consider using an async-aware Mutex type or ensuring the MutexGuard is dropped before calling await." ,
77- ty_cause. scope_span . or ( Some ( span) ) ,
78- "these are all the await points this lock is held through" ,
79- ) ;
80- }
81- }
82- }
83- }
84-
85- fn is_mutex_guard ( cx : & LateContext < ' _ > , def_id : DefId ) -> bool {
86- match_def_path ( cx, def_id, & paths:: MUTEX_GUARD )
87- || match_def_path ( cx, def_id, & paths:: RWLOCK_READ_GUARD )
88- || match_def_path ( cx, def_id, & paths:: RWLOCK_WRITE_GUARD )
89- || match_def_path ( cx, def_id, & paths:: PARKING_LOT_MUTEX_GUARD )
90- || match_def_path ( cx, def_id, & paths:: PARKING_LOT_RWLOCK_READ_GUARD )
91- || match_def_path ( cx, def_id, & paths:: PARKING_LOT_RWLOCK_WRITE_GUARD )
92- }
93-
9452declare_clippy_lint ! {
9553 /// **What it does:** Checks for calls to await while holding a
9654 /// `RefCell` `Ref` or `RefMut`.
@@ -130,9 +88,9 @@ declare_clippy_lint! {
13088 "Inside an async function, holding a RefCell ref while calling await"
13189}
13290
133- declare_lint_pass ! ( AwaitHoldingRefCellRef => [ AWAIT_HOLDING_REFCELL_REF ] ) ;
91+ declare_lint_pass ! ( AwaitHolding => [ AWAIT_HOLDING_LOCK , AWAIT_HOLDING_REFCELL_REF ] ) ;
13492
135- impl LateLintPass < ' _ > for AwaitHoldingRefCellRef {
93+ impl LateLintPass < ' _ > for AwaitHolding {
13694 fn check_body ( & mut self , cx : & LateContext < ' _ > , body : & ' _ Body < ' _ > ) {
13795 use AsyncGeneratorKind :: { Block , Closure , Fn } ;
13896 if let Some ( GeneratorKind :: Async ( Block | Closure | Fn ) ) = body. generator_kind {
@@ -141,14 +99,24 @@ impl LateLintPass<'_> for AwaitHoldingRefCellRef {
14199 } ;
142100 let def_id = cx. tcx . hir ( ) . body_owner_def_id ( body_id) ;
143101 let typeck_results = cx. tcx . typeck ( def_id) ;
144- check_interior_types_refcell ( cx, & typeck_results. generator_interior_types , body. value . span ) ;
102+ check_interior_types ( cx, & typeck_results. generator_interior_types , body. value . span ) ;
145103 }
146104 }
147105}
148106
149- fn check_interior_types_refcell ( cx : & LateContext < ' _ > , ty_causes : & [ GeneratorInteriorTypeCause < ' _ > ] , span : Span ) {
107+ fn check_interior_types ( cx : & LateContext < ' _ > , ty_causes : & [ GeneratorInteriorTypeCause < ' _ > ] , span : Span ) {
150108 for ty_cause in ty_causes {
151109 if let rustc_middle:: ty:: Adt ( adt, _) = ty_cause. ty . kind ( ) {
110+ if is_mutex_guard ( cx, adt. did ) {
111+ span_lint_and_note (
112+ cx,
113+ AWAIT_HOLDING_LOCK ,
114+ ty_cause. span ,
115+ "this MutexGuard is held across an 'await' point. Consider using an async-aware Mutex type or ensuring the MutexGuard is dropped before calling await." ,
116+ ty_cause. scope_span . or ( Some ( span) ) ,
117+ "these are all the await points this lock is held through" ,
118+ ) ;
119+ }
152120 if is_refcell_ref ( cx, adt. did ) {
153121 span_lint_and_note (
154122 cx,
@@ -163,6 +131,15 @@ fn check_interior_types_refcell(cx: &LateContext<'_>, ty_causes: &[GeneratorInte
163131 }
164132}
165133
134+ fn is_mutex_guard ( cx : & LateContext < ' _ > , def_id : DefId ) -> bool {
135+ match_def_path ( cx, def_id, & paths:: MUTEX_GUARD )
136+ || match_def_path ( cx, def_id, & paths:: RWLOCK_READ_GUARD )
137+ || match_def_path ( cx, def_id, & paths:: RWLOCK_WRITE_GUARD )
138+ || match_def_path ( cx, def_id, & paths:: PARKING_LOT_MUTEX_GUARD )
139+ || match_def_path ( cx, def_id, & paths:: PARKING_LOT_RWLOCK_READ_GUARD )
140+ || match_def_path ( cx, def_id, & paths:: PARKING_LOT_RWLOCK_WRITE_GUARD )
141+ }
142+
166143fn is_refcell_ref ( cx : & LateContext < ' _ > , def_id : DefId ) -> bool {
167144 match_def_path ( cx, def_id, & paths:: REFCELL_REF ) || match_def_path ( cx, def_id, & paths:: REFCELL_REFMUT )
168145}
0 commit comments