fix(derive): Type check default_value_t

Fixes #2064
This commit is contained in:
Ed Page 2021-10-04 13:02:47 -05:00
parent 236cf584cf
commit d0b22b53ff
3 changed files with 41 additions and 12 deletions

View file

@ -340,27 +340,28 @@ impl Attrs {
VerbatimDocComment(ident) => self.verbatim_doc_comment = Some(ident),
DefaultValueT(ident, expr) => {
let ty = if let Some(ty) = self.ty.as_ref() {
ty
} else {
abort!(
ident,
"#[clap(default_value_t)] (without an argument) can be used \
only on field level";
note = "see \
https://docs.rs/structopt/0.3.5/structopt/#magical-methods")
};
let val = if let Some(expr) = expr {
quote!(#expr)
} else {
let ty = if let Some(ty) = self.ty.as_ref() {
ty
} else {
abort!(
ident,
"#[clap(default_value_t)] (without an argument) can be used \
only on field level";
note = "see \
https://docs.rs/structopt/0.3.5/structopt/#magical-methods")
};
quote!(<#ty as ::std::default::Default>::default())
};
let val = quote_spanned!(ident.span()=> {
clap::lazy_static::lazy_static! {
static ref DEFAULT_VALUE: &'static str = {
let val = #val;
let val: #ty = #val;
let s = ::std::string::ToString::to_string(&val);
::std::boxed::Box::leak(s.into_boxed_str())
};

View file

@ -0,0 +1,21 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use clap::Clap;
#[derive(Clap, Debug)]
#[clap(name = "basic")]
struct Opt {
#[clap(default_value_t = -10)]
value: u32,
}
fn main() {
let opt = Opt::parse();
println!("{:?}", opt);
}

View file

@ -0,0 +1,7 @@
error[E0600]: cannot apply unary operator `-` to type `u32`
--> $DIR/default_value_t_invalid.rs:14:30
|
14 | #[clap(default_value_t = -10)]
| ^^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated