]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/replace_consts.rs
Auto merge of #5042 - JohnTitor:rustup-0112, 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_errors::Applicability;
4 use rustc_hir::def::{DefKind, Res};
5 use rustc_hir::*;
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for usage of standard library
11     /// `const`s that could be replaced by `const fn`s.
12     ///
13     /// **Why is this bad?** `const fn`s exist
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     /// ```rust
19     /// let x = std::u32::MIN;
20     /// let y = std::u32::MAX;
21     /// ```
22     ///
23     /// Could be written:
24     ///
25     /// ```rust
26     /// let x = u32::min_value();
27     /// let y = u32::max_value();
28     /// ```
29     pub REPLACE_CONSTS,
30     pedantic,
31     "Lint usages of standard library `const`s that could be replaced by `const fn`s"
32 }
33
34 declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
35
36 fn in_pattern(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
37     let map = &cx.tcx.hir();
38     let parent_id = map.get_parent_node(expr.hir_id);
39
40     if let Some(node) = map.find(parent_id) {
41         if let Node::Pat(_) = node {
42             return true;
43         }
44     }
45
46     false
47 }
48
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
50     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
51         if_chain! {
52             if let ExprKind::Path(ref qp) = expr.kind;
53             if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
54             // Do not lint within patterns as function calls are disallowed in them
55             if !in_pattern(cx, expr);
56             then {
57                 for &(ref const_path, repl_snip) in &REPLACEMENTS {
58                     if match_def_path(cx, def_id, const_path) {
59                         span_lint_and_sugg(
60                             cx,
61                             REPLACE_CONSTS,
62                             expr.span,
63                             &format!("using `{}`", const_path.last().expect("empty path")),
64                             "try this",
65                             repl_snip.to_string(),
66                             Applicability::MachineApplicable,
67                         );
68                         return;
69                     }
70                 }
71             }
72         }
73     }
74 }
75
76 const REPLACEMENTS: [([&str; 3], &str); 24] = [
77     // Min
78     (["core", "isize", "MIN"], "isize::min_value()"),
79     (["core", "i8", "MIN"], "i8::min_value()"),
80     (["core", "i16", "MIN"], "i16::min_value()"),
81     (["core", "i32", "MIN"], "i32::min_value()"),
82     (["core", "i64", "MIN"], "i64::min_value()"),
83     (["core", "i128", "MIN"], "i128::min_value()"),
84     (["core", "usize", "MIN"], "usize::min_value()"),
85     (["core", "u8", "MIN"], "u8::min_value()"),
86     (["core", "u16", "MIN"], "u16::min_value()"),
87     (["core", "u32", "MIN"], "u32::min_value()"),
88     (["core", "u64", "MIN"], "u64::min_value()"),
89     (["core", "u128", "MIN"], "u128::min_value()"),
90     // Max
91     (["core", "isize", "MAX"], "isize::max_value()"),
92     (["core", "i8", "MAX"], "i8::max_value()"),
93     (["core", "i16", "MAX"], "i16::max_value()"),
94     (["core", "i32", "MAX"], "i32::max_value()"),
95     (["core", "i64", "MAX"], "i64::max_value()"),
96     (["core", "i128", "MAX"], "i128::max_value()"),
97     (["core", "usize", "MAX"], "usize::max_value()"),
98     (["core", "u8", "MAX"], "u8::max_value()"),
99     (["core", "u16", "MAX"], "u16::max_value()"),
100     (["core", "u32", "MAX"], "u32::max_value()"),
101     (["core", "u64", "MAX"], "u64::max_value()"),
102     (["core", "u128", "MAX"], "u128::max_value()"),
103 ];