-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ declare_clippy_lint! { | |
|
||
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 | ||
|
@@ -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 { | ||
|
@@ -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); | ||
|
||
|
@@ -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()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,21 @@ | |
use std::time::{Duration, Instant}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add some test cases for |
||
|
||
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(); | ||
} |
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 | ||
|
There was a problem hiding this comment.
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.