Merge pull request #706 from mcarton/and_then

`span_suggestion` and macro checks
This commit is contained in:
llogiq 2016-02-24 21:05:37 +01:00
commit e3d9802072
4 changed files with 64 additions and 22 deletions

View file

@ -8,7 +8,7 @@ use rustc::middle::ty::{self, MethodTraitItemId, ImplOrTraitItemId};
use syntax::ast::{Lit, LitKind};
use utils::{get_item_name, snippet, span_lint, walk_ptrs_ty};
use utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_then, walk_ptrs_ty};
/// **What it does:** This lint checks for getting the length of something via `.len()` just to compare to zero, and suggests using `.is_empty()` where applicable.
///
@ -51,6 +51,10 @@ impl LintPass for LenZero {
impl LateLintPass for LenZero {
fn check_item(&mut self, cx: &LateContext, item: &Item) {
if in_macro(cx, item.span) {
return;
}
match item.node {
ItemTrait(_, _, _, ref trait_items) => check_trait_items(cx, item, trait_items),
ItemImpl(_, _, _, None, _, ref impl_items) => check_impl_items(cx, item, impl_items),
@ -59,6 +63,10 @@ impl LateLintPass for LenZero {
}
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
if in_macro(cx, expr.span) {
return;
}
if let ExprBinary(Spanned{node: cmp, ..}, ref left, ref right) = expr.node {
match cmp {
BiEq => check_cmp(cx, expr.span, left, right, ""),
@ -80,7 +88,6 @@ fn check_trait_items(cx: &LateContext, item: &Item, trait_items: &[TraitItem]) {
}
if !trait_items.iter().any(|i| is_named_self(i, "is_empty")) {
// span_lint(cx, LEN_WITHOUT_IS_EMPTY, item.span, &format!("trait {}", item.ident));
for i in trait_items {
if is_named_self(i, "len") {
span_lint(cx,
@ -151,12 +158,17 @@ fn check_cmp(cx: &LateContext, span: Span, left: &Expr, right: &Expr, op: &str)
fn check_len_zero(cx: &LateContext, span: Span, name: &Name, args: &[P<Expr>], lit: &Lit, op: &str) {
if let Spanned{node: LitKind::Int(0, _), ..} = *lit {
if name.as_str() == "len" && args.len() == 1 && has_is_empty(cx, &args[0]) {
span_lint(cx,
span_lint_and_then(cx,
LEN_ZERO,
span,
&format!("consider replacing the len comparison with `{}{}.is_empty()`",
"length comparison to zero",
|db| {
db.span_suggestion(span,
"consider using `is_empty`",
format!("{}{}.is_empty()",
op,
snippet(cx, args[0].span, "_")));
});
}
}
}

View file

@ -271,14 +271,17 @@ impl LateLintPass for LoopsPass {
} else {
expr_block(cx, &arms[0].body, Some(other_stuff.join("\n ")), "..")
};
span_help_and_lint(cx,
span_lint_and_then(cx,
WHILE_LET_LOOP,
expr.span,
"this loop could be written as a `while let` loop",
&format!("try\nwhile let {} = {} {}",
|db| {
let sug = format!("while let {} = {} {}",
snippet(cx, arms[0].pats[0].span, ".."),
snippet(cx, matchexpr.span, ".."),
loop_body));
loop_body);
db.span_suggestion(expr.span, "try", sug);
});
}
}
_ => (),

View file

@ -69,7 +69,10 @@ impl HasWrongIsEmpty {
#[deny(len_zero)]
fn main() {
let x = [1, 2];
if x.len() == 0 { //~ERROR consider replacing the len comparison
if x.len() == 0 {
//~^ERROR length comparison to zero
//~|HELP consider using `is_empty`
//~|SUGGESTION x.is_empty()
println!("This should not happen!");
}
@ -84,19 +87,31 @@ fn main() {
}
let hie = HasIsEmpty;
if hie.len() == 0 { //~ERROR consider replacing the len comparison
if hie.len() == 0 {
//~^ERROR length comparison to zero
//~|HELP consider using `is_empty`
//~|SUGGESTION hie.is_empty()
println!("Or this!");
}
if hie.len() != 0 { //~ERROR consider replacing the len comparison
if hie.len() != 0 {
//~^ERROR length comparison to zero
//~|HELP consider using `is_empty`
//~|SUGGESTION !hie.is_empty()
println!("Or this!");
}
if hie.len() > 0 { //~ERROR consider replacing the len comparison
if hie.len() > 0 {
//~^ERROR length comparison to zero
//~|HELP consider using `is_empty`
//~|SUGGESTION !hie.is_empty()
println!("Or this!");
}
assert!(!hie.is_empty());
let wie : &WithIsEmpty = &Wither;
if wie.len() == 0 { //~ERROR consider replacing the len comparison
if wie.len() == 0 {
//~^ERROR length comparison to zero
//~|HELP consider using `is_empty`
//~|SUGGESTION wie.is_empty()
println!("Or this!");
}
assert!(!wie.is_empty());

View file

@ -6,7 +6,10 @@
fn main() {
let y = Some(true);
loop { //~ERROR
loop {
//~^ERROR this loop could be written as a `while let` loop
//~|HELP try
//~|SUGGESTION while let Some(_x) = y {
if let Some(_x) = y {
let _v = 1;
} else {
@ -19,13 +22,19 @@ fn main() {
}
break;
}
loop { //~ERROR
loop {
//~^ERROR this loop could be written as a `while let` loop
//~|HELP try
//~|SUGGESTION while let Some(_x) = y {
match y {
Some(_x) => true,
None => break
};
}
loop { //~ERROR
loop {
//~^ERROR this loop could be written as a `while let` loop
//~|HELP try
//~|SUGGESTION while let Some(x) = y {
let x = match y {
Some(x) => x,
None => break
@ -33,7 +42,10 @@ fn main() {
let _x = x;
let _str = "foo";
}
loop { //~ERROR
loop {
//~^ERROR this loop could be written as a `while let` loop
//~|HELP try
//~|SUGGESTION while let Some(x) = y {
let x = match y {
Some(x) => x,
None => break,