fix: unchecked_time_subtraction wrongly unmangled macros

This commit is contained in:
Linshu Yang
2025-12-13 19:06:44 +00:00
parent 075548e9e9
commit e95ceddbc6
4 changed files with 46 additions and 3 deletions
+3 -2
View File
@@ -178,8 +178,9 @@ fn print_unchecked_duration_subtraction_sugg(
// avoid suggestions
if !is_chained_time_subtraction(cx, left_expr) {
let mut applicability = Applicability::MachineApplicable;
let left_sugg = Sugg::hir_with_applicability(cx, left_expr, "<left>", &mut applicability);
let right_sugg = Sugg::hir_with_applicability(cx, right_expr, "<right>", &mut applicability);
let left_sugg = Sugg::hir_with_context(cx, left_expr, expr.span.ctxt(), "<left>", &mut applicability);
let right_sugg =
Sugg::hir_with_context(cx, right_expr, expr.span.ctxt(), "<right>", &mut applicability);
diag.span_suggestion(
expr.span,
+15
View File
@@ -45,3 +45,18 @@ fn issue16230() {
let _ = Duration::ZERO.checked_sub(Duration::MAX).unwrap();
//~^ unchecked_time_subtraction
}
fn issue16234() {
use std::ops::Sub as _;
macro_rules! duration {
($secs:expr) => {
Duration::from_secs($secs)
};
}
duration!(0).checked_sub(duration!(1)).unwrap();
//~^ unchecked_time_subtraction
let _ = duration!(0).checked_sub(duration!(1)).unwrap();
//~^ unchecked_time_subtraction
}
+15
View File
@@ -45,3 +45,18 @@ fn issue16230() {
let _ = Duration::ZERO - Duration::MAX;
//~^ unchecked_time_subtraction
}
fn issue16234() {
use std::ops::Sub as _;
macro_rules! duration {
($secs:expr) => {
Duration::from_secs($secs)
};
}
duration!(0).sub(duration!(1));
//~^ unchecked_time_subtraction
let _ = duration!(0) - duration!(1);
//~^ unchecked_time_subtraction
}
+13 -1
View File
@@ -61,5 +61,17 @@ error: unchecked subtraction of a `Duration`
LL | let _ = Duration::ZERO - Duration::MAX;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Duration::ZERO.checked_sub(Duration::MAX).unwrap()`
error: aborting due to 10 previous errors
error: unchecked subtraction of a `Duration`
--> tests/ui/unchecked_time_subtraction.rs:58:5
|
LL | duration!(0).sub(duration!(1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `duration!(0).checked_sub(duration!(1)).unwrap()`
error: unchecked subtraction of a `Duration`
--> tests/ui/unchecked_time_subtraction.rs:60:13
|
LL | let _ = duration!(0) - duration!(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `duration!(0).checked_sub(duration!(1)).unwrap()`
error: aborting due to 12 previous errors