]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/duration_subsec.rs
Auto merge of #81156 - DrMeepster:read_buf, r=joshtriplett
[rust.git] / src / tools / clippy / clippy_lints / src / duration_subsec.rs
1 use clippy_utils::source::snippet_with_applicability;
2 use clippy_utils::ty::match_type;
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir::{BinOpKind, Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::Spanned;
9
10 use clippy_utils::consts::{constant, Constant};
11 use clippy_utils::diagnostics::span_lint_and_sugg;
12 use clippy_utils::paths;
13
14 declare_clippy_lint! {
15     /// ### What it does
16     /// Checks for calculation of subsecond microseconds or milliseconds
17     /// from other `Duration` methods.
18     ///
19     /// ### Why is this bad?
20     /// It's more concise to call `Duration::subsec_micros()` or
21     /// `Duration::subsec_millis()` than to calculate them.
22     ///
23     /// ### Example
24     /// ```rust
25     /// # use std::time::Duration;
26     /// let dur = Duration::new(5, 0);
27     ///
28     /// // Bad
29     /// let _micros = dur.subsec_nanos() / 1_000;
30     /// let _millis = dur.subsec_nanos() / 1_000_000;
31     ///
32     /// // Good
33     /// let _micros = dur.subsec_micros();
34     /// let _millis = dur.subsec_millis();
35     /// ```
36     #[clippy::version = "pre 1.29.0"]
37     pub DURATION_SUBSEC,
38     complexity,
39     "checks for calculation of subsecond microseconds or milliseconds"
40 }
41
42 declare_lint_pass!(DurationSubsec => [DURATION_SUBSEC]);
43
44 impl<'tcx> LateLintPass<'tcx> for DurationSubsec {
45     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
46         if_chain! {
47             if let ExprKind::Binary(Spanned { node: BinOpKind::Div, .. }, left, right) = expr.kind;
48             if let ExprKind::MethodCall(method_path, _ , args, _) = left.kind;
49             if match_type(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::DURATION);
50             if let Some((Constant::Int(divisor), _)) = constant(cx, cx.typeck_results(), right);
51             then {
52                 let suggested_fn = match (method_path.ident.as_str().as_ref(), divisor) {
53                     ("subsec_micros", 1_000) | ("subsec_nanos", 1_000_000) => "subsec_millis",
54                     ("subsec_nanos", 1_000) => "subsec_micros",
55                     _ => return,
56                 };
57                 let mut applicability = Applicability::MachineApplicable;
58                 span_lint_and_sugg(
59                     cx,
60                     DURATION_SUBSEC,
61                     expr.span,
62                     &format!("calling `{}()` is more concise than this calculation", suggested_fn),
63                     "try",
64                     format!(
65                         "{}.{}()",
66                         snippet_with_applicability(cx, args[0].span, "_", &mut applicability),
67                         suggested_fn
68                     ),
69                     applicability,
70                 );
71             }
72         }
73     }
74 }