mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-11 07:34:18 +00:00
Merge pull request #276 from Manishearth/float_cmp
check item name for eq, fixes #268
This commit is contained in:
commit
eb3b9b35da
2 changed files with 35 additions and 2 deletions
12
src/misc.rs
12
src/misc.rs
|
@ -4,6 +4,7 @@ use syntax::ast::*;
|
||||||
use syntax::ast_util::{is_comparison_binop, binop_to_string};
|
use syntax::ast_util::{is_comparison_binop, binop_to_string};
|
||||||
use syntax::codemap::{Span, Spanned};
|
use syntax::codemap::{Span, Spanned};
|
||||||
use syntax::visit::FnKind;
|
use syntax::visit::FnKind;
|
||||||
|
use rustc::ast_map::Node::*;
|
||||||
use rustc::middle::ty;
|
use rustc::middle::ty;
|
||||||
|
|
||||||
use utils::{match_path, snippet, span_lint, walk_ptrs_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)) {
|
false, |c| c.0.as_float().map_or(false, |f| f == 0.0)) {
|
||||||
return;
|
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!(
|
span_lint(cx, FLOAT_CMP, expr.span, &format!(
|
||||||
"{}-comparison of f32 or f64 detected. Consider changing this to \
|
"{}-comparison of f32 or f64 detected. Consider changing this to \
|
||||||
`abs({} - {}) < epsilon` for some suitable value of epsilon",
|
`abs({} - {}) < epsilon` for some suitable value of epsilon",
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
#![feature(plugin)]
|
#![feature(plugin)]
|
||||||
#![plugin(clippy)]
|
#![plugin(clippy)]
|
||||||
|
|
||||||
|
#![deny(float_cmp)]
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
|
||||||
const ZERO : f32 = 0.0;
|
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
|
x + x
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deny(float_cmp)]
|
fn eq_fl(x: f32, y: f32) -> bool {
|
||||||
#[allow(unused)]
|
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() {
|
fn main() {
|
||||||
ZERO == 0f32; //no error, comparison with zero is ok
|
ZERO == 0f32; //no error, comparison with zero is ok
|
||||||
ZERO == 0.0; //no error, comparison with zero is ok
|
ZERO == 0.0; //no error, comparison with zero is ok
|
||||||
|
|
Loading…
Reference in a new issue