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