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