2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2022-10-23 08:18:45 -05:00
|
|
|
use clippy_utils::match_function_call_with_def_id;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::source::{position_before_rarrow, snippet_block, snippet_opt};
|
2020-05-11 13:23:47 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::intravisit::FnKind;
|
|
|
|
use rustc_hir::{
|
2022-07-18 02:39:37 -05:00
|
|
|
AsyncGeneratorKind, Block, Body, Closure, Expr, ExprKind, FnDecl, FnRetTy, GeneratorKind, GenericArg, GenericBound,
|
2022-11-18 20:22:24 -06:00
|
|
|
HirId, ItemKind, LifetimeName, Term, TraitRef, Ty, TyKind, TypeBindingKind,
|
2020-05-11 13:23:47 -05:00
|
|
|
};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-01-15 03:56:44 -06:00
|
|
|
use rustc_span::{sym, Span};
|
2020-05-11 13:23:47 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// It checks for manual implementations of `async` functions.
|
2020-05-11 13:23:47 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It's more idiomatic to use the dedicated syntax.
|
2020-05-11 13:23:47 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-05-11 13:23:47 -05:00
|
|
|
/// ```rust
|
|
|
|
/// use std::future::Future;
|
|
|
|
///
|
|
|
|
/// fn foo() -> impl Future<Output = i32> { async { 42 } }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// async fn foo() -> i32 { 42 }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.45.0"]
|
2020-05-11 13:23:47 -05:00
|
|
|
pub MANUAL_ASYNC_FN,
|
|
|
|
style,
|
|
|
|
"manual implementations of `async` functions can be simplified using the dedicated syntax"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(ManualAsyncFn => [MANUAL_ASYNC_FN]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
|
2020-05-11 13:23:47 -05:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
2020-06-25 15:41:36 -05:00
|
|
|
cx: &LateContext<'tcx>,
|
2020-05-11 13:23:47 -05:00
|
|
|
kind: FnKind<'tcx>,
|
|
|
|
decl: &'tcx FnDecl<'_>,
|
|
|
|
body: &'tcx Body<'_>,
|
|
|
|
span: Span,
|
|
|
|
_: HirId,
|
|
|
|
) {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(header) = kind.header();
|
2022-11-18 20:22:24 -06:00
|
|
|
if !header.asyncness.is_async();
|
2020-05-11 13:23:47 -05:00
|
|
|
// Check that this function returns `impl Future`
|
|
|
|
if let FnRetTy::Return(ret_ty) = decl.output;
|
2020-08-11 08:43:21 -05:00
|
|
|
if let Some((trait_ref, output_lifetimes)) = future_trait_ref(cx, ret_ty);
|
2020-05-11 13:23:47 -05:00
|
|
|
if let Some(output) = future_output_ty(trait_ref);
|
2020-08-11 08:43:21 -05:00
|
|
|
if captures_all_lifetimes(decl.inputs, &output_lifetimes);
|
2020-05-11 13:23:47 -05:00
|
|
|
// Check that the body of the function consists of one async block
|
|
|
|
if let ExprKind::Block(block, _) = body.value.kind;
|
|
|
|
if block.stmts.is_empty();
|
|
|
|
if let Some(closure_body) = desugared_async_block(cx, block);
|
|
|
|
then {
|
|
|
|
let header_span = span.with_hi(ret_ty.span.hi());
|
|
|
|
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
MANUAL_ASYNC_FN,
|
|
|
|
header_span,
|
|
|
|
"this function can be simplified using the `async fn` syntax",
|
|
|
|
|diag| {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(header_snip) = snippet_opt(cx, header_span);
|
2021-01-02 09:29:43 -06:00
|
|
|
if let Some(ret_pos) = position_before_rarrow(&header_snip);
|
2020-05-11 13:23:47 -05:00
|
|
|
if let Some((ret_sugg, ret_snip)) = suggested_ret(cx, output);
|
|
|
|
then {
|
2022-10-06 02:44:38 -05:00
|
|
|
let help = format!("make the function `async` and {ret_sugg}");
|
2020-05-11 13:23:47 -05:00
|
|
|
diag.span_suggestion(
|
|
|
|
header_span,
|
2022-12-29 07:28:34 -06:00
|
|
|
help,
|
2022-10-06 02:44:38 -05:00
|
|
|
format!("async {}{ret_snip}", &header_snip[..ret_pos]),
|
2020-05-11 13:23:47 -05:00
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
|
|
|
|
let body_snip = snippet_block(cx, closure_body.value.span, "..", Some(block.span));
|
|
|
|
diag.span_suggestion(
|
|
|
|
block.span,
|
|
|
|
"move the body of the async block to the enclosing function",
|
2022-06-13 01:48:40 -05:00
|
|
|
body_snip,
|
2020-05-11 13:23:47 -05:00
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 08:43:21 -05:00
|
|
|
fn future_trait_ref<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
ty: &'tcx Ty<'tcx>,
|
|
|
|
) -> Option<(&'tcx TraitRef<'tcx>, Vec<LifetimeName>)> {
|
2020-05-11 13:23:47 -05:00
|
|
|
if_chain! {
|
2022-09-06 12:27:47 -05:00
|
|
|
if let TyKind::OpaqueDef(item_id, bounds, false) = ty.kind;
|
2021-01-30 05:06:04 -06:00
|
|
|
let item = cx.tcx.hir().item(item_id);
|
2020-05-11 13:23:47 -05:00
|
|
|
if let ItemKind::OpaqueTy(opaque) = &item.kind;
|
2020-08-11 08:43:21 -05:00
|
|
|
if let Some(trait_ref) = opaque.bounds.iter().find_map(|bound| {
|
|
|
|
if let GenericBound::Trait(poly, _) = bound {
|
|
|
|
Some(&poly.trait_ref)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if trait_ref.trait_def_id() == cx.tcx.lang_items().future_trait();
|
2020-05-11 13:23:47 -05:00
|
|
|
then {
|
2020-08-11 08:43:21 -05:00
|
|
|
let output_lifetimes = bounds
|
|
|
|
.iter()
|
|
|
|
.filter_map(|bound| {
|
|
|
|
if let GenericArg::Lifetime(lt) = bound {
|
2022-11-05 17:41:07 -05:00
|
|
|
Some(lt.res)
|
2020-08-11 08:43:21 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
return Some((trait_ref, output_lifetimes));
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn future_output_ty<'tcx>(trait_ref: &'tcx TraitRef<'tcx>) -> Option<&'tcx Ty<'tcx>> {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(segment) = trait_ref.path.segments.last();
|
|
|
|
if let Some(args) = segment.args;
|
|
|
|
if args.bindings.len() == 1;
|
|
|
|
let binding = &args.bindings[0];
|
2021-01-15 03:56:44 -06:00
|
|
|
if binding.ident.name == sym::Output;
|
2022-10-23 08:18:45 -05:00
|
|
|
if let TypeBindingKind::Equality { term: Term::Ty(output) } = binding.kind;
|
2020-05-11 13:23:47 -05:00
|
|
|
then {
|
2022-10-23 08:18:45 -05:00
|
|
|
return Some(output);
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-08-11 08:43:21 -05:00
|
|
|
fn captures_all_lifetimes(inputs: &[Ty<'_>], output_lifetimes: &[LifetimeName]) -> bool {
|
|
|
|
let input_lifetimes: Vec<LifetimeName> = inputs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|ty| {
|
2022-12-28 11:06:11 -06:00
|
|
|
if let TyKind::Ref(lt, _) = ty.kind {
|
2022-11-05 17:41:07 -05:00
|
|
|
Some(lt.res)
|
2020-08-11 08:43:21 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// The lint should trigger in one of these cases:
|
|
|
|
// - There are no input lifetimes
|
|
|
|
// - There's only one output lifetime bound using `+ '_`
|
|
|
|
// - All input lifetimes are explicitly bound to the output
|
|
|
|
input_lifetimes.is_empty()
|
2022-06-13 01:22:06 -05:00
|
|
|
|| (output_lifetimes.len() == 1 && matches!(output_lifetimes[0], LifetimeName::Infer))
|
2020-08-11 08:43:21 -05:00
|
|
|
|| input_lifetimes
|
|
|
|
.iter()
|
|
|
|
.all(|in_lt| output_lifetimes.iter().any(|out_lt| in_lt == out_lt))
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn desugared_async_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) -> Option<&'tcx Body<'tcx>> {
|
2020-05-11 13:23:47 -05:00
|
|
|
if_chain! {
|
|
|
|
if let Some(block_expr) = block.expr;
|
2022-10-23 08:18:45 -05:00
|
|
|
if let Some(args) = cx
|
|
|
|
.tcx
|
|
|
|
.lang_items()
|
2022-11-18 15:56:22 -06:00
|
|
|
.identity_future_fn()
|
2022-10-23 08:18:45 -05:00
|
|
|
.and_then(|def_id| match_function_call_with_def_id(cx, block_expr, def_id));
|
2020-05-11 13:23:47 -05:00
|
|
|
if args.len() == 1;
|
2022-10-23 08:18:45 -05:00
|
|
|
if let Expr {
|
|
|
|
kind: ExprKind::Closure(&Closure { body, .. }),
|
|
|
|
..
|
|
|
|
} = args[0];
|
2022-06-11 14:25:25 -05:00
|
|
|
let closure_body = cx.tcx.hir().body(body);
|
2021-10-07 04:21:30 -05:00
|
|
|
if closure_body.generator_kind == Some(GeneratorKind::Async(AsyncGeneratorKind::Block));
|
2020-05-11 13:23:47 -05:00
|
|
|
then {
|
|
|
|
return Some(closure_body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str, String)> {
|
2020-05-11 13:23:47 -05:00
|
|
|
match output.kind {
|
|
|
|
TyKind::Tup(tys) if tys.is_empty() => {
|
|
|
|
let sugg = "remove the return type";
|
2022-08-31 08:24:45 -05:00
|
|
|
Some((sugg, String::new()))
|
2020-05-11 13:23:47 -05:00
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let sugg = "return the output of the future directly";
|
2022-10-06 02:44:38 -05:00
|
|
|
snippet_opt(cx, output.span).map(|snip| (sugg, format!(" -> {snip}")))
|
2020-05-11 13:23:47 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|