rust-lang / rust-lang/rust-clippy

Fix in clippy::unchecked_duration_subtraction produces unclearer panic

Open
#10,061 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug C-enhancement C-question I-false-positive L-correctness L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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: https://github.com/libp2p/rust-libp2p/pull/3220#discussion_r1044889735

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the clippy::unchecked_duration_subtraction lint and reproduce the Instant::now() - Duration::MAX example. Examine how its suggested checked_sub(...).unwrap() changes the panic diagnostic, then determine behavior that preserves useful failure information and add regression coverage for the resulting lint output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.