@@ -314,15 +314,31 @@ where
314
314
}
315
315
}
316
316
} else if ident == "derive_unsafe" {
317
- if input. peek ( Paren ) {
318
- let content;
319
- parenthesized ! ( content in input) ;
320
- let items =
321
- content. parse_terminated ( SpannedDeriveUnsafeTrait :: parse, Token ! [ , ] ) ?;
322
- attrs. derive_unsafe_traits = items. into_iter ( ) . collect ( ) ;
323
- } else {
324
- let msg = "`derive_unsafe(...)` must be used with parenthesis." ;
325
- return Err ( syn:: Error :: new ( ident. span ( ) , msg) ) ;
317
+ cfg_if ! {
318
+ if #[ cfg( feature = "derive_unsafe" ) ] {
319
+ if input. peek( Paren ) {
320
+ let content;
321
+ parenthesized!( content in input) ;
322
+ let items =
323
+ content. parse_terminated( SpannedDeriveUnsafeTrait :: parse, Token ![ , ] ) ?;
324
+ attrs. derive_unsafe_traits = items. into_iter( ) . collect( ) ;
325
+ } else {
326
+ let msg = "`derive_unsafe(...)` must be used with parenthesis." ;
327
+ return Err ( syn:: Error :: new( ident. span( ) , msg) ) ;
328
+ }
329
+ } else {
330
+ // The feature is not enabled, so we return an error
331
+ let msg = concat!(
332
+ "To use derive_unsafe() function, the feature `derive_unsafe` of crate `nutype` needs to be enabled.\n \n " ,
333
+ "DID YOU KNOW?\n " ,
334
+ "It's called `derive_unsafe` because it enables to derive any traits that nutype is not aware of.\n " ,
335
+ "So it is developer's responsibility to ensure that the derived traits do not create a loophole to bypass the constraints.\n " ,
336
+ "As the rule of thumb avoid using `derive_unsafe` with traits that:\n " ,
337
+ "- Create a new instance of the type\n " ,
338
+ "- Mutate the value\n \n " ,
339
+ ) ;
340
+ return Err ( syn:: Error :: new( ident. span( ) , msg) ) ;
341
+ }
326
342
}
327
343
} else {
328
344
let msg = format ! ( "Unknown attribute `{ident}`" ) ;
0 commit comments