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