]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/replace_consts.rs
Auto merge of #3596 - xfix:remove-crate-from-paths, r=flip1995
[rust.git] / clippy_lints / src / replace_consts.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::utils::{match_def_path, span_lint_and_sugg};
11 use if_chain::if_chain;
12 use rustc::hir;
13 use rustc::hir::def::Def;
14 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
15 use rustc::{declare_tool_lint, lint_array};
16 use rustc_errors::Applicability;
17
18 /// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and
19 /// `uX/iX::MIN/MAX`.
20 ///
21 /// **Why is this bad?** `const fn`s exist
22 ///
23 /// **Known problems:** None.
24 ///
25 /// **Example:**
26 /// ```rust
27 /// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
28 /// ```
29 ///
30 /// Could be written:
31 ///
32 /// ```rust
33 /// static FOO: AtomicIsize = AtomicIsize::new(0);
34 /// ```
35 declare_clippy_lint! {
36     pub REPLACE_CONSTS,
37     pedantic,
38     "Lint usages of standard library `const`s that could be replaced by `const fn`s"
39 }
40
41 pub struct ReplaceConsts;
42
43 impl LintPass for ReplaceConsts {
44     fn get_lints(&self) -> LintArray {
45         lint_array!(REPLACE_CONSTS)
46     }
47 }
48
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
50     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
51         if_chain! {
52             if let hir::ExprKind::Path(ref qp) = expr.node;
53             if let Def::Const(def_id) = cx.tables.qpath_def(qp, expr.hir_id);
54             then {
55                 for &(const_path, repl_snip) in REPLACEMENTS {
56                     if match_def_path(cx.tcx, def_id, const_path) {
57                         span_lint_and_sugg(
58                             cx,
59                             REPLACE_CONSTS,
60                             expr.span,
61                             &format!("using `{}`", const_path.last().expect("empty path")),
62                             "try this",
63                             repl_snip.to_string(),
64                             Applicability::MachineApplicable,
65                         );
66                         return;
67                     }
68                 }
69             }
70         }
71     }
72 }
73
74 const REPLACEMENTS: &[(&[&str], &str)] = &[
75     // Once
76     (&["core", "sync", "ONCE_INIT"], "Once::new()"),
77     // Atomic
78     (
79         &["core", "sync", "atomic", "ATOMIC_BOOL_INIT"],
80         "AtomicBool::new(false)",
81     ),
82     (&["core", "sync", "atomic", "ATOMIC_ISIZE_INIT"], "AtomicIsize::new(0)"),
83     (&["core", "sync", "atomic", "ATOMIC_I8_INIT"], "AtomicI8::new(0)"),
84     (&["core", "sync", "atomic", "ATOMIC_I16_INIT"], "AtomicI16::new(0)"),
85     (&["core", "sync", "atomic", "ATOMIC_I32_INIT"], "AtomicI32::new(0)"),
86     (&["core", "sync", "atomic", "ATOMIC_I64_INIT"], "AtomicI64::new(0)"),
87     (&["core", "sync", "atomic", "ATOMIC_USIZE_INIT"], "AtomicUsize::new(0)"),
88     (&["core", "sync", "atomic", "ATOMIC_U8_INIT"], "AtomicU8::new(0)"),
89     (&["core", "sync", "atomic", "ATOMIC_U16_INIT"], "AtomicU16::new(0)"),
90     (&["core", "sync", "atomic", "ATOMIC_U32_INIT"], "AtomicU32::new(0)"),
91     (&["core", "sync", "atomic", "ATOMIC_U64_INIT"], "AtomicU64::new(0)"),
92     // Min
93     (&["core", "isize", "MIN"], "isize::min_value()"),
94     (&["core", "i8", "MIN"], "i8::min_value()"),
95     (&["core", "i16", "MIN"], "i16::min_value()"),
96     (&["core", "i32", "MIN"], "i32::min_value()"),
97     (&["core", "i64", "MIN"], "i64::min_value()"),
98     (&["core", "i128", "MIN"], "i128::min_value()"),
99     (&["core", "usize", "MIN"], "usize::min_value()"),
100     (&["core", "u8", "MIN"], "u8::min_value()"),
101     (&["core", "u16", "MIN"], "u16::min_value()"),
102     (&["core", "u32", "MIN"], "u32::min_value()"),
103     (&["core", "u64", "MIN"], "u64::min_value()"),
104     (&["core", "u128", "MIN"], "u128::min_value()"),
105     // Max
106     (&["core", "isize", "MAX"], "isize::max_value()"),
107     (&["core", "i8", "MAX"], "i8::max_value()"),
108     (&["core", "i16", "MAX"], "i16::max_value()"),
109     (&["core", "i32", "MAX"], "i32::max_value()"),
110     (&["core", "i64", "MAX"], "i64::max_value()"),
111     (&["core", "i128", "MAX"], "i128::max_value()"),
112     (&["core", "usize", "MAX"], "usize::max_value()"),
113     (&["core", "u8", "MAX"], "u8::max_value()"),
114     (&["core", "u16", "MAX"], "u16::max_value()"),
115     (&["core", "u32", "MAX"], "u32::max_value()"),
116     (&["core", "u64", "MAX"], "u64::max_value()"),
117     (&["core", "u128", "MAX"], "u128::max_value()"),
118 ];