2019-05-14 10:06:21 +02:00
|
|
|
use crate::utils::{match_def_path, span_lint_and_sugg};
|
2018-11-20 14:06:29 +01:00
|
|
|
use if_chain::if_chain;
|
2019-12-04 00:16:03 +01:00
|
|
|
use rustc::declare_lint_pass;
|
2018-12-29 16:04:45 +01:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-07 01:39:50 +09:00
|
|
|
use rustc_hir::def::{DefKind, Res};
|
|
|
|
use rustc_hir::*;
|
2019-12-04 00:16:03 +01:00
|
|
|
use rustc_session::declare_tool_lint;
|
2018-01-09 23:42:07 -05:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2019-12-29 09:44:50 -08:00
|
|
|
/// **What it does:** Checks for usage of standard library
|
|
|
|
/// `const`s that could be replaced by `const fn`s.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** `const fn`s exist
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-12-29 09:44:50 -08:00
|
|
|
/// let x = std::u32::MIN;
|
|
|
|
/// let y = std::u32::MAX;
|
2019-03-05 11:50:33 -05:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-12-29 09:44:50 -08:00
|
|
|
/// let x = u32::min_value();
|
|
|
|
/// let y = u32::max_value();
|
2019-03-05 11:50:33 -05:00
|
|
|
/// ```
|
2018-01-09 23:42:07 -05:00
|
|
|
pub REPLACE_CONSTS,
|
2018-03-28 15:24:26 +02:00
|
|
|
pedantic,
|
2018-01-09 23:42:07 -05:00
|
|
|
"Lint usages of standard library `const`s that could be replaced by `const fn`s"
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
|
2018-01-09 23:42:07 -05:00
|
|
|
|
2019-12-31 10:33:15 -08:00
|
|
|
fn in_pattern(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
|
|
|
|
let map = &cx.tcx.hir();
|
|
|
|
let parent_id = map.get_parent_node(expr.hir_id);
|
|
|
|
|
|
|
|
if let Some(node) = map.find(parent_id) {
|
|
|
|
if let Node::Pat(_) = node {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-01-09 23:42:07 -05:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
|
2019-12-31 10:33:15 -08:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
2018-01-09 23:42:07 -05:00
|
|
|
if_chain! {
|
2019-12-31 10:33:15 -08:00
|
|
|
if let ExprKind::Path(ref qp) = expr.kind;
|
2019-05-04 02:03:12 +02:00
|
|
|
if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
|
2019-12-31 10:33:15 -08:00
|
|
|
// Do not lint within patterns as function calls are disallowed in them
|
|
|
|
if !in_pattern(cx, expr);
|
2018-01-09 23:42:07 -05:00
|
|
|
then {
|
2019-10-16 17:12:41 -04:00
|
|
|
for &(ref const_path, repl_snip) in &REPLACEMENTS {
|
2019-05-14 01:34:08 +02:00
|
|
|
if match_def_path(cx, def_id, const_path) {
|
2018-01-09 23:42:07 -05: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 15:13:57 +01:00
|
|
|
Applicability::MachineApplicable,
|
2018-01-09 23:42:07 -05:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 12:30:11 +07:00
|
|
|
const REPLACEMENTS: [([&str; 3], &str); 24] = [
|
2018-01-09 23:42:07 -05:00
|
|
|
// Min
|
2019-05-17 23:53:54 +02:00
|
|
|
(["core", "isize", "MIN"], "isize::min_value()"),
|
|
|
|
(["core", "i8", "MIN"], "i8::min_value()"),
|
|
|
|
(["core", "i16", "MIN"], "i16::min_value()"),
|
|
|
|
(["core", "i32", "MIN"], "i32::min_value()"),
|
|
|
|
(["core", "i64", "MIN"], "i64::min_value()"),
|
|
|
|
(["core", "i128", "MIN"], "i128::min_value()"),
|
|
|
|
(["core", "usize", "MIN"], "usize::min_value()"),
|
|
|
|
(["core", "u8", "MIN"], "u8::min_value()"),
|
|
|
|
(["core", "u16", "MIN"], "u16::min_value()"),
|
|
|
|
(["core", "u32", "MIN"], "u32::min_value()"),
|
|
|
|
(["core", "u64", "MIN"], "u64::min_value()"),
|
|
|
|
(["core", "u128", "MIN"], "u128::min_value()"),
|
2018-01-09 23:42:07 -05:00
|
|
|
// Max
|
2019-05-17 23:53:54 +02:00
|
|
|
(["core", "isize", "MAX"], "isize::max_value()"),
|
|
|
|
(["core", "i8", "MAX"], "i8::max_value()"),
|
|
|
|
(["core", "i16", "MAX"], "i16::max_value()"),
|
|
|
|
(["core", "i32", "MAX"], "i32::max_value()"),
|
|
|
|
(["core", "i64", "MAX"], "i64::max_value()"),
|
|
|
|
(["core", "i128", "MAX"], "i128::max_value()"),
|
|
|
|
(["core", "usize", "MAX"], "usize::max_value()"),
|
|
|
|
(["core", "u8", "MAX"], "u8::max_value()"),
|
|
|
|
(["core", "u16", "MAX"], "u16::max_value()"),
|
|
|
|
(["core", "u32", "MAX"], "u32::max_value()"),
|
|
|
|
(["core", "u64", "MAX"], "u64::max_value()"),
|
|
|
|
(["core", "u128", "MAX"], "u128::max_value()"),
|
2018-01-09 23:42:07 -05:00
|
|
|
];
|