2023-11-02 11:35:56 -05:00
|
|
|
use clippy_config::msrvs::{self, Msrv};
|
2022-12-01 11:29:38 -06:00
|
|
|
use clippy_utils::diagnostics::{self, span_lint_and_sugg};
|
2023-03-10 03:53:50 -06:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
2022-12-01 11:29:38 -06:00
|
|
|
use clippy_utils::sugg::Sugg;
|
|
|
|
use clippy_utils::ty;
|
2022-11-21 13:34:47 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::impl_lint_pass;
|
2023-07-17 03:19:29 -05:00
|
|
|
use rustc_span::source_map::Spanned;
|
|
|
|
use rustc_span::sym;
|
2022-11-21 13:34:47 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Lints subtraction between `Instant::now()` and another `Instant`.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It is easy to accidentally write `prev_instant - Instant::now()`, which will always be 0ns
|
|
|
|
/// as `Instant` subtraction saturates.
|
|
|
|
///
|
|
|
|
/// `prev_instant.elapsed()` also more clearly signals intention.
|
|
|
|
///
|
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-11-21 13:34:47 -06:00
|
|
|
/// use std::time::Instant;
|
|
|
|
/// let prev_instant = Instant::now();
|
|
|
|
/// let duration = Instant::now() - prev_instant;
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-11-21 13:34:47 -06:00
|
|
|
/// use std::time::Instant;
|
|
|
|
/// let prev_instant = Instant::now();
|
|
|
|
/// let duration = prev_instant.elapsed();
|
|
|
|
/// ```
|
|
|
|
#[clippy::version = "1.65.0"]
|
|
|
|
pub MANUAL_INSTANT_ELAPSED,
|
|
|
|
pedantic,
|
|
|
|
"subtraction between `Instant::now()` and previous `Instant`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
2024-01-11 10:27:03 -06:00
|
|
|
/// Lints subtraction between an `Instant` and a `Duration`.
|
2022-11-21 13:34:47 -06:00
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Unchecked subtraction could cause underflow on certain platforms, leading to
|
|
|
|
/// unintentional panics.
|
|
|
|
///
|
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-11-21 13:34:47 -06:00
|
|
|
/// # use std::time::{Instant, Duration};
|
|
|
|
/// let time_passed = Instant::now() - Duration::from_secs(5);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-11-21 13:34:47 -06:00
|
|
|
/// # use std::time::{Instant, Duration};
|
|
|
|
/// let time_passed = Instant::now().checked_sub(Duration::from_secs(5));
|
|
|
|
/// ```
|
2023-01-27 14:09:08 -06:00
|
|
|
#[clippy::version = "1.67.0"]
|
2022-11-21 13:34:47 -06:00
|
|
|
pub UNCHECKED_DURATION_SUBTRACTION,
|
2023-01-12 18:04:28 -06:00
|
|
|
pedantic,
|
2022-11-21 13:34:47 -06:00
|
|
|
"finds unchecked subtraction of a 'Duration' from an 'Instant'"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct InstantSubtraction {
|
2022-12-01 11:29:38 -06:00
|
|
|
msrv: Msrv,
|
2022-11-21 13:34:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InstantSubtraction {
|
|
|
|
#[must_use]
|
2022-12-01 11:29:38 -06:00
|
|
|
pub fn new(msrv: Msrv) -> Self {
|
2022-11-21 13:34:47 -06:00
|
|
|
Self { msrv }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(InstantSubtraction => [MANUAL_INSTANT_ELAPSED, UNCHECKED_DURATION_SUBTRACTION]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for InstantSubtraction {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
|
|
|
|
if let ExprKind::Binary(
|
|
|
|
Spanned {
|
|
|
|
node: BinOpKind::Sub, ..
|
|
|
|
},
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
) = expr.kind
|
|
|
|
{
|
2023-11-16 12:13:24 -06:00
|
|
|
if is_instant_now_call(cx, lhs)
|
|
|
|
&& is_an_instant(cx, rhs)
|
|
|
|
&& let Some(sugg) = Sugg::hir_opt(cx, rhs)
|
|
|
|
{
|
|
|
|
print_manual_instant_elapsed_sugg(cx, expr, sugg);
|
|
|
|
} else if !expr.span.from_expansion()
|
|
|
|
&& 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);
|
2022-11-21 13:34:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool {
|
|
|
|
if let ExprKind::Call(fn_expr, []) = expr_block.kind
|
|
|
|
&& let Some(fn_id) = clippy_utils::path_def_id(cx, fn_expr)
|
|
|
|
&& clippy_utils::match_def_path(cx, fn_id, &clippy_utils::paths::INSTANT_NOW)
|
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_an_instant(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
|
|
|
let expr_ty = cx.typeck_results().expr_ty(expr);
|
2023-09-26 22:56:38 -05:00
|
|
|
ty::is_type_diagnostic_item(cx, expr_ty, sym::Instant)
|
2022-11-21 13:34:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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<'_>) {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MANUAL_INSTANT_ELAPSED,
|
|
|
|
expr.span,
|
|
|
|
"manual implementation of `Instant::elapsed`",
|
|
|
|
"try",
|
|
|
|
format!("{}.elapsed()", sugg.maybe_par()),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_unchecked_duration_subtraction_sugg(
|
|
|
|
cx: &LateContext<'_>,
|
|
|
|
left_expr: &Expr<'_>,
|
|
|
|
right_expr: &Expr<'_>,
|
|
|
|
expr: &Expr<'_>,
|
|
|
|
) {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
|
2023-03-10 03:53:50 -06:00
|
|
|
let ctxt = expr.span.ctxt();
|
|
|
|
let left_expr = snippet_with_context(cx, left_expr.span, ctxt, "<instant>", &mut applicability).0;
|
|
|
|
let right_expr = snippet_with_context(cx, right_expr.span, ctxt, "<duration>", &mut applicability).0;
|
2022-11-21 13:34:47 -06:00
|
|
|
|
|
|
|
diagnostics::span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
UNCHECKED_DURATION_SUBTRACTION,
|
|
|
|
expr.span,
|
|
|
|
"unchecked subtraction of a 'Duration' from an 'Instant'",
|
|
|
|
"try",
|
|
|
|
format!("{left_expr}.checked_sub({right_expr}).unwrap()"),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|