From a951958748d2a3e366a38a255e317f21430550b5 Mon Sep 17 00:00:00 2001 From: CreepySkeleton Date: Tue, 25 Aug 2020 10:53:54 +0300 Subject: [PATCH] Allow flatten with doc comments --- clap_derive/src/attrs.rs | 15 ++++------ clap_derive/tests/flatten.rs | 19 ++++++++++++ clap_derive/tests/ui/flatten_and_doc.rs | 30 ------------------- clap_derive/tests/ui/flatten_and_doc.stderr | 5 ---- .../tests/ui/flatten_and_methods.stderr | 2 +- 5 files changed, 25 insertions(+), 46 deletions(-) delete mode 100644 clap_derive/tests/ui/flatten_and_doc.rs delete mode 100644 clap_derive/tests/ui/flatten_and_doc.stderr diff --git a/clap_derive/src/attrs.rs b/clap_derive/src/attrs.rs index 2561294f38d..b40da9a111c 100644 --- a/clap_derive/src/attrs.rs +++ b/clap_derive/src/attrs.rs @@ -481,12 +481,15 @@ 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" ); } + + // ignore doc comments + res.doc_comment = vec![]; } Kind::ExternalSubcommand => { @@ -695,14 +698,6 @@ impl Attrs { .iter() .any(|m| m.name != "about" && m.name != "long_about") } - - pub fn has_doc_methods(&self) -> bool { - !self.doc_comment.is_empty() - || self - .methods - .iter() - .any(|m| m.name == "about" || m.name == "long_about") - } } /// replace all `:` with `, ` when not inside the `<>` diff --git a/clap_derive/tests/flatten.rs b/clap_derive/tests/flatten.rs index 77f99addee9..702f1ae9757 100644 --- a/clap_derive/tests/flatten.rs +++ b/clap_derive/tests/flatten.rs @@ -132,3 +132,22 @@ fn merge_subcommands_with_flatten() { Opt::parse_from(&["test", "command2", "43"]) ); } + +#[test] +fn flatten_with_doc_comment() { + #[derive(Clap, Debug)] + struct DaemonOpts { + #[clap(short)] + user: String, + #[clap(short)] + group: String, + } + + #[derive(Clap, Debug)] + #[clap(name = "basic")] + struct Opt { + /// A very important doc comment I just can't leave out! + #[clap(flatten)] + opts: DaemonOpts, + } +} diff --git a/clap_derive/tests/ui/flatten_and_doc.rs b/clap_derive/tests/ui/flatten_and_doc.rs deleted file mode 100644 index dea35c23349..00000000000 --- a/clap_derive/tests/ui/flatten_and_doc.rs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018 Guillaume Pinot (@TeXitoi) -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use clap::Clap; - -#[derive(Clap, Debug)] -struct DaemonOpts { - #[clap(short)] - user: String, - #[clap(short)] - group: String, -} - -#[derive(Clap, Debug)] -#[clap(name = "basic")] -struct Opt { - /// Opts. - #[clap(flatten)] - opts: DaemonOpts, -} - -fn main() { - let opt = Opt::parse(); - println!("{:?}", opt); -} diff --git a/clap_derive/tests/ui/flatten_and_doc.stderr b/clap_derive/tests/ui/flatten_and_doc.stderr deleted file mode 100644 index fec9a79bb97..00000000000 --- a/clap_derive/tests/ui/flatten_and_doc.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: methods and doc comments are not allowed for flattened entry - --> $DIR/flatten_and_doc.rs:23:12 - | -23 | #[clap(flatten)] - | ^^^^^^^ diff --git a/clap_derive/tests/ui/flatten_and_methods.stderr b/clap_derive/tests/ui/flatten_and_methods.stderr index 0b86ceeda61..772f2df8c38 100644 --- a/clap_derive/tests/ui/flatten_and_methods.stderr +++ b/clap_derive/tests/ui/flatten_and_methods.stderr @@ -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:19 | 22 | #[clap(short, flatten)]