1 use crate::utils::span_lint_and_sugg;
2 use if_chain::if_chain;
4 use rustc::hir::def::{DefKind, Res};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
7 use rustc_errors::Applicability;
10 /// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and
13 /// **Why is this bad?** `const fn`s exist
15 /// **Known problems:** None.
19 /// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
25 /// static FOO: AtomicIsize = AtomicIsize::new(0);
29 "Lint usages of standard library `const`s that could be replaced by `const fn`s"
32 declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
35 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
37 if let hir::ExprKind::Path(ref qp) = expr.node;
38 if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
40 for &(const_path, repl_snip) in REPLACEMENTS {
41 if cx.match_def_path(def_id, const_path) {
46 &format!("using `{}`", const_path.last().expect("empty path")),
48 repl_snip.to_string(),
49 Applicability::MachineApplicable,
59 const REPLACEMENTS: &[(&[&str], &str)] = &[
61 (&["core", "sync", "ONCE_INIT"], "Once::new()"),
63 (&["core", "isize", "MIN"], "isize::min_value()"),
64 (&["core", "i8", "MIN"], "i8::min_value()"),
65 (&["core", "i16", "MIN"], "i16::min_value()"),
66 (&["core", "i32", "MIN"], "i32::min_value()"),
67 (&["core", "i64", "MIN"], "i64::min_value()"),
68 (&["core", "i128", "MIN"], "i128::min_value()"),
69 (&["core", "usize", "MIN"], "usize::min_value()"),
70 (&["core", "u8", "MIN"], "u8::min_value()"),
71 (&["core", "u16", "MIN"], "u16::min_value()"),
72 (&["core", "u32", "MIN"], "u32::min_value()"),
73 (&["core", "u64", "MIN"], "u64::min_value()"),
74 (&["core", "u128", "MIN"], "u128::min_value()"),
76 (&["core", "isize", "MAX"], "isize::max_value()"),
77 (&["core", "i8", "MAX"], "i8::max_value()"),
78 (&["core", "i16", "MAX"], "i16::max_value()"),
79 (&["core", "i32", "MAX"], "i32::max_value()"),
80 (&["core", "i64", "MAX"], "i64::max_value()"),
81 (&["core", "i128", "MAX"], "i128::max_value()"),
82 (&["core", "usize", "MAX"], "usize::max_value()"),
83 (&["core", "u8", "MAX"], "u8::max_value()"),
84 (&["core", "u16", "MAX"], "u16::max_value()"),
85 (&["core", "u32", "MAX"], "u32::max_value()"),
86 (&["core", "u64", "MAX"], "u64::max_value()"),
87 (&["core", "u128", "MAX"], "u128::max_value()"),