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 ;
3
3
use rustc_hir:: intravisit:: FnKind ;
4
4
use rustc_hir:: { Body , FnDecl , HirId , IsAsync } ;
5
5
use rustc_lint:: { LateContext , LateLintPass } ;
6
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7
- use rustc_span:: { Span , Symbol } ;
7
+ use rustc_span:: Span ;
8
8
9
9
declare_clippy_lint ! {
10
10
/// **What it does:** Checks for calls to await while holding a MutexGuard.
@@ -44,8 +44,6 @@ declare_clippy_lint! {
44
44
"Inside an async function, holding a MutexGuard while calling await"
45
45
}
46
46
47
- const MUTEX_GUARD_TYPES : [ & str ; 3 ] = [ "MutexGuard" , "RwLockReadGuard" , "RwLockWriteGuard" ] ;
48
-
49
47
declare_lint_pass ! ( AwaitHoldingLock => [ AWAIT_HOLDING_LOCK ] ) ;
50
48
51
49
impl LateLintPass < ' _ , ' _ > for AwaitHoldingLock {
@@ -62,21 +60,18 @@ impl LateLintPass<'_, '_> for AwaitHoldingLock {
62
60
return ;
63
61
}
64
62
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" ,
78
73
) ;
79
- }
74
+ }
80
75
}
81
76
}
82
77
}
@@ -89,12 +84,11 @@ fn is_async_fn(fn_kind: FnKind<'_>) -> bool {
89
84
} )
90
85
}
91
86
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 )
100
94
}
0 commit comments