Skip to content

Commit e0a74c3

Browse files
committed
fix: manual_instant_elapsed wrongly unmangled macros
1 parent e95cedd commit e0a74c3

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

clippy_lints/src/time_subtraction.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,8 @@ impl LateLintPass<'_> for UncheckedTimeSubtraction {
9696

9797
if lhs_ty.is_diag_item(cx, sym::Instant) {
9898
// Instant::now() - instant
99-
if is_instant_now_call(cx, lhs)
100-
&& rhs_ty.is_diag_item(cx, sym::Instant)
101-
&& let Some(sugg) = Sugg::hir_opt(cx, rhs)
102-
{
103-
print_manual_instant_elapsed_sugg(cx, expr, sugg);
99+
if is_instant_now_call(cx, lhs) && rhs_ty.is_diag_item(cx, sym::Instant) {
100+
print_manual_instant_elapsed_sugg(cx, expr, rhs);
104101
}
105102
// instant - duration
106103
else if rhs_ty.is_diag_item(cx, sym::Duration)
@@ -150,15 +147,17 @@ fn is_time_type(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
150147
ty.is_diag_item(cx, sym::Duration) || ty.is_diag_item(cx, sym::Instant)
151148
}
152149

153-
fn print_manual_instant_elapsed_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Sugg<'_>) {
150+
fn print_manual_instant_elapsed_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, rhs: &Expr<'_>) {
151+
let mut applicability = Applicability::MachineApplicable;
152+
let sugg = Sugg::hir_with_context(cx, rhs, expr.span.ctxt(), "<instant>", &mut applicability);
154153
span_lint_and_sugg(
155154
cx,
156155
MANUAL_INSTANT_ELAPSED,
157156
expr.span,
158157
"manual implementation of `Instant::elapsed`",
159158
"try",
160159
format!("{}.elapsed()", sugg.maybe_paren()),
161-
Applicability::MachineApplicable,
160+
applicability,
162161
);
163162
}
164163

tests/ui/manual_instant_elapsed.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,19 @@ fn main() {
2828
//
2929
//~^^ manual_instant_elapsed
3030
}
31+
32+
fn issue16236() {
33+
use std::ops::Sub as _;
34+
macro_rules! deref {
35+
($e:expr) => {
36+
*$e
37+
};
38+
}
39+
40+
let start = &Instant::now();
41+
let _ = deref!(start).elapsed();
42+
//~^ manual_instant_elapsed
43+
44+
deref!(start).elapsed();
45+
//~^ manual_instant_elapsed
46+
}

tests/ui/manual_instant_elapsed.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,19 @@ fn main() {
2828
//
2929
//~^^ manual_instant_elapsed
3030
}
31+
32+
fn issue16236() {
33+
use std::ops::Sub as _;
34+
macro_rules! deref {
35+
($e:expr) => {
36+
*$e
37+
};
38+
}
39+
40+
let start = &Instant::now();
41+
let _ = Instant::now().sub(deref!(start));
42+
//~^ manual_instant_elapsed
43+
44+
Instant::now() - deref!(start);
45+
//~^ manual_instant_elapsed
46+
}

tests/ui/manual_instant_elapsed.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,17 @@ error: manual implementation of `Instant::elapsed`
1313
LL | Instant::now() - *ref_to_instant; // to ensure parens are added correctly
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(*ref_to_instant).elapsed()`
1515

16-
error: aborting due to 2 previous errors
16+
error: manual implementation of `Instant::elapsed`
17+
--> tests/ui/manual_instant_elapsed.rs:41:13
18+
|
19+
LL | let _ = Instant::now().sub(deref!(start));
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `deref!(start).elapsed()`
21+
22+
error: manual implementation of `Instant::elapsed`
23+
--> tests/ui/manual_instant_elapsed.rs:44:5
24+
|
25+
LL | Instant::now() - deref!(start);
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `deref!(start).elapsed()`
27+
28+
error: aborting due to 4 previous errors
1729

0 commit comments

Comments
 (0)