Skip to content

Fix in clippy::unchecked_duration_subtraction produces unclearer panic #10061

Open
@umgefahren

Description

@umgefahren

Summary

The fix for a case of clippy::unchecked_duration_substraction seems to result in a less helpful panic message then without applying the fix.

Example

Considering the reproducer; without the fix we get the following runtime panic error:

thread 'main' panicked at 'overflow when subtracting duration from instant', library/std/src/time.rs:424:9

which is very descriptive on what happened.

Applying the fix, we get a way less descriptive error:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:2:81

It's way harder from this to figure out what happened. So I think one would be better off just leaving the line alone. Considering this either don't apply a fix or move the lint to the pedantic group, but that's just from this little experience. Maybe I missed something.

Linking the issue in which we discovered this problem: libp2p/rust-libp2p#3220 (comment)

Reproducer

I tried this code:

let delta = std::time::Instant::now() - std::time::Duration::MAX;
println!("Delta: {delta:?}");

I expected to see this happen:

No changes were applied.

Instead, this happened:

Got changed to:

let delta = std::time::Instant::now().checked_sub(std::time::Duration::MAX).unwrap();
println!("Delta: {delta:?}");

Version

rustc 1.68.0-nightly (dfe3fe710 2022-12-09)
binary: rustc
commit-hash: dfe3fe710181738a2cb3060c23ec5efb3c68ca09
commit-date: 2022-12-09
host: x86_64-apple-darwin
release: 1.68.0-nightly
LLVM version: 15.0.6

Additional Labels

@rustbot label +L-suggestion +L-correctness +I-false-positive +C-bug +C-question +C-enhancement

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesC-questionCategory: QuestionsI-false-positiveIssue: The lint was triggered on code it shouldn't haveL-correctnessLint: Belongs in the correctness lint groupL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions