Skip to content

Commit

Permalink
change catagory to pedantic & add a test case & api adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Jan 19, 2024
1 parent e4d4563 commit 399780b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
42 changes: 36 additions & 6 deletions clippy_lints/src/unnecessary_blocking_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use clippy_utils::{def_path_def_ids, fn_def_id, is_lint_allowed};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, Diagnostic};
use rustc_hir::def_id::DefId;
use rustc_hir::{Body, CoroutineKind, Expr, ExprKind};
use rustc_hir::{
Body, BodyId, Closure, ClosureKind, CoroutineDesugaring, CoroutineKind, Expr, ExprKind, ImplItem, ImplItemKind,
Item, ItemKind, Node, TraitItem, TraitItemKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::Span;
Expand Down Expand Up @@ -40,7 +43,7 @@ declare_clippy_lint! {
/// ```
#[clippy::version = "1.74.0"]
pub UNNECESSARY_BLOCKING_OPS,
nursery,
pedantic,
"blocking operations in an async context"
}

Expand Down Expand Up @@ -108,8 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryBlockingOps {
if is_lint_allowed(cx, UNNECESSARY_BLOCKING_OPS, body.value.hir_id) {
return;
}

if let Some(CoroutineKind::Async(_)) = body.coroutine_kind() {
if in_async_body(cx, body.id()) {
self.is_in_async = true;
}
}
Expand All @@ -134,8 +136,8 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryBlockingOps {
}
}

fn check_body_post(&mut self, _: &LateContext<'tcx>, body: &'tcx Body<'tcx>) {
if !matches!(body.coroutine_kind(), Some(CoroutineKind::Async(_))) {
fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'tcx>) {
if !in_async_body(cx, body.id()) {
self.is_in_async = false;
}
}
Expand All @@ -153,3 +155,31 @@ fn make_suggestion(diag: &mut Diagnostic, cx: &LateContext<'_>, expr: &Expr<'_>,
Applicability::Unspecified,
);
}

fn in_async_body(cx: &LateContext<'_>, body_id: BodyId) -> bool {
let Some(parent_node) = cx.tcx.hir().find_parent(body_id.hir_id) else {
return false;
};
match parent_node {
Node::Expr(expr) => matches!(
expr.kind,
ExprKind::Closure(Closure {
kind: ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)),
..
})
),
Node::Item(Item {
kind: ItemKind::Fn(fn_sig, ..),
..
})
| Node::ImplItem(ImplItem {
kind: ImplItemKind::Fn(fn_sig, _),
..
})
| Node::TraitItem(TraitItem {
kind: TraitItemKind::Fn(fn_sig, _),
..
}) => fn_sig.header.is_async(),
_ => false,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub async fn async_fn() {
//~^ ERROR: blocking function call detected in an async body
fs::create_dir("").unwrap();
//~^ ERROR: blocking function call detected in an async body
blocking_mod::sleep(Duration::from_secs(1));
//~^ ERROR: blocking function call detected in an async body
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@ error: blocking function call detected in an async body
LL | fs::create_dir("").unwrap();
| ^^^^^^^^^^^^^^

error: aborting due to 6 previous errors
error: blocking function call detected in an async body
--> $DIR/unnecessary_blocking_ops.rs:33:5
|
LL | blocking_mod::sleep(Duration::from_secs(1));
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors

0 comments on commit 399780b

Please sign in to comment.