Skip to content

Commit

Permalink
fix(derive): Abort on non-unit variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Shir0kamii committed Mar 30, 2022
1 parent 06f855f commit ee3d12e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clap_derive/src/derives/arg_enum.rs
Expand Up @@ -35,7 +35,7 @@ pub fn derive_arg_enum(input: &DeriveInput) -> TokenStream {

pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStream {
if !e.variants.iter().all(|v| matches!(v.fields, Fields::Unit)) {
return quote!();
abort_call_site!("`#[derive(ArgEnum)]` only supports non-unit variants");
};

let attrs = Attrs::from_struct(
Expand Down
1 change: 1 addition & 0 deletions examples/derive_ref/README.md
Expand Up @@ -82,6 +82,7 @@ fn main() {
- `Subcommand` defines available subcommands.
- Subcommand arguments can be defined in a struct-variant or automatically flattened with a tuple-variant.
- `ArgEnum` allows parsing a value directly into an `enum`, erroring on unsupported values.
- The derive doesn't work on enums that contain non-unit variants

See also the [tutorial](../tutorial_derive/README.md) and [examples](../README.md).

Expand Down
10 changes: 10 additions & 0 deletions tests/derive_ui/arg_enum_non_unit.rs
@@ -0,0 +1,10 @@
use clap::ArgEnum;

#[derive(ArgEnum, Clone, Debug)]
enum Opt {
Foo(usize),
}

fn main() {
println!("{:?}", Opt::Foo(42));
}
7 changes: 7 additions & 0 deletions tests/derive_ui/arg_enum_non_unit.stderr
@@ -0,0 +1,7 @@
error: `#[derive(ArgEnum)]` only supports non-unit variants
--> tests/derive_ui/arg_enum_non_unit.rs:3:10
|
3 | #[derive(ArgEnum, Clone, Debug)]
| ^^^^^^^
|
= note: this error originates in the derive macro `ArgEnum` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit ee3d12e

Please sign in to comment.