diff --git a/src/len_zero.rs b/src/len_zero.rs index 222de03f0..f63e733d6 100644 --- a/src/len_zero.rs +++ b/src/len_zero.rs @@ -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], 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, - LEN_ZERO, - span, - &format!("consider replacing the len comparison with `{}{}.is_empty()`", - op, - snippet(cx, args[0].span, "_"))); + span_lint_and_then(cx, + LEN_ZERO, + span, + "length comparison to zero", + |db| { + db.span_suggestion(span, + "consider using `is_empty`", + format!("{}{}.is_empty()", + op, + snippet(cx, args[0].span, "_"))); + }); } } } diff --git a/src/loops.rs b/src/loops.rs index acfb6c150..602de0eeb 100644 --- a/src/loops.rs +++ b/src/loops.rs @@ -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 {} = {} {}", - snippet(cx, arms[0].pats[0].span, ".."), - snippet(cx, matchexpr.span, ".."), - loop_body)); + |db| { + let sug = format!("while let {} = {} {}", + snippet(cx, arms[0].pats[0].span, ".."), + snippet(cx, matchexpr.span, ".."), + loop_body); + db.span_suggestion(expr.span, "try", sug); + }); } } _ => (), diff --git a/tests/compile-fail/len_zero.rs b/tests/compile-fail/len_zero.rs index 626e5557f..9814a1c2d 100644 --- a/tests/compile-fail/len_zero.rs +++ b/tests/compile-fail/len_zero.rs @@ -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()); diff --git a/tests/compile-fail/while_loop.rs b/tests/compile-fail/while_loop.rs index ee8e4622e..bbb76cfbf 100644 --- a/tests/compile-fail/while_loop.rs +++ b/tests/compile-fail/while_loop.rs @@ -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,