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