also run rustfmt on clippy-lints

This commit is contained in:
Oliver Schneider 2016-12-20 18:21:30 +01:00
parent 4d0864b277
commit 4a4e1ea2c5
No known key found for this signature in database
GPG key ID: 56D6EEA0FC67AC46
72 changed files with 1017 additions and 1204 deletions

View file

@ -9,10 +9,10 @@ use rustc_const_eval::eval_const_expr_partial;
use std::borrow::Cow;
use std::fmt;
use syntax::codemap::Span;
use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, is_copy, match_path,
match_trait_method, match_type, method_chain_args, return_ty, same_tys, snippet,
span_lint, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth,
last_path_segment, single_segment_path, match_def_path};
use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, is_copy, match_path, match_trait_method,
match_type, method_chain_args, return_ty, same_tys, snippet, span_lint, span_lint_and_then,
span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, last_path_segment, single_segment_path,
match_def_path};
use utils::paths;
use utils::sugg;
@ -616,15 +616,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
lint_single_char_pattern(cx, expr, &args[pos]);
}
}
}
},
_ => (),
}
}
},
hir::ExprBinary(op, ref lhs, ref rhs) if op.node == hir::BiEq || op.node == hir::BiNe => {
if !lint_chars_next(cx, expr, lhs, rhs, op.node == hir::BiEq) {
lint_chars_next(cx, expr, rhs, lhs, op.node == hir::BiEq);
}
}
},
_ => (),
}
}
@ -708,7 +708,8 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
if ["default", "new"].contains(&path) {
let arg_ty = cx.tcx.tables().expr_ty(arg);
let default_trait_id = if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT) {
let default_trait_id = if let Some(default_trait_id) =
get_trait_def_id(cx, &paths::DEFAULT_TRAIT) {
default_trait_id
} else {
return false;
@ -716,13 +717,15 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
if implements_trait(cx, arg_ty, default_trait_id, Vec::new()) {
span_lint_and_then(cx,
OR_FUN_CALL,
span,
&format!("use of `{}` followed by a call to `{}`", name, path),
|db| {
db.span_suggestion(span, "try this",
format!("{}.unwrap_or_default()", snippet(cx, self_expr.span, "_")));
});
OR_FUN_CALL,
span,
&format!("use of `{}` followed by a call to `{}`", name, path),
|db| {
db.span_suggestion(span,
"try this",
format!("{}.unwrap_or_default()",
snippet(cx, self_expr.span, "_")));
});
return true;
}
}
@ -733,8 +736,8 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
}
/// Check for `*or(foo())`.
fn check_general_case(cx: &LateContext, name: &str, fun: &hir::Expr, self_expr: &hir::Expr, arg: &hir::Expr, or_has_args: bool,
span: Span) {
fn check_general_case(cx: &LateContext, name: &str, fun: &hir::Expr, self_expr: &hir::Expr, arg: &hir::Expr,
or_has_args: bool, span: Span) {
// don't lint for constant values
// FIXME: can we `expect` here instead of match?
if let Some(qualif) = cx.tcx.const_qualif_map.borrow().get(&arg.id) {
@ -743,18 +746,16 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
}
}
// (path, fn_has_argument, methods, suffix)
let know_types: &[(&[_], _, &[_], _)] = &[(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
(&paths::HASHMAP_ENTRY, false, &["or_insert"], "with"),
(&paths::OPTION,
false,
&["map_or", "ok_or", "or", "unwrap_or"],
"else"),
(&paths::RESULT, true, &["or", "unwrap_or"], "else")];
let know_types: &[(&[_], _, &[_], _)] =
&[(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
(&paths::HASHMAP_ENTRY, false, &["or_insert"], "with"),
(&paths::OPTION, false, &["map_or", "ok_or", "or", "unwrap_or"], "else"),
(&paths::RESULT, true, &["or", "unwrap_or"], "else")];
let self_ty = cx.tcx.tables().expr_ty(self_expr);
let (fn_has_arguments, poss, suffix) = if let Some(&(_, fn_has_arguments, poss, suffix)) =
know_types.iter().find(|&&i| match_type(cx, self_ty, i.0)) {
know_types.iter().find(|&&i| match_type(cx, self_ty, i.0)) {
(fn_has_arguments, poss, suffix)
} else {
return;
@ -770,7 +771,11 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
(false, true) => snippet(cx, fun.span, ".."),
};
span_lint_and_then(cx, OR_FUN_CALL, span, &format!("use of `{}` followed by a function call", name), |db| {
span_lint_and_then(cx,
OR_FUN_CALL,
span,
&format!("use of `{}` followed by a function call", name),
|db| {
db.span_suggestion(span,
"try this",
format!("{}.{}_{}({})", snippet(cx, self_expr.span, "_"), name, suffix, sugg));
@ -800,7 +805,9 @@ fn lint_clone_on_copy(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr, arg_t
"using `clone` on a double-reference; \
this will copy the reference instead of cloning the inner type",
|db| if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
db.span_suggestion(expr.span, "try dereferencing it", format!("({}).clone()", snip.deref()));
db.span_suggestion(expr.span,
"try dereferencing it",
format!("({}).clone()", snip.deref()));
});
return; // don't report clone_on_copy
}
@ -824,12 +831,14 @@ fn lint_clone_on_copy(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr, arg_t
fn lint_vec_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) {
let arg_ty = cx.tcx.tables().expr_ty(&args[1]);
if let Some(slice) = derefs_to_slice(cx, &args[1], arg_ty) {
span_lint_and_then(cx, EXTEND_FROM_SLICE, expr.span, "use of `extend` to extend a Vec by a slice", |db| {
span_lint_and_then(cx,
EXTEND_FROM_SLICE,
expr.span,
"use of `extend` to extend a Vec by a slice",
|db| {
db.span_suggestion(expr.span,
"try this",
format!("{}.extend_from_slice({})",
snippet(cx, args[0].span, "_"),
slice));
format!("{}.extend_from_slice({})", snippet(cx, args[0].span, "_"), slice));
});
}
}
@ -847,18 +856,14 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) {
return;
};
span_lint_and_then(
cx,
STRING_EXTEND_CHARS,
expr.span,
"calling `.extend(_.chars())`",
|db| {
db.span_suggestion(expr.span, "try this",
format!("{}.push_str({}{})",
snippet(cx, args[0].span, "_"),
ref_str,
snippet(cx, target.span, "_")));
});
span_lint_and_then(cx, STRING_EXTEND_CHARS, expr.span, "calling `.extend(_.chars())`", |db| {
db.span_suggestion(expr.span,
"try this",
format!("{}.push_str({}{})",
snippet(cx, args[0].span, "_"),
ref_str,
snippet(cx, target.span, "_")));
});
}
}
@ -888,28 +893,24 @@ fn lint_cstring_as_ptr(cx: &LateContext, expr: &hir::Expr, new: &hir::Expr, unwr
}}
}
fn lint_iter_nth(cx: &LateContext, expr: &hir::Expr, iter_args: &[hir::Expr], is_mut: bool){
let mut_str = if is_mut { "_mut" } else {""};
fn lint_iter_nth(cx: &LateContext, expr: &hir::Expr, iter_args: &[hir::Expr], is_mut: bool) {
let mut_str = if is_mut { "_mut" } else { "" };
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.tcx.tables().expr_ty(&iter_args[0])).is_some() {
"slice"
}
else if match_type(cx, cx.tcx.tables().expr_ty(&iter_args[0]), &paths::VEC) {
} else if match_type(cx, cx.tcx.tables().expr_ty(&iter_args[0]), &paths::VEC) {
"Vec"
}
else if match_type(cx, cx.tcx.tables().expr_ty(&iter_args[0]), &paths::VEC_DEQUE) {
} else if match_type(cx, cx.tcx.tables().expr_ty(&iter_args[0]), &paths::VEC_DEQUE) {
"VecDeque"
}
else {
} else {
return; // caller is not a type that we want to lint
};
span_lint(
cx,
ITER_NTH,
expr.span,
&format!("called `.iter{0}().nth()` on a {1}. Calling `.get{0}()` is both faster and more readable",
mut_str, caller_type)
);
span_lint(cx,
ITER_NTH,
expr.span,
&format!("called `.iter{0}().nth()` on a {1}. Calling `.get{0}()` is both faster and more readable",
mut_str,
caller_type));
}
fn lint_get_unwrap(cx: &LateContext, expr: &hir::Expr, get_args: &[hir::Expr], is_mut: bool) {
@ -932,32 +933,29 @@ fn lint_get_unwrap(cx: &LateContext, expr: &hir::Expr, get_args: &[hir::Expr], i
let mut_str = if is_mut { "_mut" } else { "" };
let borrow_str = if is_mut { "&mut " } else { "&" };
span_lint_and_then(
cx,
GET_UNWRAP,
expr.span,
&format!("called `.get{0}().unwrap()` on a {1}. Using `[]` is more clear and more concise",
mut_str, caller_type),
|db| {
db.span_suggestion(
expr.span,
"try this",
format!("{}{}[{}]", borrow_str, snippet(cx, get_args[0].span, "_"),
snippet(cx, get_args[1].span, "_"))
);
}
);
span_lint_and_then(cx,
GET_UNWRAP,
expr.span,
&format!("called `.get{0}().unwrap()` on a {1}. Using `[]` is more clear and more concise",
mut_str,
caller_type),
|db| {
db.span_suggestion(expr.span,
"try this",
format!("{}{}[{}]",
borrow_str,
snippet(cx, get_args[0].span, "_"),
snippet(cx, get_args[1].span, "_")));
});
}
fn lint_iter_skip_next(cx: &LateContext, expr: &hir::Expr){
fn lint_iter_skip_next(cx: &LateContext, expr: &hir::Expr) {
// lint if caller of skip is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
span_lint(
cx,
ITER_SKIP_NEXT,
expr.span,
"called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`"
);
span_lint(cx,
ITER_SKIP_NEXT,
expr.span,
"called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`");
}
}
@ -975,9 +973,7 @@ fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: ty::Ty) -> Option<sug
if let hir::ExprMethodCall(name, _, ref args) = expr.node {
if &*name.node.as_str() == "iter" && may_slice(cx, cx.tcx.tables().expr_ty(&args[0])) {
sugg::Sugg::hir_opt(cx, &args[0]).map(|sugg| {
sugg.addr()
})
sugg::Sugg::hir_opt(cx, &args[0]).map(|sugg| sugg.addr())
} else {
None
}
@ -991,7 +987,7 @@ fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: ty::Ty) -> Option<sug
} else {
None
}
}
},
_ => None,
}
}
@ -1099,8 +1095,8 @@ fn lint_map_unwrap_or_else(cx: &LateContext, expr: &hir::Expr, map_args: &[hir::
fn lint_filter_next(cx: &LateContext, expr: &hir::Expr, filter_args: &[hir::Expr]) {
// lint if caller of `.filter().next()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` \
instead.";
let msg = "called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling \
`.find(p)` instead.";
let filter_snippet = snippet(cx, filter_args[1].span, "..");
if filter_snippet.lines().count() <= 1 {
// add note if not multi-line
@ -1163,8 +1159,8 @@ fn lint_search_is_some(cx: &LateContext, expr: &hir::Expr, search_method: &str,
is_some_args: &[hir::Expr]) {
// lint if caller of search is an Iterator
if match_trait_method(cx, &is_some_args[0], &paths::ITERATOR) {
let msg = format!("called `is_some()` after searching an `Iterator` with {}. This is more succinctly expressed \
by calling `any()`.",
let msg = format!("called `is_some()` after searching an `Iterator` with {}. This is more succinctly \
expressed by calling `any()`.",
search_method);
let search_snippet = snippet(cx, search_args[1].span, "..");
if search_snippet.lines().count() <= 1 {