Skip to content

Commit bc03f46

Browse files
Remove lint for logarithm division identity
1 parent fd2506b commit bc03f46

File tree

3 files changed

+4
-182
lines changed

3 files changed

+4
-182
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ use crate::consts::{
44
};
55
use crate::utils::*;
66
use if_chain::if_chain;
7-
use rustc::declare_lint_pass;
8-
use rustc::hir::*;
9-
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7+
use rustc_hir::*;
8+
use rustc_lint::{LateContext, LateLintPass};
109
use rustc::ty;
1110
use rustc_errors::Applicability;
12-
use rustc_session::declare_tool_lint;
11+
use rustc_session::{declare_lint_pass, declare_tool_lint};
1312
use std::f32::consts as f32_consts;
1413
use std::f64::consts as f64_consts;
1514
use sugg::Sugg;
@@ -278,84 +277,6 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
278277
}
279278
}
280279

281-
// Checks whether two expressions evaluate to the same value
282-
fn are_exprs_equivalent(cx: &LateContext<'_, '_>, left: &Expr, right: &Expr) -> bool {
283-
// Checks whether the values are constant and equal
284-
if_chain! {
285-
if let Some((left_value, _)) = constant(cx, cx.tables, left);
286-
if let Some((right_value, _)) = constant(cx, cx.tables, right);
287-
if left_value == right_value;
288-
then {
289-
return true;
290-
}
291-
}
292-
293-
// Checks whether the expressions resolve to the same variable
294-
if_chain! {
295-
if let ExprKind::Path(ref left_qpath) = left.kind;
296-
if let QPath::Resolved(_, ref left_path) = *left_qpath;
297-
if left_path.segments.len() == 1;
298-
if let def::Res::Local(left_local_id) = qpath_res(cx, left_qpath, left.hir_id);
299-
if let ExprKind::Path(ref right_qpath) = right.kind;
300-
if let QPath::Resolved(_, ref right_path) = *right_qpath;
301-
if right_path.segments.len() == 1;
302-
if let def::Res::Local(right_local_id) = qpath_res(cx, right_qpath, right.hir_id);
303-
if left_local_id == right_local_id;
304-
then {
305-
return true;
306-
}
307-
}
308-
309-
false
310-
}
311-
312-
fn check_log_division(cx: &LateContext<'_, '_>, expr: &Expr) {
313-
let log_methods = ["log", "log2", "log10", "ln"];
314-
315-
if_chain! {
316-
if let ExprKind::Binary(op, ref lhs, ref rhs) = expr.kind;
317-
if op.node == BinOpKind::Div;
318-
if cx.tables.expr_ty(lhs).is_floating_point();
319-
if let ExprKind::MethodCall(left_path, _, left_args) = &lhs.kind;
320-
if cx.tables.expr_ty(&left_args[0]).is_floating_point();
321-
if let ExprKind::MethodCall(right_path, _, right_args) = &rhs.kind;
322-
if cx.tables.expr_ty(&right_args[0]).is_floating_point();
323-
let left_method = left_path.ident.name.as_str();
324-
if left_method == right_path.ident.name.as_str();
325-
if log_methods.iter().any(|&method| left_method == method);
326-
then {
327-
let left_recv = &left_args[0];
328-
let right_recv = &right_args[0];
329-
330-
// Return early when bases are not equal
331-
if left_method == "log" && !are_exprs_equivalent(cx, &left_args[1], &right_args[1]) {
332-
return;
333-
}
334-
335-
// Reduce the expression further for bases 2, 10 and e
336-
let suggestion = if let Some(method) = get_specialized_log_method(cx, right_recv) {
337-
format!("{}.{}()", Sugg::hir(cx, left_recv, ".."), method)
338-
} else {
339-
format!(
340-
"{}.log({})",
341-
Sugg::hir(cx, left_recv, ".."),
342-
Sugg::hir(cx, right_recv, "..")
343-
)
344-
};
345-
346-
span_lint_and_sugg(
347-
cx,
348-
FLOATING_POINT_IMPROVEMENTS,
349-
expr.span,
350-
"x.log(b) / y.log(b) can be reduced to x.log(y)",
351-
"consider using",
352-
suggestion,
353-
Applicability::MachineApplicable,
354-
);
355-
}
356-
}
357-
}
358-
359280
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
360281
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
361282
if let ExprKind::MethodCall(ref path, _, args) = &expr.kind {
@@ -371,7 +292,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
371292
}
372293
} else {
373294
check_expm1(cx, expr);
374-
check_log_division(cx, expr);
375295
}
376296
}
377297
}

