Skip to content

Commit c736a63

Browse files
committed
Auto merge of #8193 - ebobrow:redundant_closure_fp, r=Manishearth
fix [`redundant_closure`] fp with `Rc<F>`/`Arc<F>` fixes #8073 changelog: don't trigger [`redundant_closure`] on `Arc<F>` or `Rc<F>`
2 parents 490566b + 828ddbe commit c736a63

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

clippy_lints/src/eta_reduction.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::higher::VecArgs;
33
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::ty::is_type_diagnostic_item;
45
use clippy_utils::usage::local_used_after_expr;
56
use clippy_utils::{get_enclosing_loop_or_closure, higher, path_to_local, path_to_local_id};
67
use if_chain::if_chain;
@@ -12,6 +13,7 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
1213
use rustc_middle::ty::subst::Subst;
1314
use rustc_middle::ty::{self, ClosureKind, Ty, TypeFoldable};
1415
use rustc_session::{declare_lint_pass, declare_tool_lint};
16+
use rustc_span::symbol::sym;
1517

1618
declare_clippy_lint! {
1719
/// ### What it does
@@ -113,6 +115,9 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
113115
// A type param function ref like `T::f` is not 'static, however
114116
// it is if cast like `T::f as fn()`. This seems like a rustc bug.
115117
if !substs.types().any(|t| matches!(t.kind(), ty::Param(_)));
118+
let callee_ty_unadjusted = cx.typeck_results().expr_ty(callee).peel_refs();
119+
if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Arc);
120+
if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Rc);
116121
then {
117122
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| {
118123
if let Some(mut snippet) = snippet_opt(cx, callee.span) {

tests/ui/eta.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,14 @@ mod type_param_bound {
248248
take(X::fun as fn());
249249
}
250250
}
251+
252+
// #8073 Don't replace closure with `Arc<F>` or `Rc<F>`
253+
fn arc_fp() {
254+
let rc = std::rc::Rc::new(|| 7);
255+
let arc = std::sync::Arc::new(|n| n + 1);
256+
let ref_arc = &std::sync::Arc::new(|_| 5);
257+
258+
true.then(|| rc());
259+
(0..5).map(|n| arc(n));
260+
Some(4).map(|n| ref_arc(n));
261+
}

tests/ui/eta.rs

+11
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,14 @@ mod type_param_bound {
248248
take(X::fun as fn());
249249
}
250250
}
251+
252+
// #8073 Don't replace closure with `Arc<F>` or `Rc<F>`
253+
fn arc_fp() {
254+
let rc = std::rc::Rc::new(|| 7);
255+
let arc = std::sync::Arc::new(|n| n + 1);
256+
let ref_arc = &std::sync::Arc::new(|_| 5);
257+
258+
true.then(|| rc());
259+
(0..5).map(|n| arc(n));
260+
Some(4).map(|n| ref_arc(n));
261+
}

0 commit comments

Comments
 (0)