Skip to content

Commit

Permalink
also check for coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Feb 5, 2024
1 parent 7c3908f commit 6807977
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 7 additions & 9 deletions clippy_lints/src/redundant_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_from_proc_macro;
use clippy_utils::ty::needs_ordered_drop;
use rustc_ast::Mutability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def::Res;
use rustc_hir::{BindingAnnotation, ByRef, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
use rustc_hir_typeck::expr_use_visitor::PlaceBase;
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
// the local is user-controlled
&& !in_external_macro(cx.sess(), local.span)
&& !is_from_proc_macro(cx, expr)
&& !is_closure_capture(cx, local.hir_id, binding_id)
&& !is_by_value_closure_capture(cx, local.hir_id, binding_id)
{
span_lint_and_help(
cx,
Expand All @@ -98,16 +98,14 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
/// };
/// assert_static(closure);
/// ```
fn is_closure_capture(cx: &LateContext<'_>, redefinition: HirId, root_variable: HirId) -> bool {
let body = cx.tcx.hir().enclosing_body_owner(redefinition);
if let DefKind::Closure = cx.tcx.def_kind(body) {
cx.tcx.closure_captures(body).iter().any(|c| {
fn is_by_value_closure_capture(cx: &LateContext<'_>, redefinition: HirId, root_variable: HirId) -> bool {
let closure_def_id = cx.tcx.hir().enclosing_body_owner(redefinition);

cx.tcx.is_closure_or_coroutine(closure_def_id.to_def_id())
&& cx.tcx.closure_captures(closure_def_id).iter().any(|c| {
matches!(c.info.capture_kind, UpvarCapture::ByValue)
&& matches!(c.place.base, PlaceBase::Upvar(upvar) if upvar.var_path.hir_id == root_variable)
})
} else {
false
}
}

/// Find the annotation of a binding introduced by a pattern, or `None` if it's not introduced.
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/redundant_locals.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@aux-build:proc_macros.rs
#![allow(unused, clippy::no_effect, clippy::needless_pass_by_ref_mut)]
#![warn(clippy::redundant_locals)]
#![feature(async_closure)]
#![feature(async_closure, coroutines)]

extern crate proc_macros;
use proc_macros::{external, with_span};
Expand Down Expand Up @@ -172,6 +172,8 @@ fn issue12225() {
let v2 = String::new();
let v3 = String::new();
let v4 = String::new();
let v5 = String::new();
let v6 = String::new();

assert_static(|| {
let v1 = v1;
Expand All @@ -189,6 +191,14 @@ fn issue12225() {
let v4 = v4;
dbg!(&v4);
});
assert_static(static || {
let v5 = v5;
yield;
});
assert_static(|| {
let v6 = v6;
yield;
});

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

Expand Down

0 comments on commit 6807977

Please sign in to comment.