rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.38k stars 12.72k forks source link

Incorrect `unnecessary parenthesis` inside attribute parameters #104457

Open akrieger opened 1 year ago

akrieger commented 1 year ago

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5b455db4401f5a5f2bd6bec013fab8de

use clap::{Parser};

#[derive(Debug, Parser)]
pub struct Args {
    #[arg(short, long, value_name="ingredients.txt", default_value_t=("ingredients.rs".to_owned()))]
    ingredients: String,
}

The current output is:

warning: unnecessary parentheses around assigned value
 --> src/lib.rs:5:70
  |
5 |     #[arg(short, long, value_name="ingredients.txt", default_value_t=("ingredients.rs".to_owned()))]
  |                                                                      ^                           ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
5 -     #[arg(short, long, value_name="ingredients.txt", default_value_t=("ingredients.rs".to_owned()))]
5 +     #[arg(short, long, value_name="ingredients.txt", default_value_t="ingredients.rs".to_owned())]
  |

However removing the parenthesis results in

error: expected `,`
 --> src/lib.rs:5:86
  |
5 |     #[arg(short, long, value_name="ingredients.txt", default_value_t="ingredients.rs".to_owned())]
  |                                                                                      ^
chenyukang commented 1 year ago

I think this unnecessary parenthesis is right, the parenthesis is not necessary.

The "expected ," error should comes from clap. For string value, if you want to use default, maybe you need write like this:

use clap::{Parser, ValueEnum};

#[derive(Parser, Debug)]
struct Args {
    #[clap(value_enum, default_value_t=Level::Debug)]
    level: Level,
}

#[derive(ValueEnum, Clone, Debug)]
enum Level {
    Debug,
    Info,
    Warning,
    Error,
}

fn main() {
    println!("{:?}", Args::parse());
}

Or this: https://github.com/clap-rs/clap/blob/ba32ab87b2b126f7035f2858cf3b9b5d810fa135/examples/escaped-positional-derive.rs#L10