1- use crate :: utils:: span_lint_and_note;
2- use if_chain :: if_chain ;
1+ use crate :: utils:: { match_def_path , paths , span_lint_and_note} ;
2+ use rustc_hir :: def_id :: DefId ;
33use rustc_hir:: intravisit:: FnKind ;
44use rustc_hir:: { Body , FnDecl , HirId , IsAsync } ;
55use rustc_lint:: { LateContext , LateLintPass } ;
66use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7- use rustc_span:: { Span , Symbol } ;
7+ use rustc_span:: Span ;
88
99declare_clippy_lint ! {
1010 /// **What it does:** Checks for calls to await while holding a MutexGuard.
@@ -44,8 +44,6 @@ declare_clippy_lint! {
4444 "Inside an async function, holding a MutexGuard while calling await"
4545}
4646
47- const MUTEX_GUARD_TYPES : [ & str ; 3 ] = [ "MutexGuard" , "RwLockReadGuard" , "RwLockWriteGuard" ] ;
48-
4947declare_lint_pass ! ( AwaitHoldingLock => [ AWAIT_HOLDING_LOCK ] ) ;
5048
5149impl LateLintPass < ' _ , ' _ > for AwaitHoldingLock {
@@ -62,21 +60,18 @@ impl LateLintPass<'_, '_> for AwaitHoldingLock {
6260 return ;
6361 }
6462
65- for ty_clause in & cx. tables . generator_interior_types {
66- if_chain ! {
67- if let rustc_middle:: ty:: Adt ( adt, _) = ty_clause. ty. kind;
68- if let Some ( & sym) = cx. get_def_path( adt. did) . iter( ) . last( ) ;
69- if is_symbol_mutex_guard( sym) ;
70- then {
71- span_lint_and_note(
72- cx,
73- AWAIT_HOLDING_LOCK ,
74- ty_clause. span,
75- "this MutexGuard is held across an 'await' point" ,
76- ty_clause. scope_span. unwrap_or( span) ,
77- "these are all the await points this lock is held through"
63+ for ty_cause in & cx. tables . generator_interior_types {
64+ if let rustc_middle:: ty:: Adt ( adt, _) = ty_cause. ty . kind {
65+ if is_mutex_guard ( cx, adt. did ) {
66+ span_lint_and_note (
67+ cx,
68+ AWAIT_HOLDING_LOCK ,
69+ ty_cause. span ,
70+ "this MutexGuard is held across an 'await' point" ,
71+ ty_cause. scope_span . unwrap_or ( span) ,
72+ "these are all the await points this lock is held through" ,
7873 ) ;
79- }
74+ }
8075 }
8176 }
8277 }
@@ -89,12 +84,11 @@ fn is_async_fn(fn_kind: FnKind<'_>) -> bool {
8984 } )
9085}
9186
92- fn is_symbol_mutex_guard ( sym : Symbol ) -> bool {
93- let sym_str = sym. as_str ( ) ;
94- for ty in & MUTEX_GUARD_TYPES {
95- if sym_str == * ty {
96- return true ;
97- }
98- }
99- false
87+ fn is_mutex_guard ( cx : & LateContext < ' _ , ' _ > , def_id : DefId ) -> bool {
88+ match_def_path ( cx, def_id, & paths:: MUTEX_GUARD )
89+ || match_def_path ( cx, def_id, & paths:: RWLOCK_READ_GUARD )
90+ || match_def_path ( cx, def_id, & paths:: RWLOCK_WRITE_GUARD )
91+ || match_def_path ( cx, def_id, & paths:: PARKING_LOT_MUTEX_GUARD )
92+ || match_def_path ( cx, def_id, & paths:: PARKING_LOT_RWLOCK_READ_GUARD )
93+ || match_def_path ( cx, def_id, & paths:: PARKING_LOT_RWLOCK_WRITE_GUARD )
10094}
0 commit comments