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