Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow #[flatten] fields to have doc comments but ignore them (comments) #414

Merged
merged 2 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't the goal to ignore the doc comment? Here, the compilation fail.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal is to ignore the comments, but disallow methods with flatten, like short. This is exactly what triggers it here.

/// Comments allowed
#[structopt(flatten)]

// Methods disallowed
#[structopt(flatten, short)]

--> $DIR/flatten_and_methods.rs:22:24
|
22 | #[structopt(short, flatten)]
Expand Down