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