Skip to content

Commit 929c5f3

Browse files
committed
redundant_closure ignore coerced closure
1 parent 0d52107 commit 929c5f3

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

clippy_lints/src/eta_reduction.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::higher::VecArgs;
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::usage::local_used_after_expr;
6-
use clippy_utils::{get_enclosing_loop_or_closure, higher, path_to_local, path_to_local_id};
6+
use clippy_utils::{get_enclosing_loop_or_closure, higher, is_adjusted, path_to_local, path_to_local_id};
77
use if_chain::if_chain;
88
use rustc_errors::Applicability;
99
use rustc_hir::def_id::DefId;
@@ -102,6 +102,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
102102
let closure_ty = cx.typeck_results().expr_ty(expr);
103103

104104
if_chain!(
105+
if !is_adjusted(cx, &body.value);
105106
if let ExprKind::Call(callee, args) = body.value.kind;
106107
if let ExprKind::Path(_) = callee.kind;
107108
if check_inputs(cx, body.params, args);
@@ -144,6 +145,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
144145
);
145146

146147
if_chain!(
148+
if !is_adjusted(cx, &body.value);
147149
if let ExprKind::MethodCall(path, args, _) = body.value.kind;
148150
if check_inputs(cx, body.params, args);
149151
let method_def_id = cx.typeck_results().type_dependent_def_id(body.value.hir_id).unwrap();

tests/ui/eta.fixed

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() {
3737
}
3838

3939
// See #815
40-
let e = Some(1u8).map(divergent);
40+
let e = Some(1u8).map(|a| divergent(a));
4141
let e = Some(1u8).map(generic);
4242
let e = Some(1u8).map(generic);
4343
// See #515
@@ -229,7 +229,7 @@ fn late_bound_lifetimes() {
229229
{
230230
}
231231
map_str(|s| take_asref_path(s));
232-
map_str_to_path(std::convert::AsRef::as_ref);
232+
map_str_to_path(|s| s.as_ref());
233233
}
234234

235235
mod type_param_bound {
@@ -256,3 +256,14 @@ fn arc_fp() {
256256
(0..5).map(|n| arc(n));
257257
Some(4).map(|n| ref_arc(n));
258258
}
259+
260+
261+
// #7812 False positive on coerced closure
262+
fn coerced_closure() {
263+
fn function_returning_unit<F: FnMut(i32)>(f: F) {}
264+
function_returning_unit(|x| std::process::exit(x));
265+
266+
fn arr() -> &'static [u8; 0] { &[] }
267+
fn slice_fn(_: impl FnOnce() -> &'static [u8]) {}
268+
slice_fn(|| arr());
269+
}

tests/ui/eta.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,15 @@ fn arc_fp() {
256256
(0..5).map(|n| arc(n));
257257
Some(4).map(|n| ref_arc(n));
258258
}
259+
260+
// #7812 False positive on coerced closure
261+
fn coerced_closure() {
262+
fn function_returning_unit<F: FnMut(i32)>(f: F) {}
263+
function_returning_unit(|x| std::process::exit(x));
264+
265+
fn arr() -> &'static [u8; 0] {
266+
&[]
267+
}
268+
fn slice_fn(_: impl FnOnce() -> &'static [u8]) {}
269+
slice_fn(|| arr());
270+
}

tests/ui/eta.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ error: redundant closure
2424
LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
2525
| ^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `below`
2626

27-
error: redundant closure
28-
--> $DIR/eta.rs:40:27
29-
|
30-
LL | let e = Some(1u8).map(|a| divergent(a));
31-
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `divergent`
32-
3327
error: redundant closure
3428
--> $DIR/eta.rs:41:27
3529
|
@@ -116,11 +110,5 @@ error: redundant closure
116110
LL | Some(1).map(|n| closure(n));
117111
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut closure`
118112

119-
error: redundant closure
120-
--> $DIR/eta.rs:232:21
121-
|
122-
LL | map_str_to_path(|s| s.as_ref());
123-
| ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::convert::AsRef::as_ref`
124-
125-
error: aborting due to 20 previous errors
113+
error: aborting due to 18 previous errors
126114

0 commit comments

Comments
 (0)