]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/replace_consts.rs
Auto merge of #4109 - Manishearth:backport-merge, r=phansch
[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     /// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
20     /// ```
21     ///
22     /// Could be written:
23     ///
24     /// ```rust
25     /// static FOO: AtomicIsize = AtomicIsize::new(0);
26     /// ```
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 declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
33
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
35     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
36         if_chain! {
37             if let hir::ExprKind::Path(ref qp) = expr.node;
38             if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
39             then {
40                 for (const_path, repl_snip) in &REPLACEMENTS {
41                     if match_def_path(cx, def_id, const_path) {
42                         span_lint_and_sugg(
43                             cx,
44                             REPLACE_CONSTS,
45                             expr.span,
46                             &format!("using `{}`", const_path.last().expect("empty path")),
47                             "try this",
48                             repl_snip.to_string(),
49                             Applicability::MachineApplicable,
50                         );
51                         return;
52                     }
53                 }
54             }
55         }
56     }
57 }
58
59 const REPLACEMENTS: [([&str; 3], &str); 25] = [
60     // Once
61     (["core", "sync", "ONCE_INIT"], "Once::new()"),
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 ];