|
| 1 | +use std::cmp::Ordering; |
| 2 | + |
| 3 | +use super::UNNECESSARY_MIN; |
| 4 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 5 | + |
| 6 | +use clippy_utils::consts::{constant, Constant}; |
| 7 | +use clippy_utils::source::snippet; |
| 8 | +use clippy_utils::{clip, int_bits, unsext}; |
| 9 | +use hir::Expr; |
| 10 | + |
| 11 | +use rustc_errors::Applicability; |
| 12 | +use rustc_hir as hir; |
| 13 | +use rustc_lint::LateContext; |
| 14 | + |
| 15 | +use rustc_middle::ty; |
| 16 | +use rustc_span::Span; |
| 17 | + |
| 18 | +pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) { |
| 19 | + if both_are_constant(cx, expr, recv, arg) { |
| 20 | + return; |
| 21 | + } |
| 22 | + one_extrema(cx, expr, recv, arg); |
| 23 | +} |
| 24 | +fn lint(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Span, other: Span) { |
| 25 | + let msg = format!( |
| 26 | + "`{}` is never greater than `{}` and has therefore no effect", |
| 27 | + snippet(cx, sugg, "Not yet implemented"), |
| 28 | + snippet(cx, other, "Not yet implemented") |
| 29 | + ); |
| 30 | + span_lint_and_sugg( |
| 31 | + cx, |
| 32 | + UNNECESSARY_MIN, |
| 33 | + expr.span, |
| 34 | + &msg, |
| 35 | + "try", |
| 36 | + snippet(cx, sugg, "Not yet implemented").to_string(), |
| 37 | + Applicability::MachineApplicable, |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +fn try_to_eval<'tcx>( |
| 42 | + cx: &LateContext<'tcx>, |
| 43 | + recv: &'tcx Expr<'_>, |
| 44 | + arg: &'tcx Expr<'_>, |
| 45 | +) -> (Option<Constant<'tcx>>, Option<Constant<'tcx>>) { |
| 46 | + ( |
| 47 | + (constant(cx, cx.typeck_results(), recv)), |
| 48 | + (constant(cx, cx.typeck_results(), arg)), |
| 49 | + ) |
| 50 | +} |
| 51 | +#[derive(Debug)] |
| 52 | +enum Extrema { |
| 53 | + Minimum, |
| 54 | + Maximum, |
| 55 | +} |
| 56 | +fn detect_extrema<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Extrema> { |
| 57 | + let ty = cx.typeck_results().expr_ty(expr); |
| 58 | + |
| 59 | + let cv = constant(cx, cx.typeck_results(), expr)?; |
| 60 | + |
| 61 | + match (ty.kind(), cv) { |
| 62 | + (&ty::Uint(_), Constant::Int(0)) => Some(Extrema::Minimum), |
| 63 | + (&ty::Int(ity), Constant::Int(i)) if i == unsext(cx.tcx, i128::MIN >> (128 - int_bits(cx.tcx, ity)), ity) => { |
| 64 | + Some(Extrema::Minimum) |
| 65 | + }, |
| 66 | + |
| 67 | + (&ty::Int(ity), Constant::Int(i)) if i == unsext(cx.tcx, i128::MAX >> (128 - int_bits(cx.tcx, ity)), ity) => { |
| 68 | + Some(Extrema::Maximum) |
| 69 | + }, |
| 70 | + (&ty::Uint(uty), Constant::Int(i)) if i == clip(cx.tcx, u128::MAX, uty) => Some(Extrema::Maximum), |
| 71 | + |
| 72 | + _ => None, |
| 73 | + } |
| 74 | +} |
| 75 | +fn both_are_constant<'tcx>( |
| 76 | + cx: &LateContext<'tcx>, |
| 77 | + expr: &'tcx Expr<'_>, |
| 78 | + recv: &'tcx Expr<'_>, |
| 79 | + arg: &'tcx Expr<'_>, |
| 80 | +) -> bool { |
| 81 | + let ty = cx.typeck_results().expr_ty(recv); |
| 82 | + if let (Some(left), Some(right)) = try_to_eval(cx, recv, arg) |
| 83 | + && let Some(ord) = Constant::partial_cmp(cx.tcx, ty, &left, &right) |
| 84 | + { |
| 85 | + let (sugg, other) = match ord { |
| 86 | + Ordering::Less => (recv.span, arg.span), |
| 87 | + Ordering::Equal | Ordering::Greater => (arg.span, recv.span), |
| 88 | + }; |
| 89 | + |
| 90 | + lint(cx, expr, sugg, other); |
| 91 | + return true; |
| 92 | + } |
| 93 | + false |
| 94 | +} |
| 95 | +fn one_extrema<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) -> bool { |
| 96 | + if let Some(extrema) = detect_extrema(cx, recv) { |
| 97 | + match extrema { |
| 98 | + Extrema::Minimum => lint(cx, expr, recv.span, arg.span), |
| 99 | + Extrema::Maximum => lint(cx, expr, arg.span, recv.span), |
| 100 | + } |
| 101 | + return true; |
| 102 | + } else if let Some(extrema) = detect_extrema(cx, arg) { |
| 103 | + match extrema { |
| 104 | + Extrema::Minimum => lint(cx, expr, arg.span, recv.span), |
| 105 | + Extrema::Maximum => lint(cx, expr, recv.span, arg.span), |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + false |
| 111 | +} |
0 commit comments