Skip to content

Commit dca37f5

Browse files
Reduce cognitive complexity
1 parent 20319e8 commit dca37f5

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+36-27
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,38 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
234234
}
235235
}
236236

237+
// Expressions are considered equivalent if they are constant and evaluate to the
238+
// same value or if they refer to the same variable.
239+
fn are_exprs_equivalent(cx: &LateContext<'_, '_>, left: &Expr, right: &Expr) -> bool {
240+
// Checks whether the values are constant and equal
241+
if_chain! {
242+
if let Some((left_value, _)) = constant(cx, cx.tables, left);
243+
if let Some((right_value, _)) = constant(cx, cx.tables, right);
244+
if left_value == right_value;
245+
then {
246+
return true;
247+
}
248+
}
249+
250+
// Checks whether the values are the same variable
251+
if_chain! {
252+
if let ExprKind::Path(ref left_qpath) = left.kind;
253+
if let QPath::Resolved(_, ref left_path) = *left_qpath;
254+
if left_path.segments.len() == 1;
255+
if let def::Res::Local(left_local_id) = qpath_res(cx, left_qpath, left.hir_id);
256+
if let ExprKind::Path(ref right_qpath) = right.kind;
257+
if let QPath::Resolved(_, ref right_path) = *right_qpath;
258+
if right_path.segments.len() == 1;
259+
if let def::Res::Local(right_local_id) = qpath_res(cx, right_qpath, right.hir_id);
260+
if left_local_id == right_local_id;
261+
then {
262+
return true;
263+
}
264+
}
265+
266+
false
267+
}
268+
237269
fn check_log_division(cx: &LateContext<'_, '_>, expr: &Expr) {
238270
let log_methods = ["log", "log2", "log10", "ln"];
239271

@@ -250,32 +282,9 @@ fn check_log_division(cx: &LateContext<'_, '_>, expr: &Expr) {
250282
let left_recv = &left_args[0];
251283
let right_recv = &right_args[0];
252284

253-
if left_method == "log" {
254-
if_chain! {
255-
// Checks whether the bases are constant and equal
256-
if let Some((numerator_base, _)) = constant(cx, cx.tables, &left_args[1]);
257-
if let Some((denominator_base, _)) = constant(cx, cx.tables, &right_args[1]);
258-
if numerator_base == denominator_base;
259-
then { }
260-
else {
261-
if_chain! {
262-
// Checks whether the bases are the same variable
263-
if let ExprKind::Path(ref left_base_qpath) = &left_args[1].kind;
264-
if let QPath::Resolved(_, ref left_base_path) = *left_base_qpath;
265-
if left_base_path.segments.len() == 1;
266-
if let def::Res::Local(left_local_id) = qpath_res(cx, left_base_qpath, expr.hir_id);
267-
if let ExprKind::Path(ref right_base_qpath) = &right_args[1].kind;
268-
if let QPath::Resolved(_, ref right_base_path) = *right_base_qpath;
269-
if right_base_path.segments.len() == 1;
270-
if let def::Res::Local(right_local_id) = qpath_res(cx, right_base_qpath, expr.hir_id);
271-
if left_local_id == right_local_id;
272-
then { }
273-
else {
274-
return;
275-
}
276-
}
277-
}
278-
}
285+
// Return early when bases are not equal
286+
if left_method == "log" && !are_exprs_equivalent(cx, &left_args[1], &right_args[1]) {
287+
return;
279288
}
280289

281290
// Reduce the expression further for bases 2, 10 and e
@@ -293,7 +302,7 @@ fn check_log_division(cx: &LateContext<'_, '_>, expr: &Expr) {
293302
cx,
294303
FLOATING_POINT_IMPROVEMENTS,
295304
expr.span,
296-
"x.log(b) / y.log(b) can be expressed more succinctly",
305+
"x.log(b) / y.log(b) can be reduced to x.log(y)",
297306
"consider using",
298307
suggestion,
299308
Applicability::MachineApplicable,

tests/ui/floating_point_arithmetic.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -192,73 +192,73 @@ error: (e.pow(x) - 1) can be computed more accurately
192192
LL | let _ = x.exp() - 1.0 + 2.0;
193193
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
194194

195-
error: x.log(b) / y.log(b) can be expressed more succinctly
195+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
196196
--> $DIR/floating_point_arithmetic.rs:90:13
197197
|
198198
LL | let _ = x.log2() / y.log2();
199199
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
200200

201-
error: x.log(b) / y.log(b) can be expressed more succinctly
201+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
202202
--> $DIR/floating_point_arithmetic.rs:91:13
203203
|
204204
LL | let _ = x.log10() / y.log10();
205205
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
206206

207-
error: x.log(b) / y.log(b) can be expressed more succinctly
207+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
208208
--> $DIR/floating_point_arithmetic.rs:92:13
209209
|
210210
LL | let _ = x.ln() / y.ln();
211211
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
212212

213-
error: x.log(b) / y.log(b) can be expressed more succinctly
213+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
214214
--> $DIR/floating_point_arithmetic.rs:93:13
215215
|
216216
LL | let _ = x.log(4.0) / y.log(4.0);
217217
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
218218

219-
error: x.log(b) / y.log(b) can be expressed more succinctly
219+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
220220
--> $DIR/floating_point_arithmetic.rs:94:13
221221
|
222222
LL | let _ = x.log(b) / y.log(b);
223223
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
224224

225-
error: x.log(b) / y.log(b) can be expressed more succinctly
225+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
226226
--> $DIR/floating_point_arithmetic.rs:96:13
227227
|
228228
LL | let _ = x.log(b) / 2f32.log(b);
229229
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
230230

231-
error: x.log(b) / y.log(b) can be expressed more succinctly
231+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
232232
--> $DIR/floating_point_arithmetic.rs:102:13
233233
|
234234
LL | let _ = x.log2() / y.log2();
235235
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
236236

237-
error: x.log(b) / y.log(b) can be expressed more succinctly
237+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
238238
--> $DIR/floating_point_arithmetic.rs:103:13
239239
|
240240
LL | let _ = x.log10() / y.log10();
241241
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
242242

243-
error: x.log(b) / y.log(b) can be expressed more succinctly
243+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
244244
--> $DIR/floating_point_arithmetic.rs:104:13
245245
|
246246
LL | let _ = x.ln() / y.ln();
247247
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
248248

249-
error: x.log(b) / y.log(b) can be expressed more succinctly
249+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
250250
--> $DIR/floating_point_arithmetic.rs:105:13
251251
|
252252
LL | let _ = x.log(4.0) / y.log(4.0);
253253
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
254254

255-
error: x.log(b) / y.log(b) can be expressed more succinctly
255+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
256256
--> $DIR/floating_point_arithmetic.rs:106:13
257257
|
258258
LL | let _ = x.log(b) / y.log(b);
259259
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
260260

261-
error: x.log(b) / y.log(b) can be expressed more succinctly
261+
error: x.log(b) / y.log(b) can be reduced to x.log(y)
262262
--> $DIR/floating_point_arithmetic.rs:108:13
263263
|
264264
LL | let _ = x.log(b) / 2f64.log(b);

0 commit comments

Comments
 (0)