1
Fork 0

fix false positive len_zero in is_empty()

This commit is contained in:
Andre Bogus 2015-09-06 20:57:06 +02:00
parent 1569fd8c55
commit 87e6099ad7
3 changed files with 33 additions and 25 deletions

View file

@ -5,7 +5,7 @@ use syntax::codemap::{Span, Spanned};
use rustc::middle::def_id::DefId; use rustc::middle::def_id::DefId;
use rustc::middle::ty::{self, MethodTraitItemId, ImplOrTraitItemId}; use rustc::middle::ty::{self, MethodTraitItemId, ImplOrTraitItemId};
use utils::{span_lint, walk_ptrs_ty, snippet}; use utils::{snippet, span_lint, walk_ptrs_ty, with_item_name};
declare_lint!(pub LEN_ZERO, Warn, declare_lint!(pub LEN_ZERO, Warn,
"checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` \ "checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` \
@ -33,8 +33,8 @@ impl LintPass for LenZero {
} }
fn check_expr(&mut self, cx: &Context, expr: &Expr) { fn check_expr(&mut self, cx: &Context, expr: &Expr) {
if let &ExprBinary(Spanned{node: cmp, ..}, ref left, ref right) = if let ExprBinary(Spanned{node: cmp, ..}, ref left, ref right) =
&expr.node { expr.node {
match cmp { match cmp {
BiEq => check_cmp(cx, expr.span, left, right, ""), BiEq => check_cmp(cx, expr.span, left, right, ""),
BiGt | BiNe => check_cmp(cx, expr.span, left, right, "!"), BiGt | BiNe => check_cmp(cx, expr.span, left, right, "!"),
@ -90,6 +90,10 @@ fn is_self_sig(sig: &MethodSig) -> bool {
} }
fn check_cmp(cx: &Context, span: Span, left: &Expr, right: &Expr, op: &str) { fn check_cmp(cx: &Context, span: Span, left: &Expr, right: &Expr, op: &str) {
// check if we are in an is_empty() method
if let Some(true) = with_item_name(cx, left, |n| n == "is_empty") {
return;
}
match (&left.node, &right.node) { match (&left.node, &right.node) {
(&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) => (&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) =>
check_len_zero(cx, span, method, args, lit, op), check_len_zero(cx, span, method, args, lit, op),

View file

@ -5,10 +5,9 @@ use reexport::*;
use rustc_front::util::{is_comparison_binop, binop_to_string}; use rustc_front::util::{is_comparison_binop, binop_to_string};
use syntax::codemap::{Span, Spanned}; use syntax::codemap::{Span, Spanned};
use rustc_front::visit::FnKind; use rustc_front::visit::FnKind;
use rustc::front::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, with_item_name};
use consts::constant; use consts::constant;
declare_lint!(pub TOPLEVEL_REF_ARG, Warn, declare_lint!(pub TOPLEVEL_REF_ARG, Warn,
@ -93,20 +92,12 @@ 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); if let Some(true) = with_item_name(cx, expr, |name|
match cx.tcx.map.find(parent_id) { name == "eq" || name == "ne" ||
Some(NodeItem(&Item{ ref ident, .. })) | name.as_str().starts_with("eq_") ||
Some(NodeTraitItem(&TraitItem{ id: _, ref ident, .. })) | name.as_str().ends_with("_eq")) {
Some(NodeImplItem(&ImplItem{ id: _, ref ident, .. })) => {
let name = ident.name.as_str();
if &*name == "eq" || &*name == "ne" ||
name.starts_with("eq_") ||
name.ends_with("_eq") {
return; 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",

View file

@ -2,7 +2,7 @@ use rustc::lint::*;
use rustc_front::hir::*; use rustc_front::hir::*;
use reexport::*; use reexport::*;
use syntax::codemap::{ExpnInfo, Span, ExpnFormat}; use syntax::codemap::{ExpnInfo, Span, ExpnFormat};
use rustc::front::map::Node::NodeExpr; use rustc::front::map::Node::*;
use rustc::middle::def_id::DefId; use rustc::middle::def_id::DefId;
use rustc::middle::ty; use rustc::middle::ty;
use std::borrow::Cow; use std::borrow::Cow;
@ -100,6 +100,19 @@ pub fn match_path(path: &Path, segments: &[&str]) -> bool {
|(a, b)| &a.identifier.name == b) |(a, b)| &a.identifier.name == b)
} }
pub fn with_item_name<T, F>(cx: &Context, expr: &Expr, f: F) -> Option<T>
where F: FnOnce(Name) -> T {
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, .. })) => {
Some(f(ident.name))
},
_ => None,
}
}
/// convert a span to a code snippet if available, otherwise use default, e.g. /// convert a span to a code snippet if available, otherwise use default, e.g.
/// `snippet(cx, expr.span, "..")` /// `snippet(cx, expr.span, "..")`
pub fn snippet<'a>(cx: &Context, span: Span, default: &'a str) -> Cow<'a, str> { pub fn snippet<'a>(cx: &Context, span: Span, default: &'a str) -> Cow<'a, str> {