2 diagnostics::{self, span_lint_and_sugg},
3 meets_msrv, msrvs, source, ty,
5 use rustc_errors::Applicability;
6 use rustc_hir::{BinOpKind, Expr, ExprKind};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_semver::RustcVersion;
9 use rustc_session::{declare_tool_lint, impl_lint_pass};
10 use rustc_span::{source_map::Spanned, sym};
12 declare_clippy_lint! {
14 /// Lints subtraction between `Instant::now()` and another `Instant`.
16 /// ### Why is this bad?
17 /// It is easy to accidentally write `prev_instant - Instant::now()`, which will always be 0ns
18 /// as `Instant` subtraction saturates.
20 /// `prev_instant.elapsed()` also more clearly signals intention.
24 /// use std::time::Instant;
25 /// let prev_instant = Instant::now();
26 /// let duration = Instant::now() - prev_instant;
30 /// use std::time::Instant;
31 /// let prev_instant = Instant::now();
32 /// let duration = prev_instant.elapsed();
34 #[clippy::version = "1.64.0"]
35 pub MANUAL_INSTANT_ELAPSED,
37 "subtraction between `Instant::now()` and previous `Instant`"
40 declare_clippy_lint! {
42 /// Finds patterns of unchecked subtraction of [`Duration`] from [`Instant::now()`].
44 /// ### Why is this bad?
45 /// Unchecked subtraction could cause underflow on certain platforms, leading to
46 /// unintentional panics.
50 /// let time_passed = Instant::now() - Duration::from_secs(5);
55 /// let time_passed = Instant::now().checked_sub(Duration::from_secs(5));
58 /// [`Duration`]: std::time::Duration
59 /// [`Instant::now()`]: std::time::Instant::now;
60 #[clippy::version = "1.65.0"]
61 pub UNCHECKED_DURATION_SUBTRACTION,
63 "finds unchecked subtraction of a 'Duration' from an 'Instant'"
66 pub struct InstantSubtraction {
67 msrv: Option<RustcVersion>,
70 impl InstantSubtraction {
72 pub fn new(msrv: Option<RustcVersion>) -> Self {
77 impl_lint_pass!(InstantSubtraction => [MANUAL_INSTANT_ELAPSED, UNCHECKED_DURATION_SUBTRACTION]);
79 impl LateLintPass<'_> for InstantSubtraction {
80 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
81 if let ExprKind::Binary(Spanned {node: BinOpKind::Sub, ..}, lhs, rhs) = expr.kind
82 && check_instant_now_call(cx, lhs)
83 && let ty_resolved = cx.typeck_results().expr_ty(rhs)
84 && let rustc_middle::ty::Adt(def, _) = ty_resolved.kind()
85 && clippy_utils::match_def_path(cx, def.did(), &clippy_utils::paths::INSTANT)
86 && let Some(sugg) = clippy_utils::sugg::Sugg::hir_opt(cx, rhs)
90 MANUAL_INSTANT_ELAPSED,
92 "manual implementation of `Instant::elapsed`",
94 format!("{}.elapsed()", sugg.maybe_par()),
95 Applicability::MachineApplicable,
99 if expr.span.from_expansion() || !meets_msrv(self.msrv, msrvs::TRY_FROM) {
104 if let ExprKind::Binary(op, lhs, rhs) = expr.kind;
106 if let BinOpKind::Sub = op.node;
108 // get types of left and right side
109 if is_an_instant(cx, lhs);
110 if is_a_duration(cx, rhs);
113 print_lint_and_sugg(cx, lhs, rhs, expr)
119 fn check_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool {
120 if let ExprKind::Call(fn_expr, []) = expr_block.kind
121 && let Some(fn_id) = clippy_utils::path_def_id(cx, fn_expr)
122 && clippy_utils::match_def_path(cx, fn_id, &clippy_utils::paths::INSTANT_NOW)
130 fn is_an_instant(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
131 let expr_ty = cx.typeck_results().expr_ty(expr);
133 match expr_ty.kind() {
134 rustc_middle::ty::Adt(def, _) => clippy_utils::match_def_path(cx, def.did(), &clippy_utils::paths::INSTANT),
139 fn is_a_duration(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
140 let expr_ty = cx.typeck_results().expr_ty(expr);
141 ty::is_type_diagnostic_item(cx, expr_ty, sym::Duration)
144 fn print_lint_and_sugg(cx: &LateContext<'_>, left_expr: &Expr<'_>, right_expr: &Expr<'_>, expr: &Expr<'_>) {
145 let mut applicability = Applicability::MachineApplicable;
148 source::snippet_with_applicability(cx, left_expr.span, "std::time::Instant::now()", &mut applicability);
149 let right_expr = source::snippet_with_applicability(
152 "std::time::Duration::from_secs(1)",
156 diagnostics::span_lint_and_sugg(
158 UNCHECKED_DURATION_SUBTRACTION,
160 "unchecked subtraction of a 'Duration' from an 'Instant'",
162 format!("{}.checked_sub({}).unwrap();", left_expr, right_expr),