Skip to content

Commit 6807977

Browse files
committedFeb 5, 2024
also check for coroutines
1 parent 7c3908f commit 6807977

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed
 

‎clippy_lints/src/redundant_locals.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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_ast::Mutability;
5-
use rustc_hir::def::{DefKind, Res};
5+
use rustc_hir::def::Res;
66
use rustc_hir::{BindingAnnotation, ByRef, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
77
use rustc_hir_typeck::expr_use_visitor::PlaceBase;
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -71,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
7171
// the local is user-controlled
7272
&& !in_external_macro(cx.sess(), local.span)
7373
&& !is_from_proc_macro(cx, expr)
74-
&& !is_closure_capture(cx, local.hir_id, binding_id)
74+
&& !is_by_value_closure_capture(cx, local.hir_id, binding_id)
7575
{
7676
span_lint_and_help(
7777
cx,
@@ -98,16 +98,14 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
9898
/// };
9999
/// assert_static(closure);
100100
/// ```
101-
fn is_closure_capture(cx: &LateContext<'_>, redefinition: HirId, root_variable: HirId) -> bool {
102-
let body = cx.tcx.hir().enclosing_body_owner(redefinition);
103-
if let DefKind::Closure = cx.tcx.def_kind(body) {
104-
cx.tcx.closure_captures(body).iter().any(|c| {
101+
fn is_by_value_closure_capture(cx: &LateContext<'_>, redefinition: HirId, root_variable: HirId) -> bool {
102+
let closure_def_id = cx.tcx.hir().enclosing_body_owner(redefinition);
103+
104+
cx.tcx.is_closure_or_coroutine(closure_def_id.to_def_id())
105+
&& cx.tcx.closure_captures(closure_def_id).iter().any(|c| {
105106
matches!(c.info.capture_kind, UpvarCapture::ByValue)
106107
&& matches!(c.place.base, PlaceBase::Upvar(upvar) if upvar.var_path.hir_id == root_variable)
107108
})
108-
} else {
109-
false
110-
}
111109
}
112110

113111
/// Find the annotation of a binding introduced by a pattern, or `None` if it's not introduced.

‎tests/ui/redundant_locals.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@aux-build:proc_macros.rs
22
#![allow(unused, clippy::no_effect, clippy::needless_pass_by_ref_mut)]
33
#![warn(clippy::redundant_locals)]
4-
#![feature(async_closure)]
4+
#![feature(async_closure, coroutines)]
55

66
extern crate proc_macros;
77
use proc_macros::{external, with_span};
@@ -172,6 +172,8 @@ fn issue12225() {
172172
let v2 = String::new();
173173
let v3 = String::new();
174174
let v4 = String::new();
175+
let v5 = String::new();
176+
let v6 = String::new();
175177

176178
assert_static(|| {
177179
let v1 = v1;
@@ -189,6 +191,14 @@ fn issue12225() {
189191
let v4 = v4;
190192
dbg!(&v4);
191193
});
194+
assert_static(static || {
195+
let v5 = v5;
196+
yield;
197+
});
198+
assert_static(|| {
199+
let v6 = v6;
200+
yield;
201+
});
192202

193203
fn foo(a: &str, b: &str) {}
194204

0 commit comments

Comments
 (0)