|
| 1 | +use rustc_hir as hir; |
| 2 | +use rustc_macros::{LintDiagnostic, Subdiagnostic}; |
| 3 | +use rustc_session::{declare_lint, declare_lint_pass}; |
| 4 | +use rustc_span::Span; |
| 5 | + |
| 6 | +use crate::{LateContext, LateLintPass}; |
| 7 | + |
| 8 | +declare_lint! { |
| 9 | + /// The `closure_returning_async_block` lint detects cases where users |
| 10 | + /// write a closure that returns an async block. |
| 11 | + /// |
| 12 | + /// ### Example |
| 13 | + /// |
| 14 | + /// ```rust |
| 15 | + /// #![warn(closure_returning_async_block)] |
| 16 | + /// let c = |x: &str| async {}; |
| 17 | + /// ``` |
| 18 | + /// |
| 19 | + /// {{produces}} |
| 20 | + /// |
| 21 | + /// ### Explanation |
| 22 | + /// |
| 23 | + /// Using an async closure is preferable over a closure that returns an |
| 24 | + /// async block, since async closures are less restrictive in how its |
| 25 | + /// captures are allowed to be used. |
| 26 | + /// |
| 27 | + /// For example, this code does not work with a closure returning an async |
| 28 | + /// block: |
| 29 | + /// |
| 30 | + /// ```rust,compile_fail |
| 31 | + /// async fn callback(x: &str) {} |
| 32 | + /// |
| 33 | + /// let captured_str = String::new(); |
| 34 | + /// let c = move || async { |
| 35 | + /// callback(&captured_str).await; |
| 36 | + /// }; |
| 37 | + /// ``` |
| 38 | + /// |
| 39 | + /// But it does work with async closures: |
| 40 | + /// |
| 41 | + /// ```rust |
| 42 | + /// #![feature(async_closure)] |
| 43 | + /// |
| 44 | + /// async fn callback(x: &str) {} |
| 45 | + /// |
| 46 | + /// let captured_str = String::new(); |
| 47 | + /// let c = async move || { |
| 48 | + /// callback(&captured_str).await; |
| 49 | + /// }; |
| 50 | + /// ``` |
| 51 | + pub CLOSURE_RETURNING_ASYNC_BLOCK, |
| 52 | + Allow, |
| 53 | + "closure that returns `async {}` could be rewritten as an async closure", |
| 54 | + @feature_gate = async_closure; |
| 55 | +} |
| 56 | + |
| 57 | +declare_lint_pass!( |
| 58 | + /// Lint for potential usages of async closures and async fn trait bounds. |
| 59 | + AsyncClosureUsage => [CLOSURE_RETURNING_ASYNC_BLOCK] |
| 60 | +); |
| 61 | + |
| 62 | +impl<'tcx> LateLintPass<'tcx> for AsyncClosureUsage { |
| 63 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { |
| 64 | + let hir::ExprKind::Closure(&hir::Closure { |
| 65 | + body, |
| 66 | + kind: hir::ClosureKind::Closure, |
| 67 | + fn_decl_span, |
| 68 | + .. |
| 69 | + }) = expr.kind |
| 70 | + else { |
| 71 | + return; |
| 72 | + }; |
| 73 | + |
| 74 | + let mut body = cx.tcx.hir().body(body).value; |
| 75 | + |
| 76 | + // Only peel blocks that have no expressions. |
| 77 | + while let hir::ExprKind::Block(&hir::Block { stmts: [], expr: Some(tail), .. }, None) = |
| 78 | + body.kind |
| 79 | + { |
| 80 | + body = tail; |
| 81 | + } |
| 82 | + |
| 83 | + let hir::ExprKind::Closure(&hir::Closure { |
| 84 | + kind: |
| 85 | + hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared( |
| 86 | + hir::CoroutineDesugaring::Async, |
| 87 | + hir::CoroutineSource::Block, |
| 88 | + )), |
| 89 | + fn_decl_span: async_decl_span, |
| 90 | + .. |
| 91 | + }) = body.kind |
| 92 | + else { |
| 93 | + return; |
| 94 | + }; |
| 95 | + |
| 96 | + let deletion_span = cx.tcx.sess.source_map().span_extend_while_whitespace(async_decl_span); |
| 97 | + |
| 98 | + cx.tcx.emit_node_span_lint( |
| 99 | + CLOSURE_RETURNING_ASYNC_BLOCK, |
| 100 | + expr.hir_id, |
| 101 | + fn_decl_span, |
| 102 | + ClosureReturningAsyncBlock { |
| 103 | + async_decl_span, |
| 104 | + sugg: AsyncClosureSugg { |
| 105 | + deletion_span, |
| 106 | + insertion_span: fn_decl_span.shrink_to_lo(), |
| 107 | + }, |
| 108 | + }, |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[derive(LintDiagnostic)] |
| 114 | +#[diag(lint_closure_returning_async_block)] |
| 115 | +struct ClosureReturningAsyncBlock { |
| 116 | + #[label] |
| 117 | + async_decl_span: Span, |
| 118 | + #[subdiagnostic] |
| 119 | + sugg: AsyncClosureSugg, |
| 120 | +} |
| 121 | + |
| 122 | +#[derive(Subdiagnostic)] |
| 123 | +#[multipart_suggestion(lint_suggestion, applicability = "maybe-incorrect")] |
| 124 | +struct AsyncClosureSugg { |
| 125 | + #[suggestion_part(code = "")] |
| 126 | + deletion_span: Span, |
| 127 | + #[suggestion_part(code = "async ")] |
| 128 | + insertion_span: Span, |
| 129 | +} |
0 commit comments