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::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,
"checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` \
@ -33,14 +33,14 @@ impl LintPass for LenZero {
}
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
if let &ExprBinary(Spanned{node: cmp, ..}, ref left, ref right) =
&expr.node {
match cmp {
BiEq => check_cmp(cx, expr.span, left, right, ""),
BiGt | BiNe => check_cmp(cx, expr.span, left, right, "!"),
_ => ()
}
if let ExprBinary(Spanned{node: cmp, ..}, ref left, ref right) =
expr.node {
match cmp {
BiEq => check_cmp(cx, expr.span, left, right, ""),
BiGt | BiNe => check_cmp(cx, expr.span, left, right, "!"),
_ => ()
}
}
}
}
@ -89,7 +89,11 @@ fn is_self_sig(sig: &MethodSig) -> bool {
false } else { sig.decl.inputs.len() == 1 }
}
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) {
(&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) =>
check_len_zero(cx, span, method, args, lit, op),