2019-05-13 23:34:08 +00:00
|
|
|
use crate::utils::sym;
|
2019-05-14 08:06:21 +00:00
|
|
|
use crate::utils::{match_def_path, span_lint_and_sugg};
|
2018-11-20 13:06:29 +00:00
|
|
|
use if_chain::if_chain;
|
2019-05-14 08:06:21 +00:00
|
|
|
use lazy_static::lazy_static;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir;
|
2019-05-04 00:03:12 +00:00
|
|
|
use rustc::hir::def::{DefKind, Res};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2019-04-08 20:43:55 +00:00
|
|
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2019-05-13 23:34:08 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2018-01-10 04:42:07 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and
|
|
|
|
/// `uX/iX::MIN/MAX`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** `const fn`s exist
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// static FOO: AtomicIsize = AtomicIsize::new(0);
|
|
|
|
/// ```
|
2018-01-10 04:42:07 +00:00
|
|
|
pub REPLACE_CONSTS,
|
2018-03-28 13:24:26 +00:00
|
|
|
pedantic,
|
2018-01-10 04:42:07 +00:00
|
|
|
"Lint usages of standard library `const`s that could be replaced by `const fn`s"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
|
2018-01-10 04:42:07 +00:00
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
|
|
|
|
if_chain! {
|
2018-07-12 07:30:57 +00:00
|
|
|
if let hir::ExprKind::Path(ref qp) = expr.node;
|
2019-05-04 00:03:12 +00:00
|
|
|
if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
|
2018-01-10 04:42:07 +00:00
|
|
|
then {
|
2019-05-13 23:34:08 +00:00
|
|
|
for (const_path, repl_snip) in REPLACEMENTS.iter() {
|
|
|
|
if match_def_path(cx, def_id, const_path) {
|
2018-01-10 04:42:07 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
REPLACE_CONSTS,
|
|
|
|
expr.span,
|
|
|
|
&format!("using `{}`", const_path.last().expect("empty path")),
|
|
|
|
"try this",
|
|
|
|
repl_snip.to_string(),
|
2018-11-27 14:13:57 +00:00
|
|
|
Applicability::MachineApplicable,
|
2018-01-10 04:42:07 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 23:34:08 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref REPLACEMENTS: [([Symbol; 3], &'static str); 25] = [
|
2018-01-10 04:42:07 +00:00
|
|
|
// Once
|
2019-05-13 23:34:08 +00:00
|
|
|
([*sym::core, *sym::sync, *sym::ONCE_INIT], "Once::new()"),
|
2018-01-10 04:42:07 +00:00
|
|
|
// Min
|
2019-05-13 23:34:08 +00:00
|
|
|
([*sym::core, *sym::isize, *sym::MIN], "isize::min_value()"),
|
|
|
|
([*sym::core, *sym::i8, *sym::MIN], "i8::min_value()"),
|
|
|
|
([*sym::core, *sym::i16, *sym::MIN], "i16::min_value()"),
|
|
|
|
([*sym::core, *sym::i32, *sym::MIN], "i32::min_value()"),
|
|
|
|
([*sym::core, *sym::i64, *sym::MIN], "i64::min_value()"),
|
|
|
|
([*sym::core, *sym::i128, *sym::MIN], "i128::min_value()"),
|
|
|
|
([*sym::core, *sym::usize, *sym::MIN], "usize::min_value()"),
|
|
|
|
([*sym::core, *sym::u8, *sym::MIN], "u8::min_value()"),
|
|
|
|
([*sym::core, *sym::u16, *sym::MIN], "u16::min_value()"),
|
|
|
|
([*sym::core, *sym::u32, *sym::MIN], "u32::min_value()"),
|
|
|
|
([*sym::core, *sym::u64, *sym::MIN], "u64::min_value()"),
|
|
|
|
([*sym::core, *sym::u128, *sym::MIN], "u128::min_value()"),
|
2018-01-10 04:42:07 +00:00
|
|
|
// Max
|
2019-05-13 23:34:08 +00:00
|
|
|
([*sym::core, *sym::isize, *sym::MAX], "isize::max_value()"),
|
|
|
|
([*sym::core, *sym::i8, *sym::MAX], "i8::max_value()"),
|
|
|
|
([*sym::core, *sym::i16, *sym::MAX], "i16::max_value()"),
|
|
|
|
([*sym::core, *sym::i32, *sym::MAX], "i32::max_value()"),
|
|
|
|
([*sym::core, *sym::i64, *sym::MAX], "i64::max_value()"),
|
|
|
|
([*sym::core, *sym::i128, *sym::MAX], "i128::max_value()"),
|
|
|
|
([*sym::core, *sym::usize, *sym::MAX], "usize::max_value()"),
|
|
|
|
([*sym::core, *sym::u8, *sym::MAX], "u8::max_value()"),
|
|
|
|
([*sym::core, *sym::u16, *sym::MAX], "u16::max_value()"),
|
|
|
|
([*sym::core, *sym::u32, *sym::MAX], "u32::max_value()"),
|
|
|
|
([*sym::core, *sym::u64, *sym::MAX], "u64::max_value()"),
|
|
|
|
([*sym::core, *sym::u128, *sym::MAX], "u128::max_value()"),
|
2018-01-10 04:42:07 +00:00
|
|
|
];
|
2019-05-13 23:34:08 +00:00
|
|
|
}
|