]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/duration_subsec.rs
Replace some std::iter::repeat with str::repeat
[rust.git] / 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 crate::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:** Checks for calculation of subsecond microseconds or milliseconds
16     /// from other `Duration` methods.
17     ///
18     /// **Why is this bad?** It's more concise to call `Duration::subsec_micros()` or
19     /// `Duration::subsec_millis()` than to calculate them.
20     ///
21     /// **Known problems:** None.
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     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 match_type(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::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().as_ref(), 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 }