Skip to content

Commit

Permalink
Allow #[flatten] fields to have doc comments but ignore them (comment…
Browse files Browse the repository at this point in the history
…s) (#414)

Allow #[flatten] fields to have doc comments but ignore comments
  • Loading branch information
CreepySkeleton authored Jul 25, 2020
1 parent e47d78c commit 2d8ba48
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
8 changes: 6 additions & 2 deletions structopt-derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,16 @@ impl Attrs {
"parse attribute is not allowed for flattened entry"
);
}
if res.has_explicit_methods() || res.has_doc_methods() {
if res.has_explicit_methods() {
abort!(
res.kind.span(),
"methods and doc comments are not allowed for flattened entry"
"methods are not allowed for flattened entry"
);
}

if res.has_doc_methods() {
res.doc_comment = vec![];
}
}

Kind::ExternalSubcommand => {}
Expand Down
29 changes: 29 additions & 0 deletions tests/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use structopt::StructOpt;

mod utils;

#[test]
fn flatten() {
#[derive(StructOpt, PartialEq, Debug)]
Expand Down Expand Up @@ -151,3 +153,30 @@ fn subcommand_in_flatten() {

Struct1::from_iter(&["test", "command", "foo"]);
}

#[test]
fn flatten_doc_comment() {
#[derive(StructOpt, PartialEq, Debug)]
struct Common {
/// This is an arg. Arg means "argument". Command line argument.
arg: i32,
}

#[derive(StructOpt, PartialEq, Debug)]
struct Opt {
/// The very important comment that clippy had me put here.
/// It knows better.
#[structopt(flatten)]
common: Common,
}
assert_eq!(
Opt {
common: Common { arg: 42 }
},
Opt::from_iter(&["test", "42"])
);

let help = utils::get_help::<Opt>();
assert!(help.contains("This is an arg."));
assert!(!help.contains("The very important"));
}
30 changes: 0 additions & 30 deletions tests/ui/flatten_and_doc.rs

This file was deleted.

5 changes: 0 additions & 5 deletions tests/ui/flatten_and_doc.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/flatten_and_methods.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: methods and doc comments are not allowed for flattened entry
error: methods are not allowed for flattened entry
--> $DIR/flatten_and_methods.rs:22:24
|
22 | #[structopt(short, flatten)]
Expand Down

0 comments on commit 2d8ba48

Please sign in to comment.