@@ -5,7 +5,10 @@ use clippy_utils::{def_path_def_ids, fn_def_id, is_lint_allowed};
5
5
use rustc_data_structures:: fx:: FxHashMap ;
6
6
use rustc_errors:: { Applicability , Diagnostic } ;
7
7
use rustc_hir:: def_id:: DefId ;
8
- use rustc_hir:: { Body , CoroutineKind , Expr , ExprKind } ;
8
+ use rustc_hir:: {
9
+ Body , BodyId , Closure , ClosureKind , CoroutineDesugaring , CoroutineKind , Expr , ExprKind , ImplItem , ImplItemKind ,
10
+ Item , ItemKind , Node , TraitItem , TraitItemKind ,
11
+ } ;
9
12
use rustc_lint:: { LateContext , LateLintPass } ;
10
13
use rustc_session:: impl_lint_pass;
11
14
use rustc_span:: Span ;
@@ -40,7 +43,7 @@ declare_clippy_lint! {
40
43
/// ```
41
44
#[ clippy:: version = "1.74.0" ]
42
45
pub UNNECESSARY_BLOCKING_OPS ,
43
- nursery ,
46
+ pedantic ,
44
47
"blocking operations in an async context"
45
48
}
46
49
@@ -108,8 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryBlockingOps {
108
111
if is_lint_allowed ( cx, UNNECESSARY_BLOCKING_OPS , body. value . hir_id ) {
109
112
return ;
110
113
}
111
-
112
- if let Some ( CoroutineKind :: Async ( _) ) = body. coroutine_kind ( ) {
114
+ if in_async_body ( cx, body. id ( ) ) {
113
115
self . is_in_async = true ;
114
116
}
115
117
}
@@ -134,8 +136,8 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryBlockingOps {
134
136
}
135
137
}
136
138
137
- fn check_body_post ( & mut self , _ : & LateContext < ' tcx > , body : & ' tcx Body < ' tcx > ) {
138
- if !matches ! ( body. coroutine_kind ( ) , Some ( CoroutineKind :: Async ( _ ) ) ) {
139
+ fn check_body_post ( & mut self , cx : & LateContext < ' tcx > , body : & ' tcx Body < ' tcx > ) {
140
+ if !in_async_body ( cx , body. id ( ) ) {
139
141
self . is_in_async = false ;
140
142
}
141
143
}
@@ -153,3 +155,31 @@ fn make_suggestion(diag: &mut Diagnostic, cx: &LateContext<'_>, expr: &Expr<'_>,
153
155
Applicability :: Unspecified ,
154
156
) ;
155
157
}
158
+
159
+ fn in_async_body ( cx : & LateContext < ' _ > , body_id : BodyId ) -> bool {
160
+ let Some ( parent_node) = cx. tcx . hir ( ) . find_parent ( body_id. hir_id ) else {
161
+ return false ;
162
+ } ;
163
+ match parent_node {
164
+ Node :: Expr ( expr) => matches ! (
165
+ expr. kind,
166
+ ExprKind :: Closure ( Closure {
167
+ kind: ClosureKind :: Coroutine ( CoroutineKind :: Desugared ( CoroutineDesugaring :: Async , _) ) ,
168
+ ..
169
+ } )
170
+ ) ,
171
+ Node :: Item ( Item {
172
+ kind : ItemKind :: Fn ( fn_sig, ..) ,
173
+ ..
174
+ } )
175
+ | Node :: ImplItem ( ImplItem {
176
+ kind : ImplItemKind :: Fn ( fn_sig, _) ,
177
+ ..
178
+ } )
179
+ | Node :: TraitItem ( TraitItem {
180
+ kind : TraitItemKind :: Fn ( fn_sig, _) ,
181
+ ..
182
+ } ) => fn_sig. header . is_async ( ) ,
183
+ _ => false ,
184
+ }
185
+ }
0 commit comments