2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-08-08 09:49:13 -05:00
|
|
|
use clippy_utils::higher;
|
2021-10-29 11:14:22 -05:00
|
|
|
use clippy_utils::SpanlessEq;
|
2020-04-06 13:07:57 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast::LitKind;
|
|
|
|
use rustc_errors::Applicability;
|
2021-04-22 04:31:13 -05:00
|
|
|
use rustc_hir::{lang_items::LangItem, BinOpKind, Expr, ExprKind, QPath, StmtKind};
|
2020-04-06 13:07:57 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for implicit saturating subtraction.
|
2020-04-06 13:07:57 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Simplicity and readability. Instead we can easily use an builtin function.
|
2020-04-06 13:07:57 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-04-06 13:07:57 -05:00
|
|
|
/// ```rust
|
2020-04-06 13:10:41 -05:00
|
|
|
/// let end: u32 = 10;
|
|
|
|
/// let start: u32 = 5;
|
2020-04-06 13:07:57 -05:00
|
|
|
///
|
2020-04-06 13:10:41 -05:00
|
|
|
/// let mut i: u32 = end - start;
|
2020-04-06 13:07:57 -05:00
|
|
|
///
|
|
|
|
/// // Bad
|
|
|
|
/// if i != 0 {
|
|
|
|
/// i -= 1;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Good
|
2020-04-06 13:10:41 -05:00
|
|
|
/// i = i.saturating_sub(1);
|
2020-04-06 13:07:57 -05:00
|
|
|
/// ```
|
|
|
|
pub IMPLICIT_SATURATING_SUB,
|
|
|
|
pedantic,
|
|
|
|
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
2021-10-29 11:14:22 -05:00
|
|
|
if expr.span.from_expansion() {
|
2020-04-06 13:07:57 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if_chain! {
|
2021-10-17 08:56:59 -05:00
|
|
|
if let Some(higher::If { cond, then, r#else: None }) = higher::If::hir(expr);
|
2020-04-06 13:10:41 -05:00
|
|
|
|
2020-04-06 13:07:57 -05:00
|
|
|
// Check if the conditional expression is a binary operation
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Binary(ref cond_op, cond_left, cond_right) = cond.kind;
|
2020-04-06 13:10:41 -05:00
|
|
|
|
|
|
|
// Ensure that the binary operator is >, != and <
|
|
|
|
if BinOpKind::Ne == cond_op.node || BinOpKind::Gt == cond_op.node || BinOpKind::Lt == cond_op.node;
|
|
|
|
|
2020-04-06 13:07:57 -05:00
|
|
|
// Check if the true condition block has only one statement
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Block(block, _) = then.kind;
|
2020-04-06 13:10:41 -05:00
|
|
|
if block.stmts.len() == 1 && block.expr.is_none();
|
|
|
|
|
2020-04-06 13:07:57 -05:00
|
|
|
// Check if assign operation is done
|
2021-04-08 10:50:13 -05:00
|
|
|
if let StmtKind::Semi(e) = block.stmts[0].kind;
|
2020-04-06 13:10:41 -05:00
|
|
|
if let Some(target) = subtracts_one(cx, e);
|
|
|
|
|
|
|
|
// Extracting out the variable name
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Path(QPath::Resolved(_, ares_path)) = target.kind;
|
2020-04-06 13:10:41 -05:00
|
|
|
|
2020-04-06 13:07:57 -05:00
|
|
|
then {
|
2020-04-06 13:10:41 -05:00
|
|
|
// Handle symmetric conditions in the if statement
|
|
|
|
let (cond_var, cond_num_val) = if SpanlessEq::new(cx).eq_expr(cond_left, target) {
|
|
|
|
if BinOpKind::Gt == cond_op.node || BinOpKind::Ne == cond_op.node {
|
|
|
|
(cond_left, cond_right)
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if SpanlessEq::new(cx).eq_expr(cond_right, target) {
|
|
|
|
if BinOpKind::Lt == cond_op.node || BinOpKind::Ne == cond_op.node {
|
|
|
|
(cond_right, cond_left)
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the variable in the condition statement is an integer
|
2020-07-17 03:47:04 -05:00
|
|
|
if !cx.typeck_results().expr_ty(cond_var).is_integral() {
|
2020-04-06 13:10:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:07:57 -05:00
|
|
|
// Get the variable name
|
|
|
|
let var_name = ares_path.segments[0].ident.name.as_str();
|
2021-04-22 04:31:13 -05:00
|
|
|
const INT_TYPES: [LangItem; 5] = [
|
|
|
|
LangItem::I8,
|
|
|
|
LangItem::I16,
|
|
|
|
LangItem::I32,
|
|
|
|
LangItem::I64,
|
|
|
|
LangItem::Isize
|
|
|
|
];
|
2020-04-06 13:10:41 -05:00
|
|
|
|
|
|
|
match cond_num_val.kind {
|
|
|
|
ExprKind::Lit(ref cond_lit) => {
|
|
|
|
// Check if the constant is zero
|
|
|
|
if let LitKind::Int(0, _) = cond_lit.node {
|
2020-07-17 03:47:04 -05:00
|
|
|
if cx.typeck_results().expr_ty(cond_left).is_signed() {
|
2020-04-06 13:10:41 -05:00
|
|
|
} else {
|
|
|
|
print_lint_and_sugg(cx, &var_name, expr);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2021-04-22 04:31:13 -05:00
|
|
|
ExprKind::Path(QPath::TypeRelative(_, name)) => {
|
|
|
|
if_chain! {
|
|
|
|
if name.ident.as_str() == "MIN";
|
|
|
|
if let Some(const_id) = cx.typeck_results().type_dependent_def_id(cond_num_val.hir_id);
|
|
|
|
if let Some(impl_id) = cx.tcx.impl_of_method(const_id);
|
|
|
|
let mut int_ids = INT_TYPES.iter().filter_map(|&ty| cx.tcx.lang_items().require(ty).ok());
|
|
|
|
if int_ids.any(|int_id| int_id == impl_id);
|
|
|
|
then {
|
|
|
|
print_lint_and_sugg(cx, &var_name, expr)
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 13:10:41 -05:00
|
|
|
},
|
2021-04-22 04:31:13 -05:00
|
|
|
ExprKind::Call(func, []) => {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Path(QPath::TypeRelative(_, name)) = func.kind;
|
|
|
|
if name.ident.as_str() == "min_value";
|
|
|
|
if let Some(func_id) = cx.typeck_results().type_dependent_def_id(func.hir_id);
|
|
|
|
if let Some(impl_id) = cx.tcx.impl_of_method(func_id);
|
|
|
|
let mut int_ids = INT_TYPES.iter().filter_map(|&ty| cx.tcx.lang_items().require(ty).ok());
|
|
|
|
if int_ids.any(|int_id| int_id == impl_id);
|
|
|
|
then {
|
|
|
|
print_lint_and_sugg(cx, &var_name, expr)
|
2020-04-06 13:10:41 -05:00
|
|
|
}
|
2021-04-22 04:31:13 -05:00
|
|
|
}
|
2020-04-06 13:10:41 -05:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
2020-04-06 13:07:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 13:10:41 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn subtracts_one<'a>(cx: &LateContext<'_>, expr: &Expr<'a>) -> Option<&'a Expr<'a>> {
|
2020-04-06 13:10:41 -05:00
|
|
|
match expr.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::AssignOp(ref op1, target, value) => {
|
2020-04-06 13:10:41 -05:00
|
|
|
if_chain! {
|
|
|
|
if BinOpKind::Sub == op1.node;
|
|
|
|
// Check if literal being subtracted is one
|
|
|
|
if let ExprKind::Lit(ref lit1) = value.kind;
|
|
|
|
if let LitKind::Int(1, _) = lit1.node;
|
|
|
|
then {
|
|
|
|
Some(target)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Assign(target, value, _) => {
|
2020-04-06 13:10:41 -05:00
|
|
|
if_chain! {
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Binary(ref op1, left1, right1) = value.kind;
|
2020-04-06 13:10:41 -05:00
|
|
|
if BinOpKind::Sub == op1.node;
|
|
|
|
|
|
|
|
if SpanlessEq::new(cx).eq_expr(left1, target);
|
|
|
|
|
|
|
|
if let ExprKind::Lit(ref lit1) = right1.kind;
|
|
|
|
if let LitKind::Int(1, _) = lit1.node;
|
|
|
|
then {
|
|
|
|
Some(target)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn print_lint_and_sugg(cx: &LateContext<'_>, var_name: &str, expr: &Expr<'_>) {
|
2020-04-06 13:10:41 -05:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
IMPLICIT_SATURATING_SUB,
|
|
|
|
expr.span,
|
2020-08-28 09:10:16 -05:00
|
|
|
"implicitly performing saturating subtraction",
|
2020-04-06 13:10:41 -05:00
|
|
|
"try",
|
2020-08-28 09:10:16 -05:00
|
|
|
format!("{} = {}.saturating_sub({});", var_name, var_name, '1'),
|
2020-04-06 13:10:41 -05:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|