]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/instant_subtraction.rs
refactor: lint man. instant elapsed and unch. dur. subtr. in single pass
[rust.git] / clippy_lints / src / instant_subtraction.rs
1 use clippy_utils::{
2     diagnostics::{self, span_lint_and_sugg},
3     meets_msrv, msrvs, source, ty,
4 };
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};
11
12 declare_clippy_lint! {
13     /// ### What it does
14     /// Lints subtraction between `Instant::now()` and another `Instant`.
15     ///
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.
19     ///
20     /// `prev_instant.elapsed()` also more clearly signals intention.
21     ///
22     /// ### Example
23     /// ```rust
24     /// use std::time::Instant;
25     /// let prev_instant = Instant::now();
26     /// let duration = Instant::now() - prev_instant;
27     /// ```
28     /// Use instead:
29     /// ```rust
30     /// use std::time::Instant;
31     /// let prev_instant = Instant::now();
32     /// let duration = prev_instant.elapsed();
33     /// ```
34     #[clippy::version = "1.64.0"]
35     pub MANUAL_INSTANT_ELAPSED,
36     pedantic,
37     "subtraction between `Instant::now()` and previous `Instant`"
38 }
39
40 declare_clippy_lint! {
41     /// ### What it does
42     /// Finds patterns of unchecked subtraction of [`Duration`] from [`Instant::now()`].
43     ///
44     /// ### Why is this bad?
45     /// Unchecked subtraction could cause underflow on certain platforms, leading to
46     /// unintentional panics.
47     ///
48     /// ### Example
49     /// ```rust
50     /// let time_passed = Instant::now() - Duration::from_secs(5);
51     /// ```
52     ///
53     /// Use instead:
54     /// ```rust
55     /// let time_passed = Instant::now().checked_sub(Duration::from_secs(5));
56     /// ```
57     ///
58     /// [`Duration`]: std::time::Duration
59     /// [`Instant::now()`]: std::time::Instant::now;
60     #[clippy::version = "1.65.0"]
61     pub UNCHECKED_DURATION_SUBTRACTION,
62     suspicious,
63     "finds unchecked subtraction of a 'Duration' from an 'Instant'"
64 }
65
66 pub struct InstantSubtraction {
67     msrv: Option<RustcVersion>,
68 }
69
70 impl InstantSubtraction {
71     #[must_use]
72     pub fn new(msrv: Option<RustcVersion>) -> Self {
73         Self { msrv }
74     }
75 }
76
77 impl_lint_pass!(InstantSubtraction => [MANUAL_INSTANT_ELAPSED, UNCHECKED_DURATION_SUBTRACTION]);
78
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)
87         {
88             span_lint_and_sugg(
89                 cx,
90                 MANUAL_INSTANT_ELAPSED,
91                 expr.span,
92                 "manual implementation of `Instant::elapsed`",
93                 "try",
94                 format!("{}.elapsed()", sugg.maybe_par()),
95                 Applicability::MachineApplicable,
96             );
97         }
98
99         if expr.span.from_expansion() || !meets_msrv(self.msrv, msrvs::TRY_FROM) {
100             return;
101         }
102
103         if_chain! {
104             if let ExprKind::Binary(op, lhs, rhs) = expr.kind;
105
106             if let BinOpKind::Sub = op.node;
107
108             // get types of left and right side
109             if is_an_instant(cx, lhs);
110             if is_a_duration(cx, rhs);
111
112             then {
113                 print_lint_and_sugg(cx, lhs, rhs, expr)
114             }
115         }
116     }
117 }
118
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)
123     {
124         true
125     } else {
126         false
127     }
128 }
129
130 fn is_an_instant(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
131     let expr_ty = cx.typeck_results().expr_ty(expr);
132
133     match expr_ty.kind() {
134         rustc_middle::ty::Adt(def, _) => clippy_utils::match_def_path(cx, def.did(), &clippy_utils::paths::INSTANT),
135         _ => false,
136     }
137 }
138
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)
142 }
143
144 fn print_lint_and_sugg(cx: &LateContext<'_>, left_expr: &Expr<'_>, right_expr: &Expr<'_>, expr: &Expr<'_>) {
145     let mut applicability = Applicability::MachineApplicable;
146
147     let left_expr =
148         source::snippet_with_applicability(cx, left_expr.span, "std::time::Instant::now()", &mut applicability);
149     let right_expr = source::snippet_with_applicability(
150         cx,
151         right_expr.span,
152         "std::time::Duration::from_secs(1)",
153         &mut applicability,
154     );
155
156     diagnostics::span_lint_and_sugg(
157         cx,
158         UNCHECKED_DURATION_SUBTRACTION,
159         expr.span,
160         "unchecked subtraction of a 'Duration' from an 'Instant'",
161         "try",
162         format!("{}.checked_sub({}).unwrap();", left_expr, right_expr),
163         applicability,
164     );
165 }