]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/replace_consts.rs
Merge pull request #3081 from mikerite/fix-3078
[rust.git] / clippy_lints / src / replace_consts.rs
1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::{declare_lint, lint_array};
3 use if_chain::if_chain;
4 use rustc::hir;
5 use rustc::hir::def::Def;
6 use crate::utils::{match_def_path, span_lint_and_sugg};
7
8 /// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and
9 /// `uX/iX::MIN/MAX`.
10 ///
11 /// **Why is this bad?** `const fn`s exist
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
18 /// ```
19 ///
20 /// Could be written:
21 ///
22 /// ```rust
23 /// static FOO: AtomicIsize = AtomicIsize::new(0);
24 /// ```
25 declare_clippy_lint! {
26     pub REPLACE_CONSTS,
27     pedantic,
28     "Lint usages of standard library `const`s that could be replaced by `const fn`s"
29 }
30
31 pub struct ReplaceConsts;
32
33 impl LintPass for ReplaceConsts {
34     fn get_lints(&self) -> LintArray {
35         lint_array!(REPLACE_CONSTS)
36     }
37 }
38
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
40     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
41         if_chain! {
42             if let hir::ExprKind::Path(ref qp) = expr.node;
43             if let Def::Const(def_id) = cx.tables.qpath_def(qp, expr.hir_id);
44             then {
45                 for &(const_path, repl_snip) in REPLACEMENTS {
46                     if match_def_path(cx.tcx, def_id, const_path) {
47                         span_lint_and_sugg(
48                             cx,
49                             REPLACE_CONSTS,
50                             expr.span,
51                             &format!("using `{}`", const_path.last().expect("empty path")),
52                             "try this",
53                             repl_snip.to_string(),
54                         );
55                         return;
56                     }
57                 }
58             }
59         }
60     }
61 }
62
63 const REPLACEMENTS: &[(&[&str], &str)] = &[
64     // Once
65     (&["core", "sync",  "ONCE_INIT"], "Once::new()"),
66     // Atomic
67     (&["core", "sync", "atomic", "ATOMIC_BOOL_INIT"],  "AtomicBool::new(false)"),
68     (&["core", "sync", "atomic", "ATOMIC_ISIZE_INIT"], "AtomicIsize::new(0)"),
69     (&["core", "sync", "atomic", "ATOMIC_I8_INIT"],    "AtomicI8::new(0)"),
70     (&["core", "sync", "atomic", "ATOMIC_I16_INIT"],   "AtomicI16::new(0)"),
71     (&["core", "sync", "atomic", "ATOMIC_I32_INIT"],   "AtomicI32::new(0)"),
72     (&["core", "sync", "atomic", "ATOMIC_I64_INIT"],   "AtomicI64::new(0)"),
73     (&["core", "sync", "atomic", "ATOMIC_USIZE_INIT"], "AtomicUsize::new(0)"),
74     (&["core", "sync", "atomic", "ATOMIC_U8_INIT"],    "AtomicU8::new(0)"),
75     (&["core", "sync", "atomic", "ATOMIC_U16_INIT"],   "AtomicU16::new(0)"),
76     (&["core", "sync", "atomic", "ATOMIC_U32_INIT"],   "AtomicU32::new(0)"),
77     (&["core", "sync", "atomic", "ATOMIC_U64_INIT"],   "AtomicU64::new(0)"),
78     // Min
79     (&["core", "isize", "MIN"], "isize::min_value()"),
80     (&["core", "i8",    "MIN"], "i8::min_value()"),
81     (&["core", "i16",   "MIN"], "i16::min_value()"),
82     (&["core", "i32",   "MIN"], "i32::min_value()"),
83     (&["core", "i64",   "MIN"], "i64::min_value()"),
84     (&["core", "i128",  "MIN"], "i128::min_value()"),
85     (&["core", "usize", "MIN"], "usize::min_value()"),
86     (&["core", "u8",    "MIN"], "u8::min_value()"),
87     (&["core", "u16",   "MIN"], "u16::min_value()"),
88     (&["core", "u32",   "MIN"], "u32::min_value()"),
89     (&["core", "u64",   "MIN"], "u64::min_value()"),
90     (&["core", "u128",  "MIN"], "u128::min_value()"),
91     // Max
92     (&["core", "isize", "MAX"], "isize::max_value()"),
93     (&["core", "i8",    "MAX"], "i8::max_value()"),
94     (&["core", "i16",   "MAX"], "i16::max_value()"),
95     (&["core", "i32",   "MAX"], "i32::max_value()"),
96     (&["core", "i64",   "MAX"], "i64::max_value()"),
97     (&["core", "i128",  "MAX"], "i128::max_value()"),
98     (&["core", "usize", "MAX"], "usize::max_value()"),
99     (&["core", "u8",    "MAX"], "u8::max_value()"),
100     (&["core", "u16",   "MAX"], "u16::max_value()"),
101     (&["core", "u32",   "MAX"], "u32::max_value()"),
102     (&["core", "u64",   "MAX"], "u64::max_value()"),
103     (&["core", "u128",  "MAX"], "u128::max_value()"),
104 ];