|
| 1 | +use crate::utils::{match_qpath, snippet_with_applicability, span_lint_and_sugg}; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc::hir; |
| 4 | +use rustc::lint::LateContext; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_target::abi::LayoutOf; |
| 7 | +use syntax::ast; |
| 8 | + |
| 9 | +pub fn lint(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[&[hir::Expr]], arith: &str) { |
| 10 | + let unwrap_arg = &args[0][1]; |
| 11 | + let arith_lhs = &args[1][0]; |
| 12 | + let arith_rhs = &args[1][1]; |
| 13 | + |
| 14 | + let ty = cx.tables.expr_ty(arith_lhs); |
| 15 | + if !ty.is_integral() { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + let mm = if let Some(mm) = is_min_or_max(cx, unwrap_arg) { |
| 20 | + mm |
| 21 | + } else { |
| 22 | + return; |
| 23 | + }; |
| 24 | + |
| 25 | + if ty.is_signed() { |
| 26 | + use self::{MinMax::*, Sign::*}; |
| 27 | + |
| 28 | + let sign = if let Some(sign) = lit_sign(arith_rhs) { |
| 29 | + sign |
| 30 | + } else { |
| 31 | + return; |
| 32 | + }; |
| 33 | + |
| 34 | + match (arith, sign, mm) { |
| 35 | + ("add", Pos, Max) | ("add", Neg, Min) | ("sub", Neg, Max) | ("sub", Pos, Min) => (), |
| 36 | + // "mul" is omitted because lhs can be negative. |
| 37 | + _ => return, |
| 38 | + } |
| 39 | + |
| 40 | + let mut applicability = Applicability::MachineApplicable; |
| 41 | + span_lint_and_sugg( |
| 42 | + cx, |
| 43 | + super::MANUAL_SATURATING_ARITHMETIC, |
| 44 | + expr.span, |
| 45 | + "manual saturating arithmetic", |
| 46 | + &format!("try using `saturating_{}`", arith), |
| 47 | + format!( |
| 48 | + "{}.saturating_{}({})", |
| 49 | + snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability), |
| 50 | + arith, |
| 51 | + snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability), |
| 52 | + ), |
| 53 | + applicability, |
| 54 | + ); |
| 55 | + } else { |
| 56 | + match (mm, arith) { |
| 57 | + (MinMax::Max, "add") | (MinMax::Max, "mul") | (MinMax::Min, "sub") => (), |
| 58 | + _ => return, |
| 59 | + } |
| 60 | + |
| 61 | + let mut applicability = Applicability::MachineApplicable; |
| 62 | + span_lint_and_sugg( |
| 63 | + cx, |
| 64 | + super::MANUAL_SATURATING_ARITHMETIC, |
| 65 | + expr.span, |
| 66 | + "manual saturating arithmetic", |
| 67 | + &format!("try using `saturating_{}`", arith), |
| 68 | + format!( |
| 69 | + "{}.saturating_{}({})", |
| 70 | + snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability), |
| 71 | + arith, |
| 72 | + snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability), |
| 73 | + ), |
| 74 | + applicability, |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(PartialEq, Eq)] |
| 80 | +enum MinMax { |
| 81 | + Min, |
| 82 | + Max, |
| 83 | +} |
| 84 | + |
| 85 | +fn is_min_or_max<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &hir::Expr) -> Option<MinMax> { |
| 86 | + // `T::max_value()` `T::min_value()` inherent methods |
| 87 | + if_chain! { |
| 88 | + if let hir::ExprKind::Call(func, args) = &expr.node; |
| 89 | + if args.is_empty(); |
| 90 | + if let hir::ExprKind::Path(path) = &func.node; |
| 91 | + if let hir::QPath::TypeRelative(_, segment) = path; |
| 92 | + then { |
| 93 | + match &*segment.ident.as_str() { |
| 94 | + "max_value" => return Some(MinMax::Max), |
| 95 | + "min_value" => return Some(MinMax::Min), |
| 96 | + _ => {} |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + let ty = cx.tables.expr_ty(expr); |
| 102 | + let ty_str = ty.to_string(); |
| 103 | + |
| 104 | + // `std::T::MAX` `std::T::MIN` constants |
| 105 | + if let hir::ExprKind::Path(path) = &expr.node { |
| 106 | + if match_qpath(path, &["core", &ty_str, "MAX"][..]) { |
| 107 | + return Some(MinMax::Max); |
| 108 | + } |
| 109 | + |
| 110 | + if match_qpath(path, &["core", &ty_str, "MIN"][..]) { |
| 111 | + return Some(MinMax::Min); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Literals |
| 116 | + let bits = cx.layout_of(ty).unwrap().size.bits(); |
| 117 | + let (minval, maxval): (u128, u128) = if ty.is_signed() { |
| 118 | + let minval = 1 << (bits - 1); |
| 119 | + let mut maxval = !(1 << (bits - 1)); |
| 120 | + if bits != 128 { |
| 121 | + maxval &= (1 << bits) - 1; |
| 122 | + } |
| 123 | + (minval, maxval) |
| 124 | + } else { |
| 125 | + (0, if bits == 128 { !0 } else { (1 << bits) - 1 }) |
| 126 | + }; |
| 127 | + |
| 128 | + let check_lit = |expr: &hir::Expr, check_min: bool| { |
| 129 | + if let hir::ExprKind::Lit(lit) = &expr.node { |
| 130 | + if let ast::LitKind::Int(value, _) = lit.node { |
| 131 | + if value == maxval { |
| 132 | + return Some(MinMax::Max); |
| 133 | + } |
| 134 | + |
| 135 | + if check_min && value == minval { |
| 136 | + return Some(MinMax::Min); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + None |
| 142 | + }; |
| 143 | + |
| 144 | + if let r @ Some(_) = check_lit(expr, !ty.is_signed()) { |
| 145 | + return r; |
| 146 | + } |
| 147 | + |
| 148 | + if ty.is_signed() { |
| 149 | + if let hir::ExprKind::Unary(hir::UnNeg, val) = &expr.node { |
| 150 | + return check_lit(val, true); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + None |
| 155 | +} |
| 156 | + |
| 157 | +#[derive(PartialEq, Eq)] |
| 158 | +enum Sign { |
| 159 | + Pos, |
| 160 | + Neg, |
| 161 | +} |
| 162 | + |
| 163 | +fn lit_sign(expr: &hir::Expr) -> Option<Sign> { |
| 164 | + if let hir::ExprKind::Unary(hir::UnNeg, inner) = &expr.node { |
| 165 | + if let hir::ExprKind::Lit(..) = &inner.node { |
| 166 | + return Some(Sign::Neg); |
| 167 | + } |
| 168 | + } else if let hir::ExprKind::Lit(..) = &expr.node { |
| 169 | + return Some(Sign::Pos); |
| 170 | + } |
| 171 | + |
| 172 | + None |
| 173 | +} |
0 commit comments