]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_async.rs
Auto merge of #7794 - ThibsG:FieldReassignDefault6312, r=llogiq
[rust.git] / clippy_lints / src / unused_async.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor};
3 use rustc_hir::{Body, Expr, ExprKind, FnDecl, FnHeader, HirId, IsAsync, YieldSource};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_middle::hir::map::Map;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::Span;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks for functions that are declared `async` but have no `.await`s inside of them.
12     ///
13     /// ### Why is this bad?
14     /// Async functions with no async code create overhead, both mentally and computationally.
15     /// Callers of async methods either need to be calling from an async function themselves or run it on an executor, both of which
16     /// causes runtime overhead and hassle for the caller.
17     ///
18     /// ### Example
19     /// ```rust
20     /// // Bad
21     /// async fn get_random_number() -> i64 {
22     ///     4 // Chosen by fair dice roll. Guaranteed to be random.
23     /// }
24     /// let number_future = get_random_number();
25     ///
26     /// // Good
27     /// fn get_random_number_improved() -> i64 {
28     ///     4 // Chosen by fair dice roll. Guaranteed to be random.
29     /// }
30     /// let number_future = async { get_random_number_improved() };
31     /// ```
32     pub UNUSED_ASYNC,
33     pedantic,
34     "finds async functions with no await statements"
35 }
36
37 declare_lint_pass!(UnusedAsync => [UNUSED_ASYNC]);
38
39 struct AsyncFnVisitor<'a, 'tcx> {
40     cx: &'a LateContext<'tcx>,
41     found_await: bool,
42 }
43
44 impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> {
45     type Map = Map<'tcx>;
46
47     fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
48         if let ExprKind::Yield(_, YieldSource::Await { .. }) = ex.kind {
49             self.found_await = true;
50         }
51         walk_expr(self, ex);
52     }
53
54     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
55         NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
56     }
57 }
58
59 impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
60     fn check_fn(
61         &mut self,
62         cx: &LateContext<'tcx>,
63         fn_kind: FnKind<'tcx>,
64         fn_decl: &'tcx FnDecl<'tcx>,
65         body: &Body<'tcx>,
66         span: Span,
67         hir_id: HirId,
68     ) {
69         if let FnKind::ItemFn(_, _, FnHeader { asyncness, .. }, _) = &fn_kind {
70             if matches!(asyncness, IsAsync::Async) {
71                 let mut visitor = AsyncFnVisitor { cx, found_await: false };
72                 walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), span, hir_id);
73                 if !visitor.found_await {
74                     span_lint_and_help(
75                         cx,
76                         UNUSED_ASYNC,
77                         span,
78                         "unused `async` for function with no await statements",
79                         None,
80                         "consider removing the `async` from this function",
81                     );
82                 }
83             }
84         }
85     }
86 }