@@ -35,7 +35,7 @@ pub fn argh_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
3535/// as well as all errors that occurred.
3636fn impl_from_args ( input : & syn:: DeriveInput ) -> TokenStream {
3737 let errors = & Errors :: default ( ) ;
38- if input. generics . params . len ( ) != 0 {
38+ if ! input. generics . params . is_empty ( ) {
3939 errors. err (
4040 & input. generics ,
4141 "`#![derive(FromArgs)]` cannot be applied to types with generic parameters" ,
@@ -65,22 +65,15 @@ enum Optionality {
6565impl PartialEq < Optionality > for Optionality {
6666 fn eq ( & self , other : & Optionality ) -> bool {
6767 use Optionality :: * ;
68- match ( self , other) {
69- ( None , None ) | ( Optional , Optional ) | ( Repeating , Repeating ) => true ,
70- // NB: (Defaulted, Defaulted) can't contain the same token streams
71- _ => false ,
72- }
68+ // NB: (Defaulted, Defaulted) can't contain the same token streams
69+ matches ! ( ( self , other) , ( Optional , Optional ) | ( Repeating , Repeating ) )
7370 }
7471}
7572
7673impl Optionality {
7774 /// Whether or not this is `Optionality::None`
7875 fn is_required ( & self ) -> bool {
79- if let Optionality :: None = self {
80- true
81- } else {
82- false
83- }
76+ matches ! ( self , Optionality :: None )
8477 }
8578}
8679
@@ -252,6 +245,7 @@ fn impl_from_args_struct(
252245 let top_or_sub_cmd_impl = top_or_sub_cmd_impl ( errors, name, type_attrs) ;
253246
254247 let trait_impl = quote_spanned ! { impl_span =>
248+ #[ automatically_derived]
255249 impl argh:: FromArgs for #name {
256250 #from_args_method
257251
@@ -269,8 +263,8 @@ fn impl_from_args_struct_from_args<'a>(
269263 type_attrs : & TypeAttrs ,
270264 fields : & ' a [ StructField < ' a > ] ,
271265) -> TokenStream {
272- let init_fields = declare_local_storage_for_from_args_fields ( & fields) ;
273- let unwrap_fields = unwrap_from_args_fields ( & fields) ;
266+ let init_fields = declare_local_storage_for_from_args_fields ( fields) ;
267+ let unwrap_fields = unwrap_from_args_fields ( fields) ;
274268 let positional_fields: Vec < & StructField < ' _ > > =
275269 fields. iter ( ) . filter ( |field| field. kind == FieldKind :: Positional ) . collect ( ) ;
276270 let positional_field_idents = positional_fields. iter ( ) . map ( |field| & field. field . ident ) ;
@@ -289,13 +283,13 @@ fn impl_from_args_struct_from_args<'a>(
289283 }
290284 } ) ;
291285
292- let flag_str_to_output_table_map = flag_str_to_output_table_map_entries ( & fields) ;
286+ let flag_str_to_output_table_map = flag_str_to_output_table_map_entries ( fields) ;
293287
294288 let mut subcommands_iter =
295289 fields. iter ( ) . filter ( |field| field. kind == FieldKind :: SubCommand ) . fuse ( ) ;
296290
297291 let subcommand: Option < & StructField < ' _ > > = subcommands_iter. next ( ) ;
298- while let Some ( dup_subcommand) = subcommands_iter. next ( ) {
292+ for dup_subcommand in subcommands_iter {
299293 errors. duplicate_attrs ( "subcommand" , subcommand. unwrap ( ) . field , dup_subcommand. field ) ;
300294 }
301295
@@ -304,7 +298,7 @@ fn impl_from_args_struct_from_args<'a>(
304298 let missing_requirements_ident = syn:: Ident :: new ( "__missing_requirements" , impl_span) ;
305299
306300 let append_missing_requirements =
307- append_missing_requirements ( & missing_requirements_ident, & fields) ;
301+ append_missing_requirements ( & missing_requirements_ident, fields) ;
308302
309303 let parse_subcommands = if let Some ( subcommand) = subcommand {
310304 let name = subcommand. name ;
@@ -324,12 +318,14 @@ fn impl_from_args_struct_from_args<'a>(
324318
325319 // Identifier referring to a value containing the name of the current command as an `&[&str]`.
326320 let cmd_name_str_array_ident = syn:: Ident :: new ( "__cmd_name" , impl_span) ;
327- let help = help:: help ( errors, cmd_name_str_array_ident, type_attrs, & fields, subcommand) ;
321+ let help = help:: help ( errors, cmd_name_str_array_ident, type_attrs, fields, subcommand) ;
328322
329323 let method_impl = quote_spanned ! { impl_span =>
330324 fn from_args( __cmd_name: & [ & str ] , __args: & [ & str ] )
331325 -> std:: result:: Result <Self , argh:: EarlyExit >
332326 {
327+ #![ allow( clippy:: unwrap_in_result) ]
328+
333329 #( #init_fields ) *
334330
335331 argh:: parse_struct_args(
@@ -374,8 +370,8 @@ fn impl_from_args_struct_redact_arg_values<'a>(
374370 type_attrs : & TypeAttrs ,
375371 fields : & ' a [ StructField < ' a > ] ,
376372) -> TokenStream {
377- let init_fields = declare_local_storage_for_redacted_fields ( & fields) ;
378- let unwrap_fields = unwrap_redacted_fields ( & fields) ;
373+ let init_fields = declare_local_storage_for_redacted_fields ( fields) ;
374+ let unwrap_fields = unwrap_redacted_fields ( fields) ;
379375
380376 let positional_fields: Vec < & StructField < ' _ > > =
381377 fields. iter ( ) . filter ( |field| field. kind == FieldKind :: Positional ) . collect ( ) ;
@@ -395,13 +391,13 @@ fn impl_from_args_struct_redact_arg_values<'a>(
395391 }
396392 } ) ;
397393
398- let flag_str_to_output_table_map = flag_str_to_output_table_map_entries ( & fields) ;
394+ let flag_str_to_output_table_map = flag_str_to_output_table_map_entries ( fields) ;
399395
400396 let mut subcommands_iter =
401397 fields. iter ( ) . filter ( |field| field. kind == FieldKind :: SubCommand ) . fuse ( ) ;
402398
403399 let subcommand: Option < & StructField < ' _ > > = subcommands_iter. next ( ) ;
404- while let Some ( dup_subcommand) = subcommands_iter. next ( ) {
400+ for dup_subcommand in subcommands_iter {
405401 errors. duplicate_attrs ( "subcommand" , subcommand. unwrap ( ) . field , dup_subcommand. field ) ;
406402 }
407403
@@ -410,7 +406,7 @@ fn impl_from_args_struct_redact_arg_values<'a>(
410406 let missing_requirements_ident = syn:: Ident :: new ( "__missing_requirements" , impl_span) ;
411407
412408 let append_missing_requirements =
413- append_missing_requirements ( & missing_requirements_ident, & fields) ;
409+ append_missing_requirements ( & missing_requirements_ident, fields) ;
414410
415411 let redact_subcommands = if let Some ( subcommand) = subcommand {
416412 let name = subcommand. name ;
@@ -428,15 +424,15 @@ fn impl_from_args_struct_redact_arg_values<'a>(
428424 quote_spanned ! { impl_span => None }
429425 } ;
430426
431- let cmd_name = if type_attrs. is_subcommand . is_none ( ) {
432- quote ! { __cmd_name . last ( ) . expect ( "no command name" ) . to_string ( ) }
427+ let unwrap_cmd_name_err_string = if type_attrs. is_subcommand . is_none ( ) {
428+ quote ! { "no command name" }
433429 } else {
434- quote ! { __cmd_name . last ( ) . expect ( "no subcommand name" ) . to_string ( ) }
430+ quote ! { "no subcommand name" }
435431 } ;
436432
437433 // Identifier referring to a value containing the name of the current command as an `&[&str]`.
438434 let cmd_name_str_array_ident = syn:: Ident :: new ( "__cmd_name" , impl_span) ;
439- let help = help:: help ( errors, cmd_name_str_array_ident, type_attrs, & fields, subcommand) ;
435+ let help = help:: help ( errors, cmd_name_str_array_ident, type_attrs, fields, subcommand) ;
440436
441437 let method_impl = quote_spanned ! { impl_span =>
442438 fn redact_arg_values( __cmd_name: & [ & str ] , __args: & [ & str ] ) -> std:: result:: Result <Vec <String >, argh:: EarlyExit > {
@@ -471,7 +467,11 @@ fn impl_from_args_struct_redact_arg_values<'a>(
471467 #missing_requirements_ident. err_on_any( ) ?;
472468
473469 let mut __redacted = vec![
474- #cmd_name,
470+ if let Some ( cmd_name) = __cmd_name. last( ) {
471+ ( * cmd_name) . to_owned( )
472+ } else {
473+ return Err ( argh:: EarlyExit :: from( #unwrap_cmd_name_err_string. to_owned( ) ) ) ;
474+ }
475475 ] ;
476476
477477 #( #unwrap_fields ) *
@@ -510,6 +510,7 @@ fn top_or_sub_cmd_impl(errors: &Errors, name: &syn::Ident, type_attrs: &TypeAttr
510510 if type_attrs. is_subcommand . is_none ( ) {
511511 // Not a subcommand
512512 quote ! {
513+ #[ automatically_derived]
513514 impl argh:: TopLevelCommand for #name { }
514515 }
515516 } else {
@@ -519,6 +520,7 @@ fn top_or_sub_cmd_impl(errors: &Errors, name: &syn::Ident, type_attrs: &TypeAttr
519520 & empty_str
520521 } ) ;
521522 quote ! {
523+ #[ automatically_derived]
522524 impl argh:: SubCommand for #name {
523525 const COMMAND : & ' static argh:: CommandInfo = & argh:: CommandInfo {
524526 name: #subcommand_name,
@@ -586,7 +588,9 @@ fn unwrap_from_args_fields<'a>(
586588 let field_name = field. name ;
587589 match field. kind {
588590 FieldKind :: Option | FieldKind :: Positional => match & field. optionality {
589- Optionality :: None => quote ! { #field_name: #field_name. slot. unwrap( ) } ,
591+ Optionality :: None => quote ! {
592+ #field_name: #field_name. slot. unwrap( )
593+ } ,
590594 Optionality :: Optional | Optionality :: Repeating => {
591595 quote ! { #field_name: #field_name. slot }
592596 }
@@ -639,7 +643,7 @@ fn declare_local_storage_for_redacted_fields<'a>(
639643 let mut #field_name: argh:: ParseValueSlotTy :: <#field_slot_type, String > =
640644 argh:: ParseValueSlotTy {
641645 slot: std:: default :: Default :: default ( ) ,
642- parse_func: |arg, _| { Ok ( arg. to_string ( ) ) } ,
646+ parse_func: |arg, _| { Ok ( arg. to_owned ( ) ) } ,
643647 } ;
644648 }
645649 }
@@ -658,7 +662,7 @@ fn declare_local_storage_for_redacted_fields<'a>(
658662 let mut #field_name: argh:: ParseValueSlotTy :: <#field_slot_type, String > =
659663 argh:: ParseValueSlotTy {
660664 slot: std:: default :: Default :: default ( ) ,
661- parse_func: |_, _| { Ok ( #arg_name. to_string ( ) ) } ,
665+ parse_func: |_, _| { Ok ( #arg_name. to_owned ( ) ) } ,
662666 } ;
663667 }
664668 }
@@ -861,25 +865,37 @@ fn impl_from_args_enum(
861865 fn from_args( command_name: & [ & str ] , args: & [ & str ] )
862866 -> std:: result:: Result <Self , argh:: EarlyExit >
863867 {
864- let subcommand_name = * command_name. last( ) . expect( "no subcommand name" ) ;
868+ let subcommand_name = if let Some ( subcommand_name) = command_name. last( ) {
869+ * subcommand_name
870+ } else {
871+ return Err ( argh:: EarlyExit :: from( "no subcommand name" . to_owned( ) ) ) ;
872+ } ;
873+
865874 #(
866875 if subcommand_name == <#variant_ty as argh:: SubCommand >:: COMMAND . name {
867876 return Ok ( #name_repeating:: #variant_names(
868877 <#variant_ty as argh:: FromArgs >:: from_args( command_name, args) ?
869878 ) ) ;
870879 }
871880 ) *
872- unreachable!( "no subcommand matched" )
881+
882+ Err ( argh:: EarlyExit :: from( "no subcommand matched" . to_owned( ) ) )
873883 }
874884
875885 fn redact_arg_values( command_name: & [ & str ] , args: & [ & str ] ) -> std:: result:: Result <Vec <String >, argh:: EarlyExit > {
876- let subcommand_name = * command_name. last( ) . expect( "no subcommand name" ) ;
886+ let subcommand_name = if let Some ( subcommand_name) = command_name. last( ) {
887+ * subcommand_name
888+ } else {
889+ return Err ( argh:: EarlyExit :: from( "no subcommand name" . to_owned( ) ) ) ;
890+ } ;
891+
877892 #(
878893 if subcommand_name == <#variant_ty as argh:: SubCommand >:: COMMAND . name {
879894 return <#variant_ty as argh:: FromArgs >:: redact_arg_values( command_name, args) ;
880895 }
881896 ) *
882- unreachable!( "no subcommand matched" )
897+
898+ Err ( argh:: EarlyExit :: from( "no subcommand matched" . to_owned( ) ) )
883899 }
884900 }
885901
0 commit comments