tests/ui/floating_point_log.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,4 @@ fn check_ln1p() {
5454
let _ = (1.0 + x - 2.0).ln();
5555
}
5656

57-
fn check_log_division() {
58-
let x = 3f32;
59-
let y = 2f32;
60-
let b = 4f32;
61-
62-
let _ = x.log2() / y.log2();
63-
let _ = x.log10() / y.log10();
64-
let _ = x.ln() / y.ln();
65-
let _ = x.log(4.0) / y.log(4.0);
66-
let _ = x.log(b) / y.log(b);
67-
let _ = x.log(b) / y.log(x);
68-
let _ = x.log(b) / 2f32.log(b);
69-
70-
let x = 3f64;
71-
let y = 2f64;
72-
let b = 4f64;
73-
74-
let _ = x.log2() / y.log2();
75-
let _ = x.log10() / y.log10();
76-
let _ = x.ln() / y.ln();
77-
let _ = x.log(4.0) / y.log(4.0);
78-
let _ = x.log(b) / y.log(b);
79-
let _ = x.log(b) / y.log(x);
80-
let _ = x.log(b) / 2f64.log(b);
81-
}
82-
8357
fn main() {}

tests/ui/floating_point_log.stderr

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -168,77 +168,5 @@ error: ln(1 + x) can be computed more accurately
168168
LL | let _ = (x * 2.0 + 1.0).ln();
169169
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
170170

171-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
172-
--> $DIR/floating_point_log.rs:62:13
173-
|
174-
LL | let _ = x.log2() / y.log2();
175-
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
176-
177-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
178-
--> $DIR/floating_point_log.rs:63:13
179-
|
180-
LL | let _ = x.log10() / y.log10();
181-
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
182-
183-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
184-
--> $DIR/floating_point_log.rs:64:13
185-
|
186-
LL | let _ = x.ln() / y.ln();
187-
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
188-
189-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
190-
--> $DIR/floating_point_log.rs:65:13
191-
|
192-
LL | let _ = x.log(4.0) / y.log(4.0);
193-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
194-
195-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
196-
--> $DIR/floating_point_log.rs:66:13
197-
|
198-
LL | let _ = x.log(b) / y.log(b);
199-
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
200-
201-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
202-
--> $DIR/floating_point_log.rs:68:13
203-
|
204-
LL | let _ = x.log(b) / 2f32.log(b);
205-
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
206-
207-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
208-
--> $DIR/floating_point_log.rs:74:13
209-
|
210-
LL | let _ = x.log2() / y.log2();
211-
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
212-
213-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
214-
--> $DIR/floating_point_log.rs:75:13
215-
|
216-
LL | let _ = x.log10() / y.log10();
217-
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
218-
219-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
220-
--> $DIR/floating_point_log.rs:76:13
221-
|
222-
LL | let _ = x.ln() / y.ln();
223-
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
224-
225-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
226-
--> $DIR/floating_point_log.rs:77:13
227-
|
228-
LL | let _ = x.log(4.0) / y.log(4.0);
229-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
230-
231-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
232-
--> $DIR/floating_point_log.rs:78:13
233-
|
234-
LL | let _ = x.log(b) / y.log(b);
235-
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
236-
237-
error: x.log(b) / y.log(b) can be reduced to x.log(y)
238-
--> $DIR/floating_point_log.rs:80:13
239-
|
240-
LL | let _ = x.log(b) / 2f64.log(b);
241-
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
242-
243-
error: aborting due to 40 previous errors
171+
error: aborting due to 28 previous errors
244172

0 commit comments

Comments
 (0)