instant_subtraction: Reduce redundant work.

This commit is contained in:
Jason Newcomb 2024-06-12 13:33:02 -04:00
parent dc8403f0f5
commit aef0710054

View file

@ -85,16 +85,19 @@ impl LateLintPass<'_> for InstantSubtraction {
lhs, lhs,
rhs, rhs,
) = expr.kind ) = expr.kind
&& let typeck = cx.typeck_results()
&& ty::is_type_diagnostic_item(cx, typeck.expr_ty(lhs), sym::Instant)
{ {
let rhs_ty = typeck.expr_ty(rhs);
if is_instant_now_call(cx, lhs) if is_instant_now_call(cx, lhs)
&& is_an_instant(cx, rhs) && ty::is_type_diagnostic_item(cx, rhs_ty, sym::Instant)
&& let Some(sugg) = Sugg::hir_opt(cx, rhs) && let Some(sugg) = Sugg::hir_opt(cx, rhs)
{ {
print_manual_instant_elapsed_sugg(cx, expr, sugg); print_manual_instant_elapsed_sugg(cx, expr, sugg);
} else if !expr.span.from_expansion() } else if ty::is_type_diagnostic_item(cx, rhs_ty, sym::Duration)
&& !expr.span.from_expansion()
&& self.msrv.meets(msrvs::TRY_FROM) && self.msrv.meets(msrvs::TRY_FROM)
&& is_an_instant(cx, lhs)
&& is_a_duration(cx, rhs)
{ {
print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr); print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr);
} }
@ -115,16 +118,6 @@ fn is_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool {
} }
} }
fn is_an_instant(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let expr_ty = cx.typeck_results().expr_ty(expr);
ty::is_type_diagnostic_item(cx, expr_ty, sym::Instant)
}
fn is_a_duration(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let expr_ty = cx.typeck_results().expr_ty(expr);
ty::is_type_diagnostic_item(cx, expr_ty, sym::Duration)
}
fn print_manual_instant_elapsed_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Sugg<'_>) { fn print_manual_instant_elapsed_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Sugg<'_>) {
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,