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