Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend unchecked_duration_subtraction to check for Duration - Duration #13800

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions clippy_lints/src/instant_subtraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare_clippy_lint! {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, can we also rename the struct (and the file) from InstantSubstraction?

Seems like it won't give the right idea after the changes.

declare_clippy_lint! {
/// ### What it does
/// Lints subtraction between an `Instant` and a `Duration`.
/// Lints subtraction between an `Instant` or `Duration` and a `Duration`.
///
/// ### Why is this bad?
/// Unchecked subtraction could cause underflow on certain platforms, leading to
Expand All @@ -51,17 +51,19 @@ declare_clippy_lint! {
/// ```no_run
/// # use std::time::{Instant, Duration};
/// let time_passed = Instant::now() - Duration::from_secs(5);
/// let time_delta = Duration::from_secs(1) - Duration::from_secs(5);
/// ```
///
/// Use instead:
/// ```no_run
/// # use std::time::{Instant, Duration};
/// let time_passed = Instant::now().checked_sub(Duration::from_secs(5));
/// let time_delta = Duration::from_secs(1).checked_sub(Duration::from_secs(5));
/// ```
#[clippy::version = "1.67.0"]
pub UNCHECKED_DURATION_SUBTRACTION,
pedantic,
"finds unchecked subtraction of a 'Duration' from an 'Instant'"
"finds unchecked subtraction of a 'Duration' from an 'Instant' or 'Duration'"
}

pub struct InstantSubtraction {
Expand All @@ -88,7 +90,8 @@ impl LateLintPass<'_> for InstantSubtraction {
rhs,
) = expr.kind
&& let typeck = cx.typeck_results()
&& ty::is_type_diagnostic_item(cx, typeck.expr_ty(lhs), sym::Instant)
&& (ty::is_type_diagnostic_item(cx, typeck.expr_ty(lhs), sym::Instant)
|| ty::is_type_diagnostic_item(cx, typeck.expr_ty(lhs), sym::Duration))
{
let rhs_ty = typeck.expr_ty(rhs);

Expand Down Expand Up @@ -139,16 +142,23 @@ fn print_unchecked_duration_subtraction_sugg(
expr: &Expr<'_>,
) {
let mut applicability = Applicability::MachineApplicable;
let ty = cx.typeck_results().expr_ty(expr).peel_refs();
let (left_default, left_ty) = match ty::get_type_diagnostic_name(cx, ty) {
Some(v) => (format!("<{}>", v.as_str().to_lowercase()), v.as_str().to_string()),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure if this is needed (the 'default'), thoughts? any better way to handle?

None => {
return;
},
};

let ctxt = expr.span.ctxt();
let left_expr = snippet_with_context(cx, left_expr.span, ctxt, "<instant>", &mut applicability).0;
let left_expr = snippet_with_context(cx, left_expr.span, ctxt, &left_default, &mut applicability).0;
let right_expr = snippet_with_context(cx, right_expr.span, ctxt, "<duration>", &mut applicability).0;

span_lint_and_sugg(
cx,
UNCHECKED_DURATION_SUBTRACTION,
expr.span,
"unchecked subtraction of a 'Duration' from an 'Instant'",
format!("unchecked subtraction of 'Duration' from '{left_ty}'"),
"try",
format!("{left_expr}.checked_sub({right_expr}).unwrap()"),
applicability,
Expand Down
17 changes: 12 additions & 5 deletions tests/ui/unchecked_duration_subtraction.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
use std::time::{Duration, Instant};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add some test cases for Duration - Duration - Duration and see if the fixes are correct?


fn main() {
let _first = Instant::now();
let second = Duration::from_secs(3);
let instant = Instant::now();
let duration = Duration::from_secs(3);
let duration2 = Duration::from_secs(1);

let _ = _first.checked_sub(second).unwrap();
let _ = instant.checked_sub(duration).unwrap();

let _ = Instant::now().checked_sub(Duration::from_secs(5)).unwrap();

let _ = _first.checked_sub(Duration::from_secs(5)).unwrap();
let _ = instant.checked_sub(Duration::from_secs(5)).unwrap();

let _ = Instant::now().checked_sub(second).unwrap();
let _ = Instant::now().checked_sub(duration).unwrap();

let _ = Duration::from_secs(1).checked_sub(duration).unwrap();

let _ = duration2.checked_sub(duration).unwrap();

let _ = Instant::now().elapsed().checked_sub(duration).unwrap();
}
17 changes: 12 additions & 5 deletions tests/ui/unchecked_duration_subtraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
use std::time::{Duration, Instant};

fn main() {
let _first = Instant::now();
let second = Duration::from_secs(3);
let instant = Instant::now();
let duration = Duration::from_secs(3);
let duration2 = Duration::from_secs(1);

let _ = _first - second;
let _ = instant - duration;

let _ = Instant::now() - Duration::from_secs(5);

let _ = _first - Duration::from_secs(5);
let _ = instant - Duration::from_secs(5);

let _ = Instant::now() - second;
let _ = Instant::now() - duration;

let _ = Duration::from_secs(1) - duration;

let _ = duration2 - duration;

let _ = Instant::now().elapsed() - duration;
}
48 changes: 33 additions & 15 deletions tests/ui/unchecked_duration_subtraction.stderr
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
error: unchecked subtraction of a 'Duration' from an 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:9:13
error: unchecked subtraction of 'Duration' from 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:10:13
|
LL | let _ = _first - second;
| ^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(second).unwrap()`
LL | let _ = instant - duration;
| ^^^^^^^^^^^^^^^^^^ help: try: `instant.checked_sub(duration).unwrap()`
|
= note: `-D clippy::unchecked-duration-subtraction` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unchecked_duration_subtraction)]`

error: unchecked subtraction of a 'Duration' from an 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:11:13
error: unchecked subtraction of 'Duration' from 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:12:13
|
LL | let _ = Instant::now() - Duration::from_secs(5);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(Duration::from_secs(5)).unwrap()`

error: unchecked subtraction of a 'Duration' from an 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:13:13
error: unchecked subtraction of 'Duration' from 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:14:13
|
LL | let _ = _first - Duration::from_secs(5);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(Duration::from_secs(5)).unwrap()`
LL | let _ = instant - Duration::from_secs(5);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `instant.checked_sub(Duration::from_secs(5)).unwrap()`

error: unchecked subtraction of a 'Duration' from an 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:15:13
error: unchecked subtraction of 'Duration' from 'Instant'
--> tests/ui/unchecked_duration_subtraction.rs:16:13
|
LL | let _ = Instant::now() - second;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(second).unwrap()`
LL | let _ = Instant::now() - duration;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(duration).unwrap()`

error: aborting due to 4 previous errors
error: unchecked subtraction of 'Duration' from 'Duration'
--> tests/ui/unchecked_duration_subtraction.rs:18:13
|
LL | let _ = Duration::from_secs(1) - duration;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Duration::from_secs(1).checked_sub(duration).unwrap()`

error: unchecked subtraction of 'Duration' from 'Duration'
--> tests/ui/unchecked_duration_subtraction.rs:20:13
|
LL | let _ = duration2 - duration;
| ^^^^^^^^^^^^^^^^^^^^ help: try: `duration2.checked_sub(duration).unwrap()`

error: unchecked subtraction of 'Duration' from 'Duration'
--> tests/ui/unchecked_duration_subtraction.rs:22:13
|
LL | let _ = Instant::now().elapsed() - duration;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().elapsed().checked_sub(duration).unwrap()`

error: aborting due to 7 previous errors

Loading