Skip to content

Typo in #[arg()] leads to opaque error message #129

Open
@BenWiederhake

Description

@BenWiederhake

Some (somewhat reasonable?) mistakes lead to completely incomprehensible and unhelpful compiler errors. For example, writing

    #[arg("--time[X]")] // WRONG!
    IgnoreOptionArg(#[allow(dead_code)] Option<OsString>),

instead of:

    #[arg("--time[=X]")] // CORRECT!
    IgnoreOptionArg(#[allow(dead_code)] Option<OsString>),

leads to this unhelpful error message:

error: proc-macro derive panicked
  --> src/uu/du/src/du.rs:79:10
   |
79 | #[derive(Arguments)]
   |          ^^^^^^^^^
   |
   = help: message: called `Option::unwrap()` on a `None` value

How should we approach this? I suggest replacing every .unwrap within derive to a .expect. This way, we at least get some information on what's wrong. This affects the following places:

$ git grep -Pirn 'unwrap\(' derive/
derive/src/argument.rs:35:            return ArgumentsAttr::parse(attr).unwrap();
derive/src/argument.rs:43:    let attributes = get_arg_attributes(&v.attrs).unwrap();
derive/src/flags.rs:64:                    .unwrap();
derive/src/flags.rs:86:            let f = chars.next().unwrap();
derive/src/help.rs:88:        ).unwrap();
derive/src/help.rs:90:        writeln!(w, "{}", #summary).unwrap();
derive/src/help.rs:92:        writeln!(w, "\nUsage:\n  {}", format!(#usage, bin_name)).unwrap();
derive/src/help.rs:96:        writeln!(w, "{}", #after_options).unwrap();
derive/src/help.rs:103:    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
derive/src/help.rs:107:    let mut f = std::fs::File::open(location).unwrap();
derive/src/help.rs:108:    f.read_to_string(&mut contents).unwrap();
derive/src/help_parser.rs:117:            parse_section("some section", input).unwrap(),
derive/src/help_parser.rs:121:            parse_section("SOME SECTION", input).unwrap(),
derive/src/help_parser.rs:125:            parse_section("another section", input).unwrap(),
derive/src/help_parser.rs:146:            parse_section("after section", input).unwrap(),
derive/src/lib.rs:76:            let mut values = parser.raw_args().unwrap().collect::<Vec<OsString>>();
derive/src/lib.rs:155:            let ValueAttr { keys, value } = ValueAttr::parse(&attr).unwrap();

However, I'm not sure whether this would result in actually-useful error messages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions