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