2019-09-04 02:08:48 -05:00
|
|
|
use crate::utils::{match_qpath, snippet_with_applicability, span_lint_and_sugg};
|
|
|
|
use if_chain::if_chain;
|
2020-02-29 21:23:33 -06:00
|
|
|
use rustc_ast::ast;
|
2019-09-04 02:08:48 -05:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::LateContext;
|
2019-09-04 02:08:48 -05:00
|
|
|
use rustc_target::abi::LayoutOf;
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
pub fn lint(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[&[hir::Expr<'_>]], arith: &str) {
|
2019-09-04 02:08:48 -05:00
|
|
|
let unwrap_arg = &args[0][1];
|
|
|
|
let arith_lhs = &args[1][0];
|
|
|
|
let arith_rhs = &args[1][1];
|
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(arith_lhs);
|
2019-09-04 02:08:48 -05:00
|
|
|
if !ty.is_integral() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mm = if let Some(mm) = is_min_or_max(cx, unwrap_arg) {
|
|
|
|
mm
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ty.is_signed() {
|
2020-02-21 02:39:38 -06:00
|
|
|
use self::{
|
|
|
|
MinMax::{Max, Min},
|
|
|
|
Sign::{Neg, Pos},
|
|
|
|
};
|
2019-09-04 02:08:48 -05:00
|
|
|
|
|
|
|
let sign = if let Some(sign) = lit_sign(arith_rhs) {
|
|
|
|
sign
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
match (arith, sign, mm) {
|
|
|
|
("add", Pos, Max) | ("add", Neg, Min) | ("sub", Neg, Max) | ("sub", Pos, Min) => (),
|
|
|
|
// "mul" is omitted because lhs can be negative.
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
super::MANUAL_SATURATING_ARITHMETIC,
|
|
|
|
expr.span,
|
|
|
|
"manual saturating arithmetic",
|
|
|
|
&format!("try using `saturating_{}`", arith),
|
|
|
|
format!(
|
|
|
|
"{}.saturating_{}({})",
|
|
|
|
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability),
|
|
|
|
arith,
|
|
|
|
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability),
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
match (mm, arith) {
|
2020-06-09 09:36:01 -05:00
|
|
|
(MinMax::Max, "add" | "mul") | (MinMax::Min, "sub") => (),
|
2019-09-04 02:08:48 -05:00
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
super::MANUAL_SATURATING_ARITHMETIC,
|
|
|
|
expr.span,
|
|
|
|
"manual saturating arithmetic",
|
|
|
|
&format!("try using `saturating_{}`", arith),
|
|
|
|
format!(
|
|
|
|
"{}.saturating_{}({})",
|
|
|
|
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability),
|
|
|
|
arith,
|
|
|
|
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability),
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
enum MinMax {
|
|
|
|
Min,
|
|
|
|
Max,
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn is_min_or_max<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) -> Option<MinMax> {
|
2019-09-04 02:08:48 -05:00
|
|
|
// `T::max_value()` `T::min_value()` inherent methods
|
|
|
|
if_chain! {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let hir::ExprKind::Call(func, args) = &expr.kind;
|
2019-09-04 02:08:48 -05:00
|
|
|
if args.is_empty();
|
2020-12-06 08:01:03 -06:00
|
|
|
if let hir::ExprKind::Path(hir::QPath::TypeRelative(_, segment)) = &func.kind;
|
2019-09-04 02:08:48 -05:00
|
|
|
then {
|
|
|
|
match &*segment.ident.as_str() {
|
|
|
|
"max_value" => return Some(MinMax::Max),
|
|
|
|
"min_value" => return Some(MinMax::Min),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2019-09-04 02:08:48 -05:00
|
|
|
let ty_str = ty.to_string();
|
|
|
|
|
|
|
|
// `std::T::MAX` `std::T::MIN` constants
|
2019-09-27 10:16:06 -05:00
|
|
|
if let hir::ExprKind::Path(path) = &expr.kind {
|
2019-09-04 02:08:48 -05:00
|
|
|
if match_qpath(path, &["core", &ty_str, "MAX"][..]) {
|
|
|
|
return Some(MinMax::Max);
|
|
|
|
}
|
|
|
|
|
|
|
|
if match_qpath(path, &["core", &ty_str, "MIN"][..]) {
|
|
|
|
return Some(MinMax::Min);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Literals
|
|
|
|
let bits = cx.layout_of(ty).unwrap().size.bits();
|
|
|
|
let (minval, maxval): (u128, u128) = if ty.is_signed() {
|
|
|
|
let minval = 1 << (bits - 1);
|
|
|
|
let mut maxval = !(1 << (bits - 1));
|
|
|
|
if bits != 128 {
|
|
|
|
maxval &= (1 << bits) - 1;
|
|
|
|
}
|
|
|
|
(minval, maxval)
|
|
|
|
} else {
|
|
|
|
(0, if bits == 128 { !0 } else { (1 << bits) - 1 })
|
|
|
|
};
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
let check_lit = |expr: &hir::Expr<'_>, check_min: bool| {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let hir::ExprKind::Lit(lit) = &expr.kind {
|
2019-09-04 02:08:48 -05:00
|
|
|
if let ast::LitKind::Int(value, _) = lit.node {
|
|
|
|
if value == maxval {
|
|
|
|
return Some(MinMax::Max);
|
|
|
|
}
|
|
|
|
|
|
|
|
if check_min && value == minval {
|
|
|
|
return Some(MinMax::Min);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let r @ Some(_) = check_lit(expr, !ty.is_signed()) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ty.is_signed() {
|
2020-01-06 10:39:50 -06:00
|
|
|
if let hir::ExprKind::Unary(hir::UnOp::UnNeg, val) = &expr.kind {
|
2019-09-04 02:08:48 -05:00
|
|
|
return check_lit(val, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
enum Sign {
|
|
|
|
Pos,
|
|
|
|
Neg,
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
fn lit_sign(expr: &hir::Expr<'_>) -> Option<Sign> {
|
2020-01-06 10:39:50 -06:00
|
|
|
if let hir::ExprKind::Unary(hir::UnOp::UnNeg, inner) = &expr.kind {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let hir::ExprKind::Lit(..) = &inner.kind {
|
2019-09-04 02:08:48 -05:00
|
|
|
return Some(Sign::Neg);
|
|
|
|
}
|
2019-09-27 10:16:06 -05:00
|
|
|
} else if let hir::ExprKind::Lit(..) = &expr.kind {
|
2019-09-04 02:08:48 -05:00
|
|
|
return Some(Sign::Pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|