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