Skip to content

Commit aef0710

Browse files
committed
instant_subtraction: Reduce redundant work.
1 parent dc8403f commit aef0710

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

clippy_lints/src/instant_subtraction.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,19 @@ impl LateLintPass<'_> for InstantSubtraction {
8585
lhs,
8686
rhs,
8787
) = expr.kind
88+
&& let typeck = cx.typeck_results()
89+
&& ty::is_type_diagnostic_item(cx, typeck.expr_ty(lhs), sym::Instant)
8890
{
91+
let rhs_ty = typeck.expr_ty(rhs);
92+
8993
if is_instant_now_call(cx, lhs)
90-
&& is_an_instant(cx, rhs)
94+
&& ty::is_type_diagnostic_item(cx, rhs_ty, sym::Instant)
9195
&& let Some(sugg) = Sugg::hir_opt(cx, rhs)
9296
{
9397
print_manual_instant_elapsed_sugg(cx, expr, sugg);
94-
} else if !expr.span.from_expansion()
98+
} else if ty::is_type_diagnostic_item(cx, rhs_ty, sym::Duration)
99+
&& !expr.span.from_expansion()
95100
&& self.msrv.meets(msrvs::TRY_FROM)
96-
&& is_an_instant(cx, lhs)
97-
&& is_a_duration(cx, rhs)
98101
{
99102
print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr);
100103
}
@@ -115,16 +118,6 @@ fn is_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool {
115118
}
116119
}
117120

118-
fn is_an_instant(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
119-
let expr_ty = cx.typeck_results().expr_ty(expr);
120-
ty::is_type_diagnostic_item(cx, expr_ty, sym::Instant)
121-
}
122-
123-
fn is_a_duration(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
124-
let expr_ty = cx.typeck_results().expr_ty(expr);
125-
ty::is_type_diagnostic_item(cx, expr_ty, sym::Duration)
126-
}
127-
128121
fn print_manual_instant_elapsed_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Sugg<'_>) {
129122
span_lint_and_sugg(
130123
cx,

0 commit comments

Comments
 (0)