@@ -123,7 +123,8 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
123123 ExprKind :: Path ( QPath :: Resolved ( ..) | QPath :: TypeRelative ( ..) )
124124 ) =>
125125 {
126- let callee_ty = typeck. expr_ty ( callee) . peel_refs ( ) ;
126+ let callee_ty_raw = typeck. expr_ty ( callee) ;
127+ let callee_ty = callee_ty_raw. peel_refs ( ) ;
127128 if matches ! ( type_diagnostic_name( cx, callee_ty) , Some ( sym:: Arc | sym:: Rc ) )
128129 || !check_inputs ( typeck, body. params , None , args)
129130 {
@@ -170,15 +171,25 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
170171 {
171172 span_lint_and_then ( cx, REDUNDANT_CLOSURE , expr. span , "redundant closure" , |diag| {
172173 if let Some ( mut snippet) = snippet_opt ( cx, callee. span ) {
173- if let Ok ( ( ClosureKind :: FnMut , _ ) ) = cx . tcx . infer_ctxt ( ) . build ( ) . type_implements_fn_trait (
174- cx . param_env ,
175- Binder :: bind_with_vars ( callee_ty_adjusted , List :: empty ( ) ) ,
176- ty :: PredicatePolarity :: Positive ,
177- ) && path_to_local ( callee ) . map_or ( false , |l| {
174+ if path_to_local ( callee ) . map_or ( false , |l| {
175+ // FIXME: Do we really need this `local_used_in` check?
176+ // Isn't it checking something like... `callee(callee)`?
177+ // If somehow this check is needed, add some test for it ,
178+ // 'cuz currently nothing changes after deleting this check.
178179 local_used_in ( cx, l, args) || local_used_after_expr ( cx, l, expr)
179180 } ) {
180- // Mutable closure is used after current expr; we cannot consume it.
181- snippet = format ! ( "&mut {snippet}" ) ;
181+ match cx. tcx . infer_ctxt ( ) . build ( ) . type_implements_fn_trait (
182+ cx. param_env ,
183+ Binder :: bind_with_vars ( callee_ty_adjusted, List :: empty ( ) ) ,
184+ ty:: PredicatePolarity :: Positive ,
185+ ) {
186+ // Mutable closure is used after current expr; we cannot consume it.
187+ Ok ( ( ClosureKind :: FnMut , _) ) => snippet = format ! ( "&mut {snippet}" ) ,
188+ Ok ( ( ClosureKind :: Fn , _) ) if !callee_ty_raw. is_ref ( ) => {
189+ snippet = format ! ( "&{snippet}" ) ;
190+ } ,
191+ _ => ( ) ,
192+ }
182193 }
183194 diag. span_suggestion (
184195 expr. span ,
0 commit comments