Skip to content

Commit e95cedd

Browse files
committed
fix: unchecked_time_subtraction wrongly unmangled macros
1 parent 075548e commit e95cedd

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

clippy_lints/src/time_subtraction.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ fn print_unchecked_duration_subtraction_sugg(
178178
// avoid suggestions
179179
if !is_chained_time_subtraction(cx, left_expr) {
180180
let mut applicability = Applicability::MachineApplicable;
181-
let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability);
182-
let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability);
181+
let left_sugg = Sugg::hir_with_context(cx, left_expr, expr.span.ctxt(), "<left>", &mut applicability);
182+
let right_sugg =
183+
Sugg::hir_with_context(cx, right_expr, expr.span.ctxt(), "<right>", &mut applicability);
183184

184185
diag.span_suggestion(
185186
expr.span,

tests/ui/unchecked_time_subtraction.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,18 @@ fn issue16230() {
4545
let _ = Duration::ZERO.checked_sub(Duration::MAX).unwrap();
4646
//~^ unchecked_time_subtraction
4747
}
48+
49+
fn issue16234() {
50+
use std::ops::Sub as _;
51+
52+
macro_rules! duration {
53+
($secs:expr) => {
54+
Duration::from_secs($secs)
55+
};
56+
}
57+
58+
duration!(0).checked_sub(duration!(1)).unwrap();
59+
//~^ unchecked_time_subtraction
60+
let _ = duration!(0).checked_sub(duration!(1)).unwrap();
61+
//~^ unchecked_time_subtraction
62+
}

tests/ui/unchecked_time_subtraction.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,18 @@ fn issue16230() {
4545
let _ = Duration::ZERO - Duration::MAX;
4646
//~^ unchecked_time_subtraction
4747
}
48+
49+
fn issue16234() {
50+
use std::ops::Sub as _;
51+
52+
macro_rules! duration {
53+
($secs:expr) => {
54+
Duration::from_secs($secs)
55+
};
56+
}
57+
58+
duration!(0).sub(duration!(1));
59+
//~^ unchecked_time_subtraction
60+
let _ = duration!(0) - duration!(1);
61+
//~^ unchecked_time_subtraction
62+
}

tests/ui/unchecked_time_subtraction.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,17 @@ error: unchecked subtraction of a `Duration`
6161
LL | let _ = Duration::ZERO - Duration::MAX;
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Duration::ZERO.checked_sub(Duration::MAX).unwrap()`
6363

64-
error: aborting due to 10 previous errors
64+
error: unchecked subtraction of a `Duration`
65+
--> tests/ui/unchecked_time_subtraction.rs:58:5
66+
|
67+
LL | duration!(0).sub(duration!(1));
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `duration!(0).checked_sub(duration!(1)).unwrap()`
69+
70+
error: unchecked subtraction of a `Duration`
71+
--> tests/ui/unchecked_time_subtraction.rs:60:13
72+
|
73+
LL | let _ = duration!(0) - duration!(1);
74+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `duration!(0).checked_sub(duration!(1)).unwrap()`
75+
76+
error: aborting due to 12 previous errors
6577

0 commit comments

Comments
 (0)