2021-05-20 05:30:31 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2022-01-15 16:07:52 -06:00
|
|
|
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor};
|
2023-01-22 12:00:33 -06:00
|
|
|
use rustc_hir::{Body, Expr, ExprKind, FnDecl, YieldSource};
|
2021-05-20 05:30:31 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-01-15 16:07:52 -06:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2021-05-20 05:30:31 -05:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2023-01-22 12:00:33 -06:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2021-05-20 05:30:31 -05:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for functions that are declared `async` but have no `.await`s inside of them.
|
2021-05-20 05:30:31 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Async functions with no async code create overhead, both mentally and computationally.
|
2021-05-20 05:30:31 -05:00
|
|
|
/// 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.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2021-05-20 05:30:31 -05:00
|
|
|
/// ```rust
|
|
|
|
/// async fn get_random_number() -> i64 {
|
|
|
|
/// 4 // Chosen by fair dice roll. Guaranteed to be random.
|
|
|
|
/// }
|
|
|
|
/// let number_future = get_random_number();
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```
|
2021-05-20 05:30:31 -05:00
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2021-05-20 05:30:31 -05:00
|
|
|
/// fn get_random_number_improved() -> i64 {
|
|
|
|
/// 4 // Chosen by fair dice roll. Guaranteed to be random.
|
|
|
|
/// }
|
|
|
|
/// let number_future = async { get_random_number_improved() };
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.54.0"]
|
2021-05-20 05:30:31 -05:00
|
|
|
pub UNUSED_ASYNC,
|
|
|
|
pedantic,
|
|
|
|
"finds async functions with no await statements"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(UnusedAsync => [UNUSED_ASYNC]);
|
|
|
|
|
|
|
|
struct AsyncFnVisitor<'a, 'tcx> {
|
|
|
|
cx: &'a LateContext<'tcx>,
|
|
|
|
found_await: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> {
|
2022-01-15 16:07:52 -06:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2021-05-20 05:30:31 -05:00
|
|
|
|
|
|
|
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
|
|
|
|
if let ExprKind::Yield(_, YieldSource::Await { .. }) = ex.kind {
|
|
|
|
self.found_await = true;
|
|
|
|
}
|
|
|
|
walk_expr(self, ex);
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:07:52 -06:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.cx.tcx.hir()
|
2021-05-20 05:30:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-01-22 12:00:33 -06:00
|
|
|
def_id: LocalDefId,
|
2021-05-20 05:30:31 -05:00
|
|
|
) {
|
2022-11-18 20:22:24 -06:00
|
|
|
if !span.from_expansion() && fn_kind.asyncness().is_async() {
|
2022-06-30 03:50:09 -05:00
|
|
|
let mut visitor = AsyncFnVisitor { cx, found_await: false };
|
2023-01-22 12:00:33 -06:00
|
|
|
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), def_id);
|
2022-06-30 03:50:09 -05:00
|
|
|
if !visitor.found_await {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
UNUSED_ASYNC,
|
|
|
|
span,
|
|
|
|
"unused `async` for function with no await statements",
|
|
|
|
None,
|
|
|
|
"consider removing the `async` from this function",
|
|
|
|
);
|
2021-05-20 05:30:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|