-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
unused_async.rs
191 lines (174 loc) · 6.62 KB
/
unused_async.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::is_def_id_trait_method;
use rustc_hir::def::DefKind;
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr, walk_fn};
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId, Node, YieldSource};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter;
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::def_id::{LocalDefId, LocalDefIdSet};
declare_clippy_lint! {
/// ### What it does
/// Checks for functions that are declared `async` but have no `.await`s inside of them.
///
/// ### Why is this bad?
/// Async functions with no async code create overhead, both mentally and computationally.
/// Callers of async methods either need to be calling from an async function themselves or run it on an executor, both of which
/// causes runtime overhead and hassle for the caller.
///
/// ### Example
/// ```no_run
/// async fn get_random_number() -> i64 {
/// 4 // Chosen by fair dice roll. Guaranteed to be random.
/// }
/// let number_future = get_random_number();
/// ```
///
/// Use instead:
/// ```no_run
/// fn get_random_number_improved() -> i64 {
/// 4 // Chosen by fair dice roll. Guaranteed to be random.
/// }
/// let number_future = async { get_random_number_improved() };
/// ```
#[clippy::version = "1.54.0"]
pub UNUSED_ASYNC,
pedantic,
"finds async functions with no await statements"
}
#[derive(Default)]
pub struct UnusedAsync {
/// Keeps track of async functions used as values (i.e. path expressions to async functions that
/// are not immediately called)
async_fns_as_value: LocalDefIdSet,
/// Functions with unused `async`, linted post-crate after we've found all uses of local async
/// functions
unused_async_fns: Vec<UnusedAsyncFn>,
}
#[derive(Copy, Clone)]
struct UnusedAsyncFn {
def_id: LocalDefId,
fn_span: Span,
await_in_async_block: Option<Span>,
}
impl_lint_pass!(UnusedAsync => [UNUSED_ASYNC]);
struct AsyncFnVisitor<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
found_await: bool,
/// Also keep track of `await`s in nested async blocks so we can mention
/// it in a note
await_in_async_block: Option<Span>,
async_depth: usize,
}
impl<'tcx> Visitor<'tcx> for AsyncFnVisitor<'_, 'tcx> {
type NestedFilter = nested_filter::OnlyBodies;
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
if let ExprKind::Yield(_, YieldSource::Await { .. }) = ex.kind {
if self.async_depth == 1 {
self.found_await = true;
} else if self.await_in_async_block.is_none() {
self.await_in_async_block = Some(ex.span);
}
}
let is_async_block = matches!(
ex.kind,
ExprKind::Closure(rustc_hir::Closure {
kind: rustc_hir::ClosureKind::Coroutine(rustc_hir::CoroutineKind::Desugared(
rustc_hir::CoroutineDesugaring::Async,
_
)),
..
})
);
if is_async_block {
self.async_depth += 1;
}
walk_expr(self, ex);
if is_async_block {
self.async_depth -= 1;
}
}
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
}
}
impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
fn_kind: FnKind<'tcx>,
fn_decl: &'tcx FnDecl<'tcx>,
body: &Body<'tcx>,
span: Span,
def_id: LocalDefId,
) {
if !span.from_expansion() && fn_kind.asyncness().is_async() && !is_def_id_trait_method(cx, def_id) {
let mut visitor = AsyncFnVisitor {
cx,
found_await: false,
async_depth: 0,
await_in_async_block: None,
};
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), def_id);
if !visitor.found_await {
// Don't lint just yet, but store the necessary information for later.
// The actual linting happens in `check_crate_post`, once we've found all
// uses of local async functions that do require asyncness to pass typeck
self.unused_async_fns.push(UnusedAsyncFn {
await_in_async_block: visitor.await_in_async_block,
fn_span: span,
def_id,
});
}
}
}
fn check_path(&mut self, cx: &LateContext<'tcx>, path: &rustc_hir::Path<'tcx>, hir_id: HirId) {
// Find paths to local async functions that aren't immediately called.
// E.g. `async fn f() {}; let x = f;`
// Depending on how `x` is used, f's asyncness might be required despite not having any `await`
// statements, so don't lint at all if there are any such paths.
if let Some(def_id) = path.res.opt_def_id()
&& let Some(local_def_id) = def_id.as_local()
&& cx.tcx.def_kind(def_id) == DefKind::Fn
&& cx.tcx.asyncness(def_id).is_async()
&& let parent = cx.tcx.parent_hir_node(hir_id)
&& !matches!(
parent,
Node::Expr(Expr {
kind: ExprKind::Call(Expr { span, .. }, _),
..
}) if *span == path.span
)
{
self.async_fns_as_value.insert(local_def_id);
}
}
// After collecting all unused `async` and problematic paths to such functions,
// lint those unused ones that didn't have any path expressions to them.
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
let iter = self
.unused_async_fns
.iter()
.filter(|UnusedAsyncFn { def_id, .. }| (!self.async_fns_as_value.contains(def_id)));
for fun in iter {
span_lint_hir_and_then(
cx,
UNUSED_ASYNC,
cx.tcx.local_def_id_to_hir_id(fun.def_id),
fun.fn_span,
"unused `async` for function with no await statements",
|diag| {
diag.help("consider removing the `async` from this function");
if let Some(span) = fun.await_in_async_block {
diag.span_note(
span,
"`await` used in an async block, which does not require \
the enclosing function to be `async`",
);
}
},
);
}
}
}