]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/async_yields_async.rs
Rollup merge of #85760 - ChrisDenton:path-doc-platform-specific, r=m-ou-se
[rust.git] / src / tools / clippy / clippy_lints / src / async_yields_async.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet;
3 use clippy_utils::ty::implements_trait;
4 use rustc_errors::Applicability;
5 use rustc_hir::{AsyncGeneratorKind, Body, BodyId, ExprKind, GeneratorKind, QPath};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for async blocks that yield values of types
11     /// that can themselves be awaited.
12     ///
13     /// **Why is this bad?** An await is likely missing.
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     ///
19     /// ```rust
20     /// async fn foo() {}
21     ///
22     /// fn bar() {
23     ///   let x = async {
24     ///     foo()
25     ///   };
26     /// }
27     /// ```
28     /// Use instead:
29     /// ```rust
30     /// async fn foo() {}
31     ///
32     /// fn bar() {
33     ///   let x = async {
34     ///     foo().await
35     ///   };
36     /// }
37     /// ```
38     pub ASYNC_YIELDS_ASYNC,
39     correctness,
40     "async blocks that return a type that can be awaited"
41 }
42
43 declare_lint_pass!(AsyncYieldsAsync => [ASYNC_YIELDS_ASYNC]);
44
45 impl<'tcx> LateLintPass<'tcx> for AsyncYieldsAsync {
46     fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
47         use AsyncGeneratorKind::{Block, Closure};
48         // For functions, with explicitly defined types, don't warn.
49         // XXXkhuey maybe we should?
50         if let Some(GeneratorKind::Async(Block | Closure)) = body.generator_kind {
51             if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait() {
52                 let body_id = BodyId {
53                     hir_id: body.value.hir_id,
54                 };
55                 let typeck_results = cx.tcx.typeck_body(body_id);
56                 let expr_ty = typeck_results.expr_ty(&body.value);
57
58                 if implements_trait(cx, expr_ty, future_trait_def_id, &[]) {
59                     let return_expr_span = match &body.value.kind {
60                         // XXXkhuey there has to be a better way.
61                         ExprKind::Block(block, _) => block.expr.map(|e| e.span),
62                         ExprKind::Path(QPath::Resolved(_, path)) => Some(path.span),
63                         _ => None,
64                     };
65                     if let Some(return_expr_span) = return_expr_span {
66                         span_lint_and_then(
67                             cx,
68                             ASYNC_YIELDS_ASYNC,
69                             return_expr_span,
70                             "an async construct yields a type which is itself awaitable",
71                             |db| {
72                                 db.span_label(body.value.span, "outer async construct");
73                                 db.span_label(return_expr_span, "awaitable value not awaited");
74                                 db.span_suggestion(
75                                     return_expr_span,
76                                     "consider awaiting this value",
77                                     format!("{}.await", snippet(cx, return_expr_span, "..")),
78                                     Applicability::MaybeIncorrect,
79                                 );
80                             },
81                         );
82                     }
83                 }
84             }
85         }
86     }
87 }