Opportunity to increase compile-time safety for logic errors

⚓ Rust    📅 2025-10-24    👤 surdeus    👁️ 3      

surdeus

Recently, a bug with a big impact happened in a Rust project when the Rust-coreutils date replacement silently ignored the -r flag, as reported by LWN. I will say that I have absolutely no stake in that, coreutils, clap or anything else here, and these are just my passing observations.

One of the comments on the article mentioned that the Rust compiler ideally should have caught the problem. As it stands, with the current code, there was an unused variable which meant the -r flag was silently ignored.

In fact, it dates back all the way to the original commit of the date program. This is what the code looked like before the commit which properly added support for the -r flag:

const OPT_REFERENCE: &str = "reference";
// ...
        .arg(
            Arg::new(OPT_REFERENCE)
                .short('r')
                .long(OPT_REFERENCE)
                .value_name("FILE")
                .value_hint(clap::ValueHint::AnyPath)
                .help(translate!("date-help-reference")),
        )
// ...
// This is all the code which involves OPT_REFERENCE

In this case, the Rust compiler would not complain that OPT_REFERENCE is unused, because it's used to create a new argument. Logically, however, this is not a "real use" of the variable.

This issue could have been caught if there was a way for functions to inform the compiler that they're not actually using the variable, and it should still be considered unused for the purpose of code analysis. In that case, the clap library could be updated to add this hint for the new and long functions listed above.

2 posts - 1 participant

Read full topic

🏷️ Rust_feed