@@ -49,48 +49,6 @@ declare_clippy_lint! {
49
49
"Inside an async function, holding a MutexGuard while calling await"
50
50
}
51
51
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
-
94
52
declare_clippy_lint ! {
95
53
/// **What it does:** Checks for calls to await while holding a
96
54
/// `RefCell` `Ref` or `RefMut`.
@@ -130,9 +88,9 @@ declare_clippy_lint! {
130
88
"Inside an async function, holding a RefCell ref while calling await"
131
89
}
132
90
133
- declare_lint_pass ! ( AwaitHoldingRefCellRef => [ AWAIT_HOLDING_REFCELL_REF ] ) ;
91
+ declare_lint_pass ! ( AwaitHolding => [ AWAIT_HOLDING_LOCK , AWAIT_HOLDING_REFCELL_REF ] ) ;
134
92
135
- impl LateLintPass < ' _ > for AwaitHoldingRefCellRef {
93
+ impl LateLintPass < ' _ > for AwaitHolding {
136
94
fn check_body ( & mut self , cx : & LateContext < ' _ > , body : & ' _ Body < ' _ > ) {
137
95
use AsyncGeneratorKind :: { Block , Closure , Fn } ;
138
96
if let Some ( GeneratorKind :: Async ( Block | Closure | Fn ) ) = body. generator_kind {
@@ -141,14 +99,24 @@ impl LateLintPass<'_> for AwaitHoldingRefCellRef {
141
99
} ;
142
100
let def_id = cx. tcx . hir ( ) . body_owner_def_id ( body_id) ;
143
101
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 ) ;
145
103
}
146
104
}
147
105
}
148
106
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 ) {
150
108
for ty_cause in ty_causes {
151
109
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
+ }
152
120
if is_refcell_ref ( cx, adt. did ) {
153
121
span_lint_and_note (
154
122
cx,
@@ -163,6 +131,15 @@ fn check_interior_types_refcell(cx: &LateContext<'_>, ty_causes: &[GeneratorInte
163
131
}
164
132
}
165
133
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
+
166
143
fn is_refcell_ref ( cx : & LateContext < ' _ > , def_id : DefId ) -> bool {
167
144
match_def_path ( cx, def_id, & paths:: REFCELL_REF ) || match_def_path ( cx, def_id, & paths:: REFCELL_REFMUT )
168
145
}
0 commit comments