Skip to content

Commit d17eeb1

Browse files
committed
Bless clippy.
1 parent 0e3ae7a commit d17eeb1

5 files changed

Lines changed: 35 additions & 43 deletions

File tree

src/tools/clippy/clippy_lints/src/doc/missing_headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn check(
8383
Some(owner_id.def_id.to_def_id()),
8484
&[],
8585
)
86-
&& let ty::Coroutine(_, subs) = ret_ty.kind()
86+
&& let Some((_, subs)) = cx.tcx.try_unwrap_desugared_coroutine(ret_ty)
8787
&& subs.as_coroutine().return_ty().is_diag_item(cx, sym::Result)
8888
{
8989
span_lint(

src/tools/clippy/clippy_lints/src/implicit_return.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitReturn {
229229
_: LocalDefId,
230230
) {
231231
if (!matches!(kind, FnKind::Closure) && matches!(decl.output, FnRetTy::DefaultReturn(_)))
232-
|| !span.eq_ctxt(body.value.span)
233232
|| span.in_external_macro(cx.sess().source_map())
234233
{
235234
return;
@@ -251,7 +250,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitReturn {
251250
body.value
252251
};
253252

254-
if is_from_proc_macro(cx, expr) {
253+
if is_from_proc_macro(cx, expr) || !span.eq_ctxt(expr.span) {
255254
return;
256255
}
257256
lint_implicit_returns(cx, expr, expr.span.ctxt(), None);

src/tools/clippy/clippy_lints/src/manual_async_fn.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::desugared_async_block;
23
use clippy_utils::source::{SpanRangeExt, position_before_rarrow, snippet_block};
34
use rustc_errors::Applicability;
45
use rustc_hir::intravisit::FnKind;
5-
use rustc_hir::{
6-
Block, Body, Closure, ClosureKind, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, FnDecl,
7-
FnRetTy, GenericBound, Node, OpaqueTy, TraitRef, Ty, TyKind,
8-
};
6+
use rustc_hir::{Body, ExprKind, FnDecl, FnRetTy, GenericBound, Node, OpaqueTy, TraitRef, Ty, TyKind};
97
use rustc_lint::{LateContext, LateLintPass};
108
use rustc_middle::middle::resolve_bound_vars::ResolvedArg;
119
use rustc_middle::ty;
@@ -59,7 +57,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
5957
// Check that the body of the function consists of one async block
6058
&& let ExprKind::Block(block, _) = body.value.kind
6159
&& block.stmts.is_empty()
62-
&& let Some(closure_body) = desugared_async_block(cx, block)
60+
&& let Some(body_expr) = block.expr
61+
&& let Some((_, closure_body)) = desugared_async_block(cx, body_expr)
6362
&& let Some(vis_span_opt) = match cx.tcx.hir_node_by_def_id(fn_def_id) {
6463
Node::Item(item) => Some(Some(item.vis_span)),
6564
Node::ImplItem(impl_item) => Some(impl_item.vis_span()),
@@ -165,20 +164,6 @@ fn captures_all_lifetimes(cx: &LateContext<'_>, fn_def_id: LocalDefId, opaque_de
165164
num_captured_lifetimes == num_early_lifetimes + num_late_lifetimes
166165
}
167166

168-
fn desugared_async_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) -> Option<&'tcx Body<'tcx>> {
169-
if let Some(&Expr {
170-
kind: ExprKind::Closure(&Closure { kind, body, .. }),
171-
..
172-
}) = block.expr
173-
&& let ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, CoroutineSource::Block)) =
174-
kind
175-
{
176-
return Some(cx.tcx.hir_body(body));
177-
}
178-
179-
None
180-
}
181-
182167
fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str, String)> {
183168
if let TyKind::Tup([]) = output.kind {
184169
let sugg = "remove the return type";

src/tools/clippy/clippy_lints/src/redundant_async_block.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::{snippet, walk_span_to_context};
33
use clippy_utils::ty::implements_trait;
4-
use clippy_utils::{desugar_await, peel_blocks};
4+
use clippy_utils::{desugar_await, desugared_async_block, peel_blocks};
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Closure, ClosureKind, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind};
6+
use rustc_hir::Expr;
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty::UpvarCapture;
99
use rustc_session::declare_lint_pass;
@@ -70,27 +70,19 @@ impl<'tcx> LateLintPass<'tcx> for RedundantAsyncBlock {
7070
/// If `expr` is a desugared `async` block, return the original expression if it does not capture
7171
/// any variable by ref.
7272
fn desugar_async_block<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
73-
if let ExprKind::Closure(Closure { body, def_id, kind, .. }) = expr.kind
74-
&& let body = cx.tcx.hir_body(*body)
75-
&& matches!(
76-
kind,
77-
ClosureKind::Coroutine(CoroutineKind::Desugared(
78-
CoroutineDesugaring::Async,
79-
CoroutineSource::Block
80-
))
81-
)
82-
{
83-
cx.typeck_results()
73+
let (def_id, body) = desugared_async_block(cx, expr)?;
74+
if cx.typeck_results()
8475
.closure_min_captures
85-
.get(def_id)
76+
.get(&def_id)
8677
.is_none_or(|m| {
8778
m.values().all(|places| {
8879
places
8980
.iter()
9081
.all(|place| matches!(place.info.capture_kind, UpvarCapture::ByValue))
9182
})
9283
})
93-
.then_some(body.value)
84+
{
85+
Some(body.value)
9486
} else {
9587
None
9688
}

src/tools/clippy/clippy_utils/src/lib.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ use rustc_middle::ty::{
114114
use rustc_span::hygiene::{ExpnKind, MacroKind};
115115
use rustc_span::source_map::SourceMap;
116116
use rustc_span::symbol::{Ident, Symbol, kw};
117-
use rustc_span::{InnerSpan, Span, SyntaxContext};
117+
use rustc_span::{InnerSpan, Span, SyntaxContext, DesugaringKind};
118118
use source::{SpanRangeExt, walk_span_to_context};
119119
use visitors::{Visitable, for_each_unconsumed_temporary};
120120

@@ -1791,11 +1791,13 @@ pub fn if_sequence<'tcx>(mut expr: &'tcx Expr<'tcx>) -> (Vec<&'tcx Expr<'tcx>>,
17911791

17921792
/// Peels away all the compiler generated code surrounding the body of an async closure.
17931793
pub fn get_async_closure_expr<'tcx>(tcx: TyCtxt<'tcx>, expr: &Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
1794-
if let ExprKind::Closure(&Closure {
1795-
body,
1796-
kind: hir::ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)),
1797-
..
1798-
}) = expr.kind
1794+
if expr.span.is_desugaring(DesugaringKind::Async)
1795+
&& let ExprKind::Call(_, [closure]) = expr.kind
1796+
&& let ExprKind::Closure(&Closure {
1797+
body,
1798+
kind: hir::ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)),
1799+
..
1800+
}) = closure.kind
17991801
&& let ExprKind::Block(
18001802
Block {
18011803
expr:
@@ -1819,6 +1821,20 @@ pub fn get_async_fn_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Option<&'t
18191821
get_async_closure_expr(tcx, body.value)
18201822
}
18211823

1824+
/// If `expr` is a desugared `async` block, return the original expression.
1825+
pub fn desugared_async_block<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<(LocalDefId, &'tcx Body<'tcx>)> {
1826+
if expr.span.is_desugaring(DesugaringKind::Async)
1827+
&& let ExprKind::Call(_, [closure]) = expr.kind
1828+
&& let ExprKind::Closure(&Closure { kind, body, def_id, .. }) = closure.kind
1829+
&& let hir::ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, CoroutineSource::Block)) =
1830+
kind
1831+
{
1832+
return Some((def_id, cx.tcx.hir_body(body)));
1833+
}
1834+
1835+
None
1836+
}
1837+
18221838
// check if expr is calling method or function with #[must_use] attribute
18231839
pub fn is_must_use_func_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
18241840
let did = match expr.kind {

0 commit comments

Comments
 (0)