Skip to content

Commit 237309b

Browse files
committed
Lint logbase for arbitrary base
1 parent c8a640c commit 237309b

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,17 @@ fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
573573
}
574574
}
575575

576-
fn are_same_base_logs(expr_a: &Expr<'_>, expr_b: &Expr<'_>) -> bool {
576+
fn are_same_base_logs(cx: &LateContext<'_, '_>, expr_a: &Expr<'_>, expr_b: &Expr<'_>) -> bool {
577577
if_chain! {
578-
if let ExprKind::MethodCall(PathSegment { ident: method_name_a, .. }, _, _) = expr_a.kind;
579-
if let ExprKind::MethodCall(PathSegment { ident: method_name_b, .. }, _, _) = expr_b.kind;
578+
if let ExprKind::MethodCall(PathSegment { ident: method_name_a, .. }, _, ref args_a) = expr_a.kind;
579+
if let ExprKind::MethodCall(PathSegment { ident: method_name_b, .. }, _, ref args_b) = expr_b.kind;
580580
then {
581-
return method_name_a.as_str() == method_name_b.as_str() && ["ln", "log2", "log10"].contains(&&*method_name_a.as_str());
581+
return method_name_a.as_str() == method_name_b.as_str() &&
582+
args_a.len() == args_b.len() &&
583+
(
584+
["ln", "log2", "log10"].contains(&&*method_name_a.as_str()) ||
585+
method_name_a.as_str() == "log" && args_a.len() == 2 && are_exprs_equal(cx, &args_a[1], &args_b[1])
586+
);
582587
}
583588
}
584589

@@ -595,7 +600,7 @@ fn check_logbase(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
595600
lhs,
596601
rhs,
597602
) = &expr.kind;
598-
if are_same_base_logs(lhs, rhs);
603+
if are_same_base_logs(cx, lhs, rhs);
599604
if let ExprKind::MethodCall(_, _, ref largs) = lhs.kind;
600605
if let ExprKind::MethodCall(_, _, ref rargs) = rhs.kind;
601606
then {

tests/ui/floating_point_logbase.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ fn main() {
77
let _ = x.log(y);
88
let _ = x.log(y);
99
let _ = x.log(y);
10+
let _ = x.log(y);
1011
// Cases where the lint shouldn't be applied
1112
let _ = x.ln() / y.powf(3.2);
1213
let _ = x.powf(3.2) / y.powf(3.2);
1314
let _ = x.powf(3.2) / y.ln();
15+
let _ = x.log(5f32) / y.log(7f32);
1416
}

tests/ui/floating_point_logbase.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ fn main() {
77
let _ = x.ln() / y.ln();
88
let _ = x.log2() / y.log2();
99
let _ = x.log10() / y.log10();
10+
let _ = x.log(5f32) / y.log(5f32);
1011
// Cases where the lint shouldn't be applied
1112
let _ = x.ln() / y.powf(3.2);
1213
let _ = x.powf(3.2) / y.powf(3.2);
1314
let _ = x.powf(3.2) / y.ln();
15+
let _ = x.log(5f32) / y.log(7f32);
1416
}

tests/ui/floating_point_logbase.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ error: division of logarithms can be calculated more efficiently and accurately
1818
LL | let _ = x.log10() / y.log10();
1919
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
2020

21-
error: aborting due to 3 previous errors
21+
error: division of logarithms can be calculated more efficiently and accurately
22+
--> $DIR/floating_point_logbase.rs:10:13
23+
|
24+
LL | let _ = x.log(5f32) / y.log(5f32);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
26+
27+
error: aborting due to 4 previous errors
2228

0 commit comments

Comments
 (0)