Merge pull request #328 from Manishearth/falsepositives

less false positives for approx_const and float_cmp
This commit is contained in:
Manish Goregaokar 2015-09-11 19:31:36 +05:30
commit b86ebad0e7
2 changed files with 11 additions and 14 deletions

View File

@ -1,8 +1,6 @@
use rustc::lint::*;
use rustc_front::hir::*;
use syntax::codemap::Span;
use std::f64::consts as f64;
use utils::span_lint;
declare_lint! {
@ -31,29 +29,28 @@ impl LintPass for ApproxConstant {
fn check_expr(&mut self, cx: &Context, e: &Expr) {
if let &ExprLit(ref lit) = &e.node {
check_lit(cx, lit, e.span);
check_lit(cx, lit, e);
}
}
}
fn check_lit(cx: &Context, lit: &Lit, span: Span) {
fn check_lit(cx: &Context, lit: &Lit, e: &Expr) {
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"),
LitFloat(ref str, TyF32) => check_known_consts(cx, e, str, "f32"),
LitFloat(ref str, TyF64) => check_known_consts(cx, e, str, "f64"),
LitFloatUnsuffixed(ref str) =>
check_known_consts(cx, span, str, "f{32, 64}"),
check_known_consts(cx, e, str, "f{32, 64}"),
_ => ()
}
}
fn check_known_consts(cx: &Context, span: Span, str: &str, module: &str) {
fn check_known_consts(cx: &Context, e: &Expr, str: &str, module: &str) {
if let Ok(value) = str.parse::<f64>() {
for &(constant, name) in KNOWN_CONSTS {
if within_epsilon(constant, value) {
span_lint(cx, APPROX_CONSTANT, span, &format!(
"approximate value of `{}::{}` found. \
Consider using it directly", module, &name));
}
if !within_epsilon(constant, value) { continue; }
span_lint(cx, APPROX_CONSTANT, e.span, &format!(
"approximate value of `{}::{}` found. \
Consider using it directly", module, &name));
}
}
}

View File

@ -93,7 +93,7 @@ impl LintPass for FloatCmp {
return;
}
if let Some(name) = get_item_name(cx, expr) {
if name == "eq" || name == "ne" ||
if name == "eq" || name == "ne" || name == "is_nan" ||
name.as_str().starts_with("eq_") ||
name.as_str().ends_with("_eq") {
return;