Skip to content

Commit

Permalink
got rid of get_both_as_expr
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixMaetzler committed Dec 15, 2023
1 parent 7a32e98 commit 2d36617
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4207,7 +4207,7 @@ impl Methods {
Some(("bytes", recv2, [], _, _)) => bytes_count_to_len::check(cx, expr, recv, recv2),
_ => {},
},
("min", [arg]) => unnecessary_min::check(cx, expr, arg),
("min", [arg]) => unnecessary_min::check(cx, expr, recv, arg),
("drain", ..) => {
if let Node::Stmt(Stmt { hir_id: _, kind, .. }) = cx.tcx.hir().get_parent(expr.hir_id)
&& matches!(kind, StmtKind::Semi(_))
Expand Down
58 changes: 27 additions & 31 deletions clippy_lints/src/methods/unnecessary_min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use rustc_lint::LateContext;
use rustc_middle::ty::{self, IntTy};
use rustc_span::Span;

pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, _: &'tcx Expr<'_>) {
if both_are_constant(cx, expr) {
pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) {
if both_are_constant(cx, expr, recv, arg) {
return;
}
one_extrema(cx, expr);
one_extrema(cx, expr, recv, arg);
}
fn lint(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Span, other: Span) {
let msg = format!(
Expand All @@ -38,23 +38,16 @@ fn lint(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Span, other: Span) {
);
}

fn try_to_eval<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> (Option<Constant<'tcx>>, Option<Constant<'tcx>>) {
let (left, right) = get_both_as_expr(expr);
fn try_to_eval<'tcx>(
cx: &LateContext<'tcx>,
recv: &'tcx Expr<'_>,
arg: &'tcx Expr<'_>,
) -> (Option<Constant<'tcx>>, Option<Constant<'tcx>>) {
(
(constant(cx, cx.typeck_results(), left)),
(constant(cx, cx.typeck_results(), right)),
(constant(cx, cx.typeck_results(), recv)),
(constant(cx, cx.typeck_results(), arg)),
)
}
fn get_both_as_expr<'tcx>(expr: &'tcx Expr<'_>) -> (&'tcx Expr<'tcx>, &'tcx Expr<'tcx>) {
match expr.kind {
hir::ExprKind::MethodCall(_, left1, right1, _) => {
let left = left1;
let right = &right1[0];
(left, right)
},
_ => unreachable!("this function gets only called on methods"),
}
}
#[derive(Debug)]
enum Extrema {
Minimum,
Expand Down Expand Up @@ -113,38 +106,41 @@ impl From<(u128, &LateContext<'_>, IntTy)> for Sign {
}
}
}
fn both_are_constant<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
let ty = cx.typeck_results().expr_ty(expr);
if let (Some(left), Some(right)) = try_to_eval(cx, expr) {
fn both_are_constant<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx Expr<'_>,
recv: &'tcx Expr<'_>,
arg: &'tcx Expr<'_>,
) -> bool {
let ty = cx.typeck_results().expr_ty(recv);
if let (Some(left), Some(right)) = try_to_eval(cx, recv, arg) {
let ord = match (ty.kind(), left, right) {
(ty::Int(ty), Constant::Int(left), Constant::Int(right)) => cmp_for_signed(left, right, cx, *ty),
(ty::Uint(_), Constant::Int(left), Constant::Int(right)) => left.cmp(&right),
_ => return false,
};

let (sugg, other) = match ord {
Ordering::Less => (get_both_as_expr(expr).0.span, get_both_as_expr(expr).1.span),
Ordering::Equal | Ordering::Greater => (get_both_as_expr(expr).1.span, get_both_as_expr(expr).0.span),
Ordering::Less => (recv.span, arg.span),
Ordering::Equal | Ordering::Greater => (arg.span, recv.span),
};

lint(cx, expr, sugg, other);
return true;
}
false
}
fn one_extrema<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
//let ty = cx.typeck_results().expr_ty(expr);
let (left, right) = get_both_as_expr(expr);
if let Some(extrema) = detect_extrema(cx, left) {
fn one_extrema<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) -> bool {
if let Some(extrema) = detect_extrema(cx, recv) {
match extrema {
Extrema::Minimum => lint(cx, expr, left.span, right.span),
Extrema::Maximum => lint(cx, expr, right.span, left.span),
Extrema::Minimum => lint(cx, expr, recv.span, arg.span),
Extrema::Maximum => lint(cx, expr, arg.span, recv.span),
}
return true;
} else if let Some(extrema) = detect_extrema(cx, right) {
} else if let Some(extrema) = detect_extrema(cx, arg) {
match extrema {
Extrema::Minimum => lint(cx, expr, right.span, left.span),
Extrema::Maximum => lint(cx, expr, left.span, right.span),
Extrema::Minimum => lint(cx, expr, arg.span, recv.span),
Extrema::Maximum => lint(cx, expr, recv.span, arg.span),
}
return true;
}
Expand Down

0 comments on commit 2d36617

Please sign in to comment.