1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
10 use crate::rustc::hir;
11 use crate::rustc::hir::def::Def;
12 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::rustc_errors::Applicability;
15 use crate::utils::{match_def_path, span_lint_and_sugg};
16 use if_chain::if_chain;
18 /// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and
21 /// **Why is this bad?** `const fn`s exist
23 /// **Known problems:** None.
27 /// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
33 /// static FOO: AtomicIsize = AtomicIsize::new(0);
35 declare_clippy_lint! {
38 "Lint usages of standard library `const`s that could be replaced by `const fn`s"
41 pub struct ReplaceConsts;
43 impl LintPass for ReplaceConsts {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(REPLACE_CONSTS)
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
50 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
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);
55 for &(const_path, repl_snip) in REPLACEMENTS {
56 if match_def_path(cx.tcx, def_id, const_path) {
61 &format!("using `{}`", const_path.last().expect("empty path")),
63 repl_snip.to_string(),
64 Applicability::MachineApplicable,
74 const REPLACEMENTS: &[(&[&str], &str)] = &[
76 (&["core", "sync", "ONCE_INIT"], "Once::new()"),
79 &["core", "sync", "atomic", "ATOMIC_BOOL_INIT"],
80 "AtomicBool::new(false)",
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)"),
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()"),
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()"),