Skip to content

Check for loops/closures in local_used_after_expr #8676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::higher::VecArgs;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::usage::local_used_after_expr;
use clippy_utils::{get_enclosing_loop_or_closure, higher, path_to_local, path_to_local_id};
use clippy_utils::{higher, path_to_local, path_to_local_id};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -125,8 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
if_chain! {
if let ty::Closure(_, substs) = callee_ty.peel_refs().kind();
if substs.as_closure().kind() == ClosureKind::FnMut;
if get_enclosing_loop_or_closure(cx.tcx, expr).is_some()
|| path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, callee));
if path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr));

then {
// Mutable closure is used after current expr; we cannot consume it.
Expand Down
33 changes: 31 additions & 2 deletions clippy_utils/src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::visitors::{expr_visitor, expr_visitor_no_bodies};
use rustc_hir as hir;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::HirIdSet;
use rustc_hir::{Expr, ExprKind, HirId};
use rustc_hir::{Expr, ExprKind, HirId, Node};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::LateContext;
use rustc_middle::hir::nested_filter;
Expand Down Expand Up @@ -169,6 +169,32 @@ pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {

pub fn local_used_after_expr(cx: &LateContext<'_>, local_id: HirId, after: &Expr<'_>) -> bool {
let Some(block) = utils::get_enclosing_block(cx, local_id) else { return false };

// for _ in 1..3 {
// local
// }
//
// let closure = || local;
// closure();
// closure();
let in_loop_or_closure = cx
.tcx
.hir()
.parent_iter(after.hir_id)
.take_while(|&(id, _)| id != block.hir_id)
.any(|(_, node)| {
matches!(
node,
Node::Expr(Expr {
kind: ExprKind::Loop(..) | ExprKind::Closure(..),
..
})
)
});
if in_loop_or_closure {
return true;
}

let mut used_after_expr = false;
let mut past_expr = false;
expr_visitor(cx, |expr| {
Expand All @@ -178,7 +204,10 @@ pub fn local_used_after_expr(cx: &LateContext<'_>, local_id: HirId, after: &Expr

if expr.hir_id == after.hir_id {
past_expr = true;
} else if past_expr && utils::path_to_local_id(expr, local_id) {
return false;
}

if past_expr && utils::path_to_local_id(expr, local_id) {
used_after_expr = true;
}
!used_after_expr
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/eta.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ fn mutable_closure_in_loop() {
let mut closure = |n| value += n;
for _ in 0..5 {
Some(1).map(&mut closure);

let mut value = 0;
let mut in_loop = |n| value += n;
Some(1).map(in_loop);
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/eta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ fn mutable_closure_in_loop() {
let mut closure = |n| value += n;
for _ in 0..5 {
Some(1).map(|n| closure(n));

let mut value = 0;
let mut in_loop = |n| value += n;
Some(1).map(|n| in_loop(n));
}
}

Expand Down
10 changes: 8 additions & 2 deletions tests/ui/eta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ LL | Some(1).map(|n| closure(n));
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut closure`

error: redundant closure
--> $DIR/eta.rs:232:21
--> $DIR/eta.rs:217:21
|
LL | Some(1).map(|n| in_loop(n));
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `in_loop`

error: redundant closure
--> $DIR/eta.rs:236:21
|
LL | map_str_to_path(|s| s.as_ref());
| ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::convert::AsRef::as_ref`

error: aborting due to 20 previous errors
error: aborting due to 21 previous errors

14 changes: 14 additions & 0 deletions tests/ui/needless_option_as_deref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ fn main() {
let _ = Some(Box::new(1)).as_deref();
let _ = Some(Box::new(1)).as_deref_mut();

let mut y = 0;
let mut x = Some(&mut y);
for _ in 0..3 {
let _ = x.as_deref_mut();
}

let mut y = 0;
let mut x = Some(&mut y);
let mut closure = || {
let _ = x.as_deref_mut();
};
closure();
closure();

// #7846
let mut i = 0;
let mut opt_vec = vec![Some(&mut i)];
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/needless_option_as_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ fn main() {
let _ = Some(Box::new(1)).as_deref();
let _ = Some(Box::new(1)).as_deref_mut();

let mut y = 0;
let mut x = Some(&mut y);
for _ in 0..3 {
let _ = x.as_deref_mut();
}

let mut y = 0;
let mut x = Some(&mut y);
let mut closure = || {
let _ = x.as_deref_mut();
};
closure();
closure();

// #7846
let mut i = 0;
let mut opt_vec = vec![Some(&mut i)];
Expand Down