@@ -8,6 +8,7 @@ use rustc_hir::intravisit::FnKind;
88use rustc_hir:: { self as hir, Body , Constness , FnDecl , GenericParamKind } ;
99use rustc_lint:: { LateContext , LateLintPass } ;
1010use rustc_middle:: lint:: in_external_macro;
11+ use rustc_middle:: ty;
1112use rustc_session:: impl_lint_pass;
1213use rustc_span:: def_id:: LocalDefId ;
1314use rustc_span:: Span ;
@@ -131,6 +132,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
131132 FnKind :: Closure => return ,
132133 }
133134
135+ if fn_inputs_has_impl_trait_ty ( cx, def_id) {
136+ return ;
137+ }
138+
134139 let hir_id = cx. tcx . local_def_id_to_hir_id ( def_id) ;
135140
136141 // Const fns are not allowed as methods in a trait.
@@ -185,3 +190,15 @@ fn could_be_const_with_abi(cx: &LateContext<'_>, msrv: &Msrv, abi: Abi) -> bool
185190 _ => cx. tcx . features ( ) . const_extern_fn ,
186191 }
187192}
193+
194+ /// Return `true` when the given `def_id` is a function that has `impl Trait` ty as one of
195+ /// its parameter types.
196+ fn fn_inputs_has_impl_trait_ty ( cx : & LateContext < ' _ > , def_id : LocalDefId ) -> bool {
197+ let inputs = cx. tcx . fn_sig ( def_id) . instantiate_identity ( ) . inputs ( ) . skip_binder ( ) ;
198+ inputs. iter ( ) . any ( |input| {
199+ matches ! (
200+ input. kind( ) ,
201+ ty:: Alias ( ty:: AliasTyKind :: Weak , alias_ty) if cx. tcx. type_of( alias_ty. def_id) . skip_binder( ) . is_impl_trait( )
202+ )
203+ } )
204+ }
0 commit comments