check item name for eq, fixes #268

This commit is contained in:
llogiq 2015-09-02 10:30:11 +02:00
parent 2086f97bbd
commit 0c7f05dd76
2 changed files with 35 additions and 2 deletions

View file

@ -4,6 +4,7 @@ use syntax::ast::*;
use syntax::ast_util::{is_comparison_binop, binop_to_string};
use syntax::codemap::{Span, Spanned};
use syntax::visit::FnKind;
use rustc::ast_map::Node::*;
use rustc::middle::ty;
use utils::{match_path, snippet, span_lint, walk_ptrs_ty};
@ -91,6 +92,17 @@ impl LintPass for FloatCmp {
false, |c| c.0.as_float().map_or(false, |f| f == 0.0)) {
return;
}
let parent_id = cx.tcx.map.get_parent(expr.id);
match cx.tcx.map.find(parent_id) {
Some(NodeItem(&Item{ ref ident, .. })) |
Some(NodeTraitItem(&TraitItem{ id: _, ref ident, .. })) |
Some(NodeImplItem(&ImplItem{ id: _, ref ident, .. })) => {
let name = ident.name.as_str();
if &*name == "eq" || name.starts_with("eq_") ||
name.ends_with("_eq") { return; }
},
_ => (),
}
span_lint(cx, FLOAT_CMP, expr.span, &format!(
"{}-comparison of f32 or f64 detected. Consider changing this to \
`abs({} - {}) < epsilon` for some suitable value of epsilon",

View file

@ -1,6 +1,9 @@
#![feature(plugin)]
#![plugin(clippy)]
#![deny(float_cmp)]
#![allow(unused)]
use std::ops::Add;
const ZERO : f32 = 0.0;
@ -10,8 +13,26 @@ fn twice<T>(x : T) -> T where T : Add<T, Output = T>, T : Copy {
x + x
}
#[deny(float_cmp)]
#[allow(unused)]
fn eq_fl(x: f32, y: f32) -> bool {
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
}
fn fl_eq(x: f32, y: f32) -> bool {
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
}
struct X { val: f32 }
impl PartialEq for X {
fn eq(&self, o: &X) -> bool {
if self.val.is_nan() {
o.val.is_nan()
} else {
self.val == o.val // no error, inside "eq" fn
}
}
}
fn main() {
ZERO == 0f32; //no error, comparison with zero is ok
ZERO == 0.0; //no error, comparison with zero is ok