From e7729d12826b26de7bdf1980d1d85f41dd376763 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 5 Jun 2023 12:31:55 -0500 Subject: [PATCH 1/3] fix(derive): Mark all impls as automatically derived Unsure what all it does. I removed our `allow`s and we still get lints, so unsure if its only some that it applies to it. Inspired by #4951 --- clap_derive/src/derives/args.rs | 2 ++ clap_derive/src/derives/into_app.rs | 2 ++ clap_derive/src/derives/parser.rs | 2 ++ clap_derive/src/derives/subcommand.rs | 2 ++ clap_derive/src/derives/value_enum.rs | 1 + clap_derive/src/dummies.rs | 6 ++++++ 6 files changed, 15 insertions(+) diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index 83598f56..5d587950 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -99,6 +99,7 @@ pub fn gen_for_struct( clippy::suspicious_else_formatting, clippy::almost_swapped, )] + #[automatically_derived] impl #impl_generics clap::FromArgMatches for #item_name #ty_generics #where_clause { fn from_arg_matches(__clap_arg_matches: &clap::ArgMatches) -> ::std::result::Result { Self::from_arg_matches_mut(&mut __clap_arg_matches.clone()) @@ -134,6 +135,7 @@ pub fn gen_for_struct( clippy::suspicious_else_formatting, clippy::almost_swapped, )] + #[automatically_derived] impl #impl_generics clap::Args for #item_name #ty_generics #where_clause { fn group_id() -> Option { #group_id diff --git a/clap_derive/src/derives/into_app.rs b/clap_derive/src/derives/into_app.rs index 72f081fd..71eb59f5 100644 --- a/clap_derive/src/derives/into_app.rs +++ b/clap_derive/src/derives/into_app.rs @@ -42,6 +42,7 @@ pub fn gen_for_struct( clippy::suspicious_else_formatting, clippy::almost_swapped, )] + #[automatically_derived] impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause { fn command<'b>() -> clap::Command { let #app_var = clap::Command::new(#name); @@ -82,6 +83,7 @@ pub fn gen_for_enum( clippy::suspicious_else_formatting, clippy::almost_swapped, )] + #[automatically_derived] impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause { fn command<'b>() -> clap::Command { let #app_var = clap::Command::new(#name) diff --git a/clap_derive/src/derives/parser.rs b/clap_derive/src/derives/parser.rs index 926c50ce..605ae190 100644 --- a/clap_derive/src/derives/parser.rs +++ b/clap_derive/src/derives/parser.rs @@ -86,6 +86,7 @@ fn gen_for_struct( let args = args::gen_for_struct(item, item_name, generics, fields)?; Ok(quote! { + #[automatically_derived] impl #impl_generics clap::Parser for #item_name #ty_generics #where_clause {} #into_app @@ -105,6 +106,7 @@ fn gen_for_enum( let subcommand = subcommand::gen_for_enum(item, item_name, generics, variants)?; Ok(quote! { + #[automatically_derived] impl #impl_generics clap::Parser for #item_name #ty_generics #where_clause {} #into_app diff --git a/clap_derive/src/derives/subcommand.rs b/clap_derive/src/derives/subcommand.rs index 5c235096..51450d24 100644 --- a/clap_derive/src/derives/subcommand.rs +++ b/clap_derive/src/derives/subcommand.rs @@ -79,6 +79,7 @@ pub fn gen_for_enum( clippy::suspicious_else_formatting, clippy::almost_swapped, )] + #[automatically_derived] impl #impl_generics clap::FromArgMatches for #item_name #ty_generics #where_clause { fn from_arg_matches(__clap_arg_matches: &clap::ArgMatches) -> ::std::result::Result { Self::from_arg_matches_mut(&mut __clap_arg_matches.clone()) @@ -105,6 +106,7 @@ pub fn gen_for_enum( clippy::suspicious_else_formatting, clippy::almost_swapped, )] + #[automatically_derived] impl #impl_generics clap::Subcommand for #item_name #ty_generics #where_clause { fn augment_subcommands <'b>(__clap_app: clap::Command) -> clap::Command { #augmentation diff --git a/clap_derive/src/derives/value_enum.rs b/clap_derive/src/derives/value_enum.rs index 6f107c01..b7d8c2f8 100644 --- a/clap_derive/src/derives/value_enum.rs +++ b/clap_derive/src/derives/value_enum.rs @@ -64,6 +64,7 @@ pub fn gen_for_enum( clippy::suspicious_else_formatting, clippy::almost_swapped, )] + #[automatically_derived] impl clap::ValueEnum for #item_name { #value_variants #to_possible_value diff --git a/clap_derive/src/dummies.rs b/clap_derive/src/dummies.rs index b10bedc6..3a1581b2 100644 --- a/clap_derive/src/dummies.rs +++ b/clap_derive/src/dummies.rs @@ -7,6 +7,7 @@ use quote::quote; pub fn parser(name: &Ident) -> proc_macro2::TokenStream { let into_app = into_app(name); quote!( + #[automatically_derived] impl clap::Parser for #name {} #into_app ) @@ -15,6 +16,7 @@ pub fn parser(name: &Ident) -> proc_macro2::TokenStream { #[must_use] pub fn into_app(name: &Ident) -> proc_macro2::TokenStream { quote! { + #[automatically_derived] impl clap::CommandFactory for #name { fn command<'b>() -> clap::Command { unimplemented!() @@ -29,6 +31,7 @@ pub fn into_app(name: &Ident) -> proc_macro2::TokenStream { #[must_use] pub fn from_arg_matches(name: &Ident) -> proc_macro2::TokenStream { quote! { + #[automatically_derived] impl clap::FromArgMatches for #name { fn from_arg_matches(_m: &clap::ArgMatches) -> ::std::result::Result { unimplemented!() @@ -44,6 +47,7 @@ pub fn from_arg_matches(name: &Ident) -> proc_macro2::TokenStream { pub fn subcommand(name: &Ident) -> proc_macro2::TokenStream { let from_arg_matches = from_arg_matches(name); quote! { + #[automatically_derived] impl clap::Subcommand for #name { fn augment_subcommands(_cmd: clap::Command) -> clap::Command { unimplemented!() @@ -63,6 +67,7 @@ pub fn subcommand(name: &Ident) -> proc_macro2::TokenStream { pub fn args(name: &Ident) -> proc_macro2::TokenStream { let from_arg_matches = from_arg_matches(name); quote! { + #[automatically_derived] impl clap::Args for #name { fn augment_args(_cmd: clap::Command) -> clap::Command { unimplemented!() @@ -78,6 +83,7 @@ pub fn args(name: &Ident) -> proc_macro2::TokenStream { #[must_use] pub fn value_enum(name: &Ident) -> proc_macro2::TokenStream { quote! { + #[automatically_derived] impl clap::ValueEnum for #name { fn value_variants<'a>() -> &'a [Self]{ unimplemented!() From 5661b6b50843d2386a63c1243ee2c19050b00a40 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 5 Jun 2023 12:49:26 -0500 Subject: [PATCH 2/3] style: Remove unused mut --- clap_builder/src/builder/command.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index 603942af..62930766 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -4030,7 +4030,7 @@ impl Command { } .to_owned(); - for mut sc in &mut self.subcommands { + for sc in &mut self.subcommands { debug!("Command::_build_bin_names:iter: bin_name set..."); if sc.usage_name.is_none() { From 103ae5cf626819c7bfcfb933f549664227ac0a64 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 5 Jun 2023 12:55:06 -0500 Subject: [PATCH 3/3] fix(derive): Don't warn when people bring types into scope Fixes #4951 --- clap_derive/src/derives/args.rs | 16 ++++++++++++++-- clap_derive/src/derives/into_app.rs | 16 ++++++++++++++-- clap_derive/src/derives/parser.rs | 3 +++ clap_derive/src/derives/subcommand.rs | 16 ++++++++++++++-- clap_derive/src/derives/value_enum.rs | 8 +++++++- tests/derive/deny_warnings.rs | 14 ++++++++++++++ 6 files changed, 66 insertions(+), 7 deletions(-) diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index 5d587950..20164ff2 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -86,7 +86,13 @@ pub fn gen_for_struct( }; Ok(quote! { - #[allow(dead_code, unreachable_code, unused_variables, unused_braces)] + #[allow( + dead_code, + unreachable_code, + unused_variables, + unused_braces, + unused_qualifications, + )] #[allow( clippy::style, clippy::complexity, @@ -122,7 +128,13 @@ pub fn gen_for_struct( } } - #[allow(dead_code, unreachable_code, unused_variables, unused_braces)] + #[allow( + dead_code, + unreachable_code, + unused_variables, + unused_braces, + unused_qualifications, + )] #[allow( clippy::style, clippy::complexity, diff --git a/clap_derive/src/derives/into_app.rs b/clap_derive/src/derives/into_app.rs index 71eb59f5..0bd63624 100644 --- a/clap_derive/src/derives/into_app.rs +++ b/clap_derive/src/derives/into_app.rs @@ -29,7 +29,13 @@ pub fn gen_for_struct( let app_var = Ident::new("__clap_app", Span::call_site()); let tokens = quote! { - #[allow(dead_code, unreachable_code, unused_variables, unused_braces)] + #[allow( + dead_code, + unreachable_code, + unused_variables, + unused_braces, + unused_qualifications, + )] #[allow( clippy::style, clippy::complexity, @@ -70,7 +76,13 @@ pub fn gen_for_enum( let app_var = Ident::new("__clap_app", Span::call_site()); Ok(quote! { - #[allow(dead_code, unreachable_code, unused_variables, unused_braces)] + #[allow( + dead_code, + unreachable_code, + unused_variables, + unused_braces, + unused_qualifications, + )] #[allow( clippy::style, clippy::complexity, diff --git a/clap_derive/src/derives/parser.rs b/clap_derive/src/derives/parser.rs index 605ae190..272948b6 100644 --- a/clap_derive/src/derives/parser.rs +++ b/clap_derive/src/derives/parser.rs @@ -87,6 +87,9 @@ fn gen_for_struct( Ok(quote! { #[automatically_derived] + #[allow( + unused_qualifications, + )] impl #impl_generics clap::Parser for #item_name #ty_generics #where_clause {} #into_app diff --git a/clap_derive/src/derives/subcommand.rs b/clap_derive/src/derives/subcommand.rs index 51450d24..3ae74d01 100644 --- a/clap_derive/src/derives/subcommand.rs +++ b/clap_derive/src/derives/subcommand.rs @@ -66,7 +66,13 @@ pub fn gen_for_enum( let has_subcommand = gen_has_subcommand(variants)?; Ok(quote! { - #[allow(dead_code, unreachable_code, unused_variables, unused_braces)] + #[allow( + dead_code, + unreachable_code, + unused_variables, + unused_braces, + unused_qualifications, + )] #[allow( clippy::style, clippy::complexity, @@ -93,7 +99,13 @@ pub fn gen_for_enum( #update_from_arg_matches } - #[allow(dead_code, unreachable_code, unused_variables, unused_braces)] + #[allow( + dead_code, + unreachable_code, + unused_variables, + unused_braces, + unused_qualifications, + )] #[allow( clippy::style, clippy::complexity, diff --git a/clap_derive/src/derives/value_enum.rs b/clap_derive/src/derives/value_enum.rs index b7d8c2f8..397eb332 100644 --- a/clap_derive/src/derives/value_enum.rs +++ b/clap_derive/src/derives/value_enum.rs @@ -51,7 +51,13 @@ pub fn gen_for_enum( let to_possible_value = gen_to_possible_value(item, &lits); Ok(quote! { - #[allow(dead_code, unreachable_code, unused_variables, unused_braces)] + #[allow( + dead_code, + unreachable_code, + unused_variables, + unused_braces, + unused_qualifications, + )] #[allow( clippy::style, clippy::complexity, diff --git a/tests/derive/deny_warnings.rs b/tests/derive/deny_warnings.rs index 1b3fea26..086733c1 100644 --- a/tests/derive/deny_warnings.rs +++ b/tests/derive/deny_warnings.rs @@ -12,6 +12,7 @@ // commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the // MIT/Apache 2.0 license. +#![deny(unused_qualifications)] #![deny(warnings)] use clap::Parser; @@ -51,3 +52,16 @@ fn warning_never_enum() { Opt::try_parse_from(["test", "foo", "foo"]).unwrap() ); } + +#[test] +fn warning_unused_qualifications() { + // This causes `clap::Args` within the derive to be unused qualifications + use clap::Args; + + #[derive(Args, Clone, Copy, Debug, Default)] + #[group(skip)] + pub struct Compose { + #[command(flatten)] + pub left: L, + } +}