@@ -85,6 +85,17 @@ fn contains_trait_object(ty: Ty<'_>) -> bool {
8585 }
8686}
8787
88+ fn determine_derive_macro ( cx : & LateContext < ' _ > , is_const : bool ) -> Option < & ' static str > {
89+ if is_const {
90+ if !cx. tcx . features ( ) . enabled ( sym:: derive_const) {
91+ return None ;
92+ }
93+ return Some ( "derive_const" ) ;
94+ }
95+ Some ( "derive" )
96+ }
97+
98+ #[ expect( clippy:: too_many_arguments) ]
8899fn check_struct < ' tcx > (
89100 cx : & LateContext < ' tcx > ,
90101 item : & ' tcx Item < ' _ > ,
@@ -93,6 +104,7 @@ fn check_struct<'tcx>(
93104 adt_def : AdtDef < ' _ > ,
94105 ty_args : GenericArgsRef < ' _ > ,
95106 typeck_results : & ' tcx TypeckResults < ' tcx > ,
107+ is_const : bool ,
96108) {
97109 if let TyKind :: Path ( QPath :: Resolved ( _, p) ) = self_ty. kind
98110 && let Some ( PathSegment { args, .. } ) = p. segments . last ( )
@@ -128,11 +140,15 @@ fn check_struct<'tcx>(
128140 _ => false ,
129141 } ;
130142
143+ let Some ( derive_snippet) = determine_derive_macro ( cx, is_const) else {
144+ return ;
145+ } ;
146+
131147 if should_emit {
132148 let struct_span = cx. tcx . def_span ( adt_def. did ( ) ) ;
133149 let suggestions = vec ! [
134150 ( item. span, String :: new( ) ) , // Remove the manual implementation
135- ( struct_span. shrink_to_lo( ) , "#[derive (Default)]\n " . to_string ( ) ) , // Add the derive attribute
151+ ( struct_span. shrink_to_lo( ) , format! ( "#[{derive_snippet} (Default)]\n " ) ) , // Add the derive attribute
136152 ] ;
137153
138154 span_lint_and_then ( cx, DERIVABLE_IMPLS , item. span , "this `impl` can be derived" , |diag| {
@@ -145,7 +161,13 @@ fn check_struct<'tcx>(
145161 }
146162}
147163
148- fn check_enum < ' tcx > ( cx : & LateContext < ' tcx > , item : & ' tcx Item < ' _ > , func_expr : & Expr < ' _ > , adt_def : AdtDef < ' _ > ) {
164+ fn check_enum < ' tcx > (
165+ cx : & LateContext < ' tcx > ,
166+ item : & ' tcx Item < ' _ > ,
167+ func_expr : & Expr < ' _ > ,
168+ adt_def : AdtDef < ' _ > ,
169+ is_const : bool ,
170+ ) {
149171 if let ExprKind :: Path ( QPath :: Resolved ( None , p) ) = & peel_blocks ( func_expr) . kind
150172 && let Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Const ) , id) = p. res
151173 && let variant_id = cx. tcx . parent ( id)
@@ -158,11 +180,15 @@ fn check_enum<'tcx>(cx: &LateContext<'tcx>, item: &'tcx Item<'_>, func_expr: &Ex
158180 let variant_span = cx. tcx . def_span ( variant_def. def_id ) ;
159181 let indent_variant = indent_of ( cx, variant_span) . unwrap_or ( 0 ) ;
160182
183+ let Some ( derive_snippet) = determine_derive_macro ( cx, is_const) else {
184+ return ;
185+ } ;
186+
161187 let suggestions = vec ! [
162188 ( item. span, String :: new( ) ) , // Remove the manual implementation
163189 (
164190 enum_span. shrink_to_lo( ) ,
165- format!( "#[derive (Default)]\n {}" , " " . repeat( indent_enum) ) ,
191+ format!( "#[{derive_snippet} (Default)]\n {}" , " " . repeat( indent_enum) ) ,
166192 ) , // Add the derive attribute
167193 (
168194 variant_span. shrink_to_lo( ) ,
@@ -201,10 +227,20 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
201227 && !attrs. iter ( ) . any ( |attr| attr. doc_str ( ) . is_some ( ) )
202228 && cx. tcx . hir_attrs ( impl_item_hir) . is_empty ( )
203229 {
230+ let is_const = of_trait. constness == hir:: Constness :: Const ;
204231 if adt_def. is_struct ( ) {
205- check_struct ( cx, item, self_ty, func_expr, adt_def, args, cx. tcx . typeck_body ( * b) ) ;
232+ check_struct (
233+ cx,
234+ item,
235+ self_ty,
236+ func_expr,
237+ adt_def,
238+ args,
239+ cx. tcx . typeck_body ( * b) ,
240+ is_const,
241+ ) ;
206242 } else if adt_def. is_enum ( ) && self . msrv . meets ( cx, msrvs:: DEFAULT_ENUM_ATTRIBUTE ) {
207- check_enum ( cx, item, func_expr, adt_def) ;
243+ check_enum ( cx, item, func_expr, adt_def, is_const ) ;
208244 }
209245 }
210246 }
0 commit comments