Skip to content

Commit 63c7197

Browse files
authored
refactor(time_subtraction): give the same message for Instant - Duration and Duration - Duration (#16097)
Supersedes #16057 changelog: [`time_subtraction`]: give the same message for `Instant - Duration` and `Duration - Duration`
2 parents 03ab7b8 + 32f5ae1 commit 63c7197

File tree

3 files changed

+36
-56
lines changed

3 files changed

+36
-56
lines changed

clippy_lints/src/time_subtraction.rs

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
2+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
55
use clippy_utils::sugg::Sugg;
@@ -109,36 +109,16 @@ impl LateLintPass<'_> for UncheckedTimeSubtraction {
109109
&& !expr.span.from_expansion()
110110
&& self.msrv.meets(cx, msrvs::TRY_FROM)
111111
{
112-
// For chained subtraction like (instant - dur1) - dur2, avoid suggestions
113-
if is_chained_time_subtraction(cx, lhs) {
114-
span_lint(
115-
cx,
116-
UNCHECKED_TIME_SUBTRACTION,
117-
expr.span,
118-
"unchecked subtraction of a 'Duration' from an 'Instant'",
119-
);
120-
} else {
121-
// instant - duration
122-
print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr);
123-
}
112+
print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr);
124113
}
125-
} else if lhs_ty.is_diag_item(cx, sym::Duration)
114+
}
115+
// duration - duration
116+
else if lhs_ty.is_diag_item(cx, sym::Duration)
126117
&& rhs_ty.is_diag_item(cx, sym::Duration)
127118
&& !expr.span.from_expansion()
128119
&& self.msrv.meets(cx, msrvs::TRY_FROM)
129120
{
130-
// For chained subtraction like (dur1 - dur2) - dur3, avoid suggestions
131-
if is_chained_time_subtraction(cx, lhs) {
132-
span_lint(
133-
cx,
134-
UNCHECKED_TIME_SUBTRACTION,
135-
expr.span,
136-
"unchecked subtraction between 'Duration' values",
137-
);
138-
} else {
139-
// duration - duration
140-
print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr);
141-
}
121+
print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr);
142122
}
143123
}
144124
}
@@ -191,26 +171,26 @@ fn print_unchecked_duration_subtraction_sugg(
191171
right_expr: &Expr<'_>,
192172
expr: &Expr<'_>,
193173
) {
194-
let typeck = cx.typeck_results();
195-
let left_ty = typeck.expr_ty(left_expr);
196-
197-
let lint_msg = if left_ty.is_diag_item(cx, sym::Instant) {
198-
"unchecked subtraction of a 'Duration' from an 'Instant'"
199-
} else {
200-
"unchecked subtraction between 'Duration' values"
201-
};
202-
203-
let mut applicability = Applicability::MachineApplicable;
204-
let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability);
205-
let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability);
206-
207-
span_lint_and_sugg(
174+
span_lint_and_then(
208175
cx,
209176
UNCHECKED_TIME_SUBTRACTION,
210177
expr.span,
211-
lint_msg,
212-
"try",
213-
format!("{}.checked_sub({}).unwrap()", left_sugg.maybe_paren(), right_sugg),
214-
applicability,
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);
186+
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+
},
215195
);
216196
}

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)