Skip to content

Commit e51fcd0

Browse files
committed
Make pow_call_result_sign compile
1 parent 02214f2 commit e51fcd0

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

clippy_lints/src/casts/cast_sign_loss.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,9 @@ fn should_lint(cx: &LateContext<'_>, cast_op: &Expr<'_>, cast_from: Ty<'_>, cast
5959

6060
let (mut uncertain_count, mut negative_count) = (0, 0);
6161
// Peel off possible binary expressions, for example:
62-
// x * x * y => [x, x, y]
62+
// x * x / y => [x, x, y]
6363
// a % b => [a]
64-
let Some(exprs) = exprs_with_selected_binop_peeled(cast_op) else {
65-
// Assume cast sign lose if we cannot determine the sign of `cast_op`
66-
return true;
67-
};
64+
let exprs = exprs_with_selected_binop_peeled(cast_op);
6865
for expr in exprs {
6966
let ty = cx.typeck_results().expr_ty(expr);
7067
match expr_sign(cx, expr, ty) {
@@ -141,23 +138,24 @@ fn expr_sign(cx: &LateContext<'_>, expr: &Expr<'_>, ty: Ty<'_>) -> Sign {
141138
/// otherwise if the exponent is an odd number, the result is always negative.
142139
///
143140
/// If either value can't be evaluated, [`Sign::Uncertain`] will be returned.
144-
fn pow_call_result_sign(cx: &LateContext<'_>, caller: &Expr<'_>, power_of: &Expr<'_>) -> Sign {
145-
let caller_ty = cx.typeck_results().expr_ty(caller);
146-
let Some(caller_val) = get_const_int_eval(cx, caller, caller_ty) else {
141+
fn pow_call_result_sign(cx: &LateContext<'_>, base: &Expr<'_>, exponent: &Expr<'_>) -> Sign {
142+
let base_ty = cx.typeck_results().expr_ty(base);
143+
let Some(base_val) = get_const_int_eval(cx, base, base_ty) else {
147144
return Sign::Uncertain;
148-
}
149-
// Non-negative values raised to non-negative exponents are always non-negative, ignoring overflow.
145+
};
146+
// Non-negative bases raised to non-negative exponents are always non-negative, ignoring overflow.
150147
// (Rust's integer pow() function takes an unsigned exponent.)
151-
if caller_val >= 0 {
148+
if base_val >= 0 {
152149
return Sign::ZeroOrPositive;
153150
}
154151

155-
let Some(Constant::Int(n)) = constant(cx, cx.typeck_results(), power_of) else {
152+
let Some(Constant::Int(n)) = constant(cx, cx.typeck_results(), exponent) else {
156153
return Sign::Uncertain;
157-
}
154+
};
155+
158156
// A negative value raised to an even exponent is non-negative, and an odd exponent
159157
// is negative, ignoring overflow.
160-
if clip(cx.tcx, n, UintTy::U32) % 2 == 0 0 {
158+
if clip(cx.tcx, n, UintTy::U32) % 2 == 0 {
161159
return Sign::ZeroOrPositive;
162160
} else {
163161
return Sign::Negative;

0 commit comments

Comments
 (0)