Merge pull request #43 from Manishearth/approx_const

New lint: approx_const
This commit is contained in:
llogiq 2015-05-04 12:51:57 +02:00
commit 872628e99e
4 changed files with 124 additions and 1 deletions

View File

@ -15,6 +15,7 @@ Lints included in this crate:
- `bad_bit_mask`: Denies expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
- `needless_bool` : Warns on if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`
- `ptr_arg`: Warns on fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively
- `approx_constant`: Warns if the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) is found and suggests to use the constant
In your code, you may add `#![plugin(clippy)]` to use it (you may also need to include a `#![feature(plugin)]` line)

63
src/approx_const.rs Normal file
View File

@ -0,0 +1,63 @@
use rustc::plugin::Registry;
use rustc::lint::*;
use rustc::middle::const_eval::lookup_const_by_id;
use rustc::middle::def::*;
use syntax::ast::*;
use syntax::ast_util::{is_comparison_binop, binop_to_string};
use syntax::ptr::P;
use syntax::codemap::Span;
use std::f64::consts as f64;
declare_lint! {
pub APPROX_CONSTANT,
Warn,
"Warn if a user writes an approximate known constant in their code"
}
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")];
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)
}
fn check_expr(&mut self, cx: &Context, e: &Expr) {
if let &ExprLit(ref lit) = &e.node {
check_lit(cx, lit, e.span);
}
}
}
fn check_lit(cx: &Context, lit: &Lit, span: Span) {
match &lit.node {
&LitFloat(ref str, TyF32) => check_known_consts(cx, span, str, "f32"),
&LitFloat(ref str, TyF64) => check_known_consts(cx, span, str, "f64"),
&LitFloatUnsuffixed(ref str) => check_known_consts(cx, span, str, "f{32, 64}"),
_ => ()
}
}
fn check_known_consts(cx: &Context, span: Span, str: &str, module: &str) {
if let Ok(value) = str.parse::<f64>() {
for &(constant, name) in KNOWN_CONSTS {
if within_epsilon(constant, value) {
cx.span_lint(APPROX_CONSTANT, span, &format!(
"Approximate value of {}::{} found, consider using it directly.", module, &name));
}
}
}
}
fn within_epsilon(target: f64, value: f64) -> bool {
f64::abs(value - target) < f64::abs((if target > value { target } else { value })) / EPSILON_DIVISOR
}

View File

@ -20,6 +20,7 @@ pub mod eq_op;
pub mod bit_mask;
pub mod ptr_arg;
pub mod needless_bool;
pub mod approx_const;
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
@ -31,11 +32,13 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_lint_pass(box bit_mask::BitMask as LintPassObject);
reg.register_lint_pass(box ptr_arg::PtrArg as LintPassObject);
reg.register_lint_pass(box needless_bool::NeedlessBool as LintPassObject);
reg.register_lint_pass(box approx_const::ApproxConstant as LintPassObject);
reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST,
misc::SINGLE_MATCH, misc::STR_TO_STRING,
misc::TOPLEVEL_REF_ARG, eq_op::EQ_OP,
bit_mask::BAD_BIT_MASK, ptr_arg::PTR_ARG,
needless_bool::NEEDLESS_BOOL
needless_bool::NEEDLESS_BOOL,
approx_const::APPROX_CONSTANT
]);
}

View File

@ -0,0 +1,56 @@
#![feature(plugin)]
#![plugin(clippy)]
#[deny(approx_constant)]
#[allow(unused)]
fn main() {
let my_e = 2.7182; //~ERROR
let almost_e = 2.718; //~ERROR
let no_e = 2.71;
let my_1_frac_pi = 0.3183; //~ERROR
let no_1_frac_pi = 0.31;
let my_frac_1_sqrt_2 = 0.70710678; //~ERROR
let almost_frac_1_sqrt_2 = 0.70711; //~ERROR
let my_frac_1_sqrt_2 = 0.707;
let my_frac_2_pi = 0.63661977; //~ERROR
let no_frac_2_pi = 0.636;
let my_frac_2_sq_pi = 1.128379; //~ERROR
let no_frac_2_sq_pi = 1.128;
let my_frac_2_pi = 1.57079632679; //~ERROR
let no_frac_2_pi = 1.5705;
let my_frac_3_pi = 1.04719755119; //~ERROR
let no_frac_3_pi = 1.047;
let my_frac_4_pi = 0.785398163397; //~ERROR
let no_frac_4_pi = 0.785;
let my_frac_6_pi = 0.523598775598; //~ERROR
let no_frac_6_pi = 0.523;
let my_frac_8_pi = 0.3926990816987; //~ERROR
let no_frac_8_pi = 0.392;
let my_ln_10 = 2.302585092994046; //~ERROR
let no_ln_10 = 2.303;
let my_ln_2 = 0.6931471805599453; //~ERROR
let no_ln_2 = 0.693;
let my_log10_e = 0.43429448190325176; //~ERROR
let no_log10_e = 0.434;
let my_log2_e = 1.4426950408889634; //~ERROR
let no_log2_e = 1.442;
let my_pi = 3.1415; //~ERROR
let almost_pi = 3.141;
let my_sq2 = 1.4142; //~ERROR
let no_sq2 = 1.414;
}