Skip to content

Commit

Permalink
redundant_closure ignore coerced closure
Browse files Browse the repository at this point in the history
  • Loading branch information
dswij committed Feb 28, 2022
1 parent 8d12cd4 commit 4f5e96e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
4 changes: 3 additions & 1 deletion 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::{get_enclosing_loop_or_closure, higher, is_adjusted, 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 @@ -103,6 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
let closure_ty = cx.typeck_results().expr_ty(expr);

if_chain!(
if !is_adjusted(cx, &body.value);
if let ExprKind::Call(callee, args) = body.value.kind;
if let ExprKind::Path(_) = callee.kind;
if check_inputs(cx, body.params, args);
Expand Down Expand Up @@ -145,6 +146,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
);

if_chain!(
if !is_adjusted(cx, &body.value);
if let ExprKind::MethodCall(path, args, _) = body.value.kind;
if check_inputs(cx, body.params, args);
let method_def_id = cx.typeck_results().type_dependent_def_id(body.value.hir_id).unwrap();
Expand Down
16 changes: 14 additions & 2 deletions tests/ui/eta.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() {
}

// See #815
let e = Some(1u8).map(divergent);
let e = Some(1u8).map(|a| divergent(a));
let e = Some(1u8).map(generic);
let e = Some(1u8).map(generic);
// See #515
Expand Down Expand Up @@ -229,7 +229,7 @@ fn late_bound_lifetimes() {
{
}
map_str(|s| take_asref_path(s));
map_str_to_path(std::convert::AsRef::as_ref);
map_str_to_path(|s| s.as_ref());
}

mod type_param_bound {
Expand Down Expand Up @@ -275,3 +275,15 @@ mod bind_by_ref {
Some(A).map(|ref a| B::from(a));
}
}

// #7812 False positive on coerced closure
fn coerced_closure() {
fn function_returning_unit<F: FnMut(i32)>(f: F) {}
function_returning_unit(|x| std::process::exit(x));

fn arr() -> &'static [u8; 0] {
&[]
}
fn slice_fn(_: impl FnOnce() -> &'static [u8]) {}
slice_fn(|| arr());
}
12 changes: 12 additions & 0 deletions tests/ui/eta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,15 @@ mod bind_by_ref {
Some(A).map(|ref a| B::from(a));
}
}

// #7812 False positive on coerced closure
fn coerced_closure() {
fn function_returning_unit<F: FnMut(i32)>(f: F) {}
function_returning_unit(|x| std::process::exit(x));

fn arr() -> &'static [u8; 0] {
&[]
}
fn slice_fn(_: impl FnOnce() -> &'static [u8]) {}
slice_fn(|| arr());
}
14 changes: 1 addition & 13 deletions tests/ui/eta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ error: redundant closure
LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
| ^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `below`

error: redundant closure
--> $DIR/eta.rs:40:27
|
LL | let e = Some(1u8).map(|a| divergent(a));
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `divergent`

error: redundant closure
--> $DIR/eta.rs:41:27
|
Expand Down Expand Up @@ -116,11 +110,5 @@ error: redundant closure
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
|
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 18 previous errors

0 comments on commit 4f5e96e

Please sign in to comment.