]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/replace_consts.rs
Auto merge of #4683 - HMPerson1:inefficient_to_string, r=Manishearth
[rust.git] / clippy_lints / src / replace_consts.rs
1 use crate::utils::{match_def_path, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc::hir;
4 use rustc::hir::def::{DefKind, Res};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
7 use rustc_errors::Applicability;
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and
11     /// `uX/iX::MIN/MAX`.
12     ///
13     /// **Why is this bad?** `const fn`s exist
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     /// ```rust
19     /// # use core::sync::atomic::{ATOMIC_ISIZE_INIT, AtomicIsize};
20     /// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
21     /// ```
22     ///
23     /// Could be written:
24     ///
25     /// ```rust
26     /// # use core::sync::atomic::AtomicIsize;
27     /// static FOO: AtomicIsize = AtomicIsize::new(0);
28     /// ```
29     pub REPLACE_CONSTS,
30     pedantic,
31     "Lint usages of standard library `const`s that could be replaced by `const fn`s"
32 }
33
34 declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
35
36 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
37     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
38         if_chain! {
39             if let hir::ExprKind::Path(ref qp) = expr.kind;
40             if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
41             then {
42                 for &(ref const_path, repl_snip) in &REPLACEMENTS {
43                     if match_def_path(cx, def_id, const_path) {
44                         span_lint_and_sugg(
45                             cx,
46                             REPLACE_CONSTS,
47                             expr.span,
48                             &format!("using `{}`", const_path.last().expect("empty path")),
49                             "try this",
50                             repl_snip.to_string(),
51                             Applicability::MachineApplicable,
52                         );
53                         return;
54                     }
55                 }
56             }
57         }
58     }
59 }
60
61 const REPLACEMENTS: [([&str; 3], &str); 24] = [
62     // Min
63     (["core", "isize", "MIN"], "isize::min_value()"),
64     (["core", "i8", "MIN"], "i8::min_value()"),
65     (["core", "i16", "MIN"], "i16::min_value()"),
66     (["core", "i32", "MIN"], "i32::min_value()"),
67     (["core", "i64", "MIN"], "i64::min_value()"),
68     (["core", "i128", "MIN"], "i128::min_value()"),
69     (["core", "usize", "MIN"], "usize::min_value()"),
70     (["core", "u8", "MIN"], "u8::min_value()"),
71     (["core", "u16", "MIN"], "u16::min_value()"),
72     (["core", "u32", "MIN"], "u32::min_value()"),
73     (["core", "u64", "MIN"], "u64::min_value()"),
74     (["core", "u128", "MIN"], "u128::min_value()"),
75     // Max
76     (["core", "isize", "MAX"], "isize::max_value()"),
77     (["core", "i8", "MAX"], "i8::max_value()"),
78     (["core", "i16", "MAX"], "i16::max_value()"),
79     (["core", "i32", "MAX"], "i32::max_value()"),
80     (["core", "i64", "MAX"], "i64::max_value()"),
81     (["core", "i128", "MAX"], "i128::max_value()"),
82     (["core", "usize", "MAX"], "usize::max_value()"),
83     (["core", "u8", "MAX"], "u8::max_value()"),
84     (["core", "u16", "MAX"], "u16::max_value()"),
85     (["core", "u32", "MAX"], "u32::max_value()"),
86     (["core", "u64", "MAX"], "u64::max_value()"),
87     (["core", "u128", "MAX"], "u128::max_value()"),
88 ];