Skip to content

Commit 32f5ae1

Browse files
committed
Give the same message for Instant - Duration and Duration - Duration
Diff best seen with whitespace ignored
1 parent e1344da commit 32f5ae1

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

clippy_lints/src/time_subtraction.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,29 +171,26 @@ fn print_unchecked_duration_subtraction_sugg(
171171
right_expr: &Expr<'_>,
172172
expr: &Expr<'_>,
173173
) {
174-
let typeck = cx.typeck_results();
175-
let left_ty = typeck.expr_ty(left_expr);
176-
177-
let lint_msg = if left_ty.is_diag_item(cx, sym::Instant) {
178-
"unchecked subtraction of a 'Duration' from an 'Instant'"
179-
} else {
180-
"unchecked subtraction between 'Duration' values"
181-
};
182-
183-
span_lint_and_then(cx, UNCHECKED_TIME_SUBTRACTION, expr.span, lint_msg, |diag| {
184-
// For chained subtraction, like `(dur1 - dur2) - dur3` or `(instant - dur1) - dur2`,
185-
// avoid suggestions
186-
if !is_chained_time_subtraction(cx, left_expr) {
187-
let mut applicability = Applicability::MachineApplicable;
188-
let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability);
189-
let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability);
174+
span_lint_and_then(
175+
cx,
176+
UNCHECKED_TIME_SUBTRACTION,
177+
expr.span,
178+
"unchecked subtraction of a `Duration`",
179+
|diag| {
180+
// For chained subtraction, like `(dur1 - dur2) - dur3` or `(instant - dur1) - dur2`,
181+
// avoid suggestions
182+
if !is_chained_time_subtraction(cx, left_expr) {
183+
let mut applicability = Applicability::MachineApplicable;
184+
let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability);
185+
let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability);
190186

191-
diag.span_suggestion(
192-
expr.span,
193-
"try",
194-
format!("{}.checked_sub({}).unwrap()", left_sugg.maybe_paren(), right_sugg),
195-
applicability,
196-
);
197-
}
198-
});
187+
diag.span_suggestion(
188+
expr.span,
189+
"try",
190+
format!("{}.checked_sub({}).unwrap()", left_sugg.maybe_paren(), right_sugg),
191+
applicability,
192+
);
193+
}
194+
},
195+
);
199196
}

tests/ui/unchecked_time_subtraction.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: unchecked subtraction of a 'Duration' from an 'Instant'
1+
error: unchecked subtraction of a `Duration`
22
--> tests/ui/unchecked_time_subtraction.rs:9:13
33
|
44
LL | let _ = _first - second;
@@ -7,43 +7,43 @@ LL | let _ = _first - second;
77
= note: `-D clippy::unchecked-time-subtraction` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::unchecked_time_subtraction)]`
99

10-
error: unchecked subtraction of a 'Duration' from an 'Instant'
10+
error: unchecked subtraction of a `Duration`
1111
--> tests/ui/unchecked_time_subtraction.rs:12:13
1212
|
1313
LL | let _ = Instant::now() - Duration::from_secs(5);
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(Duration::from_secs(5)).unwrap()`
1515

16-
error: unchecked subtraction of a 'Duration' from an 'Instant'
16+
error: unchecked subtraction of a `Duration`
1717
--> tests/ui/unchecked_time_subtraction.rs:15:13
1818
|
1919
LL | let _ = _first - Duration::from_secs(5);
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(Duration::from_secs(5)).unwrap()`
2121

22-
error: unchecked subtraction of a 'Duration' from an 'Instant'
22+
error: unchecked subtraction of a `Duration`
2323
--> tests/ui/unchecked_time_subtraction.rs:18:13
2424
|
2525
LL | let _ = Instant::now() - second;
2626
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(second).unwrap()`
2727

28-
error: unchecked subtraction between 'Duration' values
28+
error: unchecked subtraction of a `Duration`
2929
--> tests/ui/unchecked_time_subtraction.rs:25:13
3030
|
3131
LL | let _ = dur1 - dur2;
3232
| ^^^^^^^^^^^ help: try: `dur1.checked_sub(dur2).unwrap()`
3333

34-
error: unchecked subtraction between 'Duration' values
34+
error: unchecked subtraction of a `Duration`
3535
--> tests/ui/unchecked_time_subtraction.rs:28:13
3636
|
3737
LL | let _ = Duration::from_secs(10) - Duration::from_secs(5);
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Duration::from_secs(10).checked_sub(Duration::from_secs(5)).unwrap()`
3939

40-
error: unchecked subtraction between 'Duration' values
40+
error: unchecked subtraction of a `Duration`
4141
--> tests/ui/unchecked_time_subtraction.rs:31:13
4242
|
4343
LL | let _ = second - dur1;
4444
| ^^^^^^^^^^^^^ help: try: `second.checked_sub(dur1).unwrap()`
4545

46-
error: unchecked subtraction between 'Duration' values
46+
error: unchecked subtraction of a `Duration`
4747
--> tests/ui/unchecked_time_subtraction.rs:35:13
4848
|
4949
LL | let _ = 2 * dur1 - dur2;

tests/ui/unchecked_time_subtraction_unfixable.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: unchecked subtraction between 'Duration' values
1+
error: unchecked subtraction of a `Duration`
22
--> tests/ui/unchecked_time_subtraction_unfixable.rs:12:13
33
|
44
LL | let _ = dur1 - dur2 - dur3;
@@ -7,19 +7,19 @@ LL | let _ = dur1 - dur2 - dur3;
77
= note: `-D clippy::unchecked-time-subtraction` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::unchecked_time_subtraction)]`
99

10-
error: unchecked subtraction between 'Duration' values
10+
error: unchecked subtraction of a `Duration`
1111
--> tests/ui/unchecked_time_subtraction_unfixable.rs:12:13
1212
|
1313
LL | let _ = dur1 - dur2 - dur3;
1414
| ^^^^^^^^^^^ help: try: `dur1.checked_sub(dur2).unwrap()`
1515

16-
error: unchecked subtraction of a 'Duration' from an 'Instant'
16+
error: unchecked subtraction of a `Duration`
1717
--> tests/ui/unchecked_time_subtraction_unfixable.rs:19:13
1818
|
1919
LL | let _ = instant1 - dur2 - dur3;
2020
| ^^^^^^^^^^^^^^^^^^^^^^
2121

22-
error: unchecked subtraction of a 'Duration' from an 'Instant'
22+
error: unchecked subtraction of a `Duration`
2323
--> tests/ui/unchecked_time_subtraction_unfixable.rs:19:13
2424
|
2525
LL | let _ = instant1 - dur2 - dur3;

0 commit comments

Comments
 (0)