2015-05-04 10:01:34 +00:00
|
|
|
use rustc::lint::*;
|
2015-09-03 14:42:17 +00:00
|
|
|
use rustc_front::hir::*;
|
2015-05-04 10:01:34 +00:00
|
|
|
use std::f64::consts as f64;
|
2015-07-26 14:53:11 +00:00
|
|
|
use utils::span_lint;
|
2015-09-17 00:01:41 +00:00
|
|
|
use syntax::ast::Lit_::*;
|
|
|
|
use syntax::ast::Lit;
|
|
|
|
use syntax::ast::FloatTy::*;
|
2015-05-04 10:01:34 +00:00
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub APPROX_CONSTANT,
|
|
|
|
Warn,
|
2015-08-13 08:32:35 +00:00
|
|
|
"the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) \
|
|
|
|
is found; suggests to use the constant"
|
2015-05-04 10:01:34 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 18:22:20 +00:00
|
|
|
const KNOWN_CONSTS : &'static [(f64, &'static str)] = &[(f64::E, "E"), (f64::FRAC_1_PI, "FRAC_1_PI"),
|
|
|
|
(f64::FRAC_1_SQRT_2, "FRAC_1_SQRT_2"), (f64::FRAC_2_PI, "FRAC_2_PI"),
|
|
|
|
(f64::FRAC_2_SQRT_PI, "FRAC_2_SQRT_PI"), (f64::FRAC_PI_2, "FRAC_PI_2"), (f64::FRAC_PI_3, "FRAC_PI_3"),
|
|
|
|
(f64::FRAC_PI_4, "FRAC_PI_4"), (f64::FRAC_PI_6, "FRAC_PI_6"), (f64::FRAC_PI_8, "FRAC_PI_8"),
|
|
|
|
(f64::LN_10, "LN_10"), (f64::LN_2, "LN_2"), (f64::LOG10_E, "LOG10_E"), (f64::LOG2_E, "LOG2_E"),
|
|
|
|
(f64::PI, "PI"), (f64::SQRT_2, "SQRT_2")];
|
2015-05-04 10:01:34 +00:00
|
|
|
|
|
|
|
const EPSILON_DIVISOR : f64 = 8192f64; //TODO: test to find a good value
|
|
|
|
|
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct ApproxConstant;
|
|
|
|
|
|
|
|
impl LintPass for ApproxConstant {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(APPROX_CONSTANT)
|
|
|
|
}
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2015-05-04 10:01:34 +00:00
|
|
|
fn check_expr(&mut self, cx: &Context, e: &Expr) {
|
2015-08-11 18:22:20 +00:00
|
|
|
if let &ExprLit(ref lit) = &e.node {
|
2015-09-11 13:59:19 +00:00
|
|
|
check_lit(cx, lit, e);
|
2015-08-11 18:22:20 +00:00
|
|
|
}
|
2015-05-04 10:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 13:59:19 +00:00
|
|
|
fn check_lit(cx: &Context, lit: &Lit, e: &Expr) {
|
2015-08-21 18:44:48 +00:00
|
|
|
match lit.node {
|
2015-09-11 13:59:19 +00:00
|
|
|
LitFloat(ref str, TyF32) => check_known_consts(cx, e, str, "f32"),
|
|
|
|
LitFloat(ref str, TyF64) => check_known_consts(cx, e, str, "f64"),
|
2015-08-27 05:39:40 +00:00
|
|
|
LitFloatUnsuffixed(ref str) =>
|
2015-09-11 13:59:19 +00:00
|
|
|
check_known_consts(cx, e, str, "f{32, 64}"),
|
2015-08-11 18:22:20 +00:00
|
|
|
_ => ()
|
|
|
|
}
|
2015-05-04 10:01:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 13:59:19 +00:00
|
|
|
fn check_known_consts(cx: &Context, e: &Expr, str: &str, module: &str) {
|
2015-08-11 18:22:20 +00:00
|
|
|
if let Ok(value) = str.parse::<f64>() {
|
|
|
|
for &(constant, name) in KNOWN_CONSTS {
|
2015-09-11 13:59:19 +00:00
|
|
|
if !within_epsilon(constant, value) { continue; }
|
|
|
|
span_lint(cx, APPROX_CONSTANT, e.span, &format!(
|
|
|
|
"approximate value of `{}::{}` found. \
|
|
|
|
Consider using it directly", module, &name));
|
2015-08-11 18:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 10:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn within_epsilon(target: f64, value: f64) -> bool {
|
2015-08-27 05:39:40 +00:00
|
|
|
f64::abs(value - target) < f64::abs(if target > value {
|
|
|
|
target
|
2015-08-26 12:26:43 +00:00
|
|
|
} else { value }) / EPSILON_DIVISOR
|
2015-05-04 10:01:34 +00:00
|
|
|
}
|