1
Fork 0

Use span_lint_and_sugg

This commit is contained in:
Seo Sanghyeon 2017-06-22 03:04:04 +09:00
parent 88101d5b78
commit 745233f3ab
12 changed files with 91 additions and 122 deletions

View file

@ -4,7 +4,7 @@ use rustc::ty;
use rustc::hir::*;
use syntax::ast::{Lit, LitKind, Name};
use syntax::codemap::{Span, Spanned};
use utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_then, walk_ptrs_ty};
use utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_sugg, walk_ptrs_ty};
/// **What it does:** Checks for getting the length of something via `.len()`
/// just to compare to zero, and suggests using `.is_empty()` where applicable.
@ -171,11 +171,9 @@ fn check_cmp(cx: &LateContext, span: Span, left: &Expr, right: &Expr, op: &str)
fn check_len_zero(cx: &LateContext, span: Span, name: Name, args: &[Expr], lit: &Lit, op: &str) {
if let Spanned { node: LitKind::Int(0, _), .. } = *lit {
if name == "len" && args.len() == 1 && has_is_empty(cx, &args[0]) {
span_lint_and_then(cx, LEN_ZERO, span, "length comparison to zero", |db| {
db.span_suggestion(span,
"using `is_empty` is more concise:",
format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")));
});
span_lint_and_sugg(cx, LEN_ZERO, span, "length comparison to zero",
"using `is_empty` is more concise:",
format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")));
}
}
}