Skip to content

Commit ac25636

Browse files
committed
fix RedundantLocals clippy caused by async and await
1 parent 75b9f53 commit ac25636

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

compiler/rustc_middle/src/lint.rs

+9
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,12 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
463463
ExpnKind::Macro { .. } => true, // definitely a plugin
464464
}
465465
}
466+
467+
/// Return whether `span` is generated by `async` or `await`.
468+
pub fn is_from_async_await(span: Span) -> bool {
469+
let expn_data = span.ctxt().outer_expn_data();
470+
match expn_data.kind {
471+
ExpnKind::Desugaring(DesugaringKind::Async | DesugaringKind::Await) => true,
472+
_ => false,
473+
}
474+
}

src/tools/clippy/clippy_lints/src/redundant_locals.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::is_from_proc_macro;
33
use clippy_utils::ty::needs_ordered_drop;
44
use rustc_hir::def::Res;
5-
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
5+
use rustc_hir::{
6+
BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath,
7+
};
68
use rustc_lint::{LateContext, LateLintPass, LintContext};
7-
use rustc_middle::lint::in_external_macro;
9+
use rustc_middle::lint::{in_external_macro, is_from_async_await};
810
use rustc_session::{declare_lint_pass, declare_tool_lint};
911
use rustc_span::symbol::Ident;
1012

@@ -65,6 +67,9 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
6567
// the local is user-controlled
6668
if !in_external_macro(cx.sess(), local.span);
6769
if !is_from_proc_macro(cx, expr);
70+
// Async function parameters are lowered into the closure body, so we can't lint them.
71+
// see `lower_maybe_async_body` in `rust_ast_lowering`
72+
if !is_from_async_await(local.span);
6873
then {
6974
span_lint_and_help(
7075
cx,
@@ -93,7 +98,12 @@ fn find_binding(pat: &Pat<'_>, name: Ident) -> Option<BindingAnnotation> {
9398
}
9499

95100
/// Check if a rebinding of a local affects the code's drop behavior.
96-
fn affects_drop_behavior<'tcx>(cx: &LateContext<'tcx>, bind: HirId, rebind: HirId, rebind_expr: &Expr<'tcx>) -> bool {
101+
fn affects_drop_behavior<'tcx>(
102+
cx: &LateContext<'tcx>,
103+
bind: HirId,
104+
rebind: HirId,
105+
rebind_expr: &Expr<'tcx>,
106+
) -> bool {
97107
let hir = cx.tcx.hir();
98108

99109
// the rebinding is in a different scope than the original binding

0 commit comments

Comments
 (0)