From 78676f5043aba7f1e1b8d8683a681b7b60e4da98 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 5 Oct 2022 16:25:11 -0500 Subject: [PATCH] fix(derive): Make mismatched behavior more obvious If the inner type never implemented `group_id()`, then it won't work and people will be confused. This at least gives people an idea of whats going wrong. This is most likely to be a problem until #3165 is fully implemented but hand-implementations can still run into this. Probably should have made the groups another trait to catch this in type system but too late. --- clap_derive/src/derives/args.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index 15256fcc14b..f49883ce3bb 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -447,7 +447,9 @@ pub fn gen_constructor(fields: &[(&Field, Item)]) -> TokenStream { Ty::Option => { quote_spanned! { kind.span()=> #field_name: { - if <#inner_type as clap::Args>::group_id().map(|group_id| #arg_matches.contains_id(group_id.as_str())).unwrap_or(false) { + let group_id = <#inner_type as clap::Args>::group_id() + .expect("`#[arg(flatten)]`ed field type implements `Args::group_id`"); + if #arg_matches.contains_id(group_id.as_str()) { Some( <#inner_type as clap::FromArgMatches>::from_arg_matches_mut(#arg_matches)? )