cleanup and create another helper function that we should use more often
This commit is contained in:
parent
0bea3e76d4
commit
c1cf5a2cde
@ -16,7 +16,7 @@ use utils::sugg;
|
|||||||
|
|
||||||
use utils::{snippet, span_lint, get_parent_expr, match_trait_method, match_type, multispan_sugg, in_external_macro,
|
use utils::{snippet, span_lint, get_parent_expr, match_trait_method, match_type, multispan_sugg, in_external_macro,
|
||||||
is_refutable, span_help_and_lint, is_integer_literal, get_enclosing_block, span_lint_and_then, higher,
|
is_refutable, span_help_and_lint, is_integer_literal, get_enclosing_block, span_lint_and_then, higher,
|
||||||
last_path_segment};
|
last_path_segment, span_lint_and_sugg};
|
||||||
use utils::paths;
|
use utils::paths;
|
||||||
|
|
||||||
/// **What it does:** Checks for looping over the range of `0..len` of some
|
/// **What it does:** Checks for looping over the range of `0..len` of some
|
||||||
@ -644,23 +644,20 @@ fn check_for_loop_reverse_range(cx: &LateContext, arg: &Expr, expr: &Expr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lint_iter_method(cx: &LateContext, args: &[Expr], arg: &Expr, expr: &Expr, method_name: &str) {
|
fn lint_iter_method(cx: &LateContext, args: &[Expr], arg: &Expr, method_name: &str) {
|
||||||
let object = snippet(cx, args[0].span, "_");
|
let object = snippet(cx, args[0].span, "_");
|
||||||
let suggestion = format!("&{}{}",
|
let muta = if method_name == "iter_mut" {
|
||||||
if method_name == "iter_mut" {
|
"mut "
|
||||||
"mut "
|
} else {
|
||||||
} else {
|
""
|
||||||
""
|
};
|
||||||
},
|
span_lint_and_sugg(cx,
|
||||||
object);
|
|
||||||
span_lint_and_then(cx,
|
|
||||||
EXPLICIT_ITER_LOOP,
|
EXPLICIT_ITER_LOOP,
|
||||||
expr.span,
|
arg.span,
|
||||||
&format!("it is more idiomatic to loop over `{}` instead of `{}.{}()`",
|
"it is more idiomatic to loop over references to containers instead of using explicit \
|
||||||
suggestion,
|
iteration methods",
|
||||||
object,
|
"to write this more concisely, try looping over",
|
||||||
method_name),
|
format!("&{}{}", muta, object))
|
||||||
|db| db.span_suggestion(arg.span, "to write this more concisely, try looping over", suggestion));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_for_loop_arg(cx: &LateContext, pat: &Pat, arg: &Expr, expr: &Expr) {
|
fn check_for_loop_arg(cx: &LateContext, pat: &Pat, arg: &Expr, expr: &Expr) {
|
||||||
@ -672,7 +669,7 @@ fn check_for_loop_arg(cx: &LateContext, pat: &Pat, arg: &Expr, expr: &Expr) {
|
|||||||
// check for looping over x.iter() or x.iter_mut(), could use &x or &mut x
|
// check for looping over x.iter() or x.iter_mut(), could use &x or &mut x
|
||||||
if method_name == "iter" || method_name == "iter_mut" {
|
if method_name == "iter" || method_name == "iter_mut" {
|
||||||
if is_ref_iterable_type(cx, &args[0]) {
|
if is_ref_iterable_type(cx, &args[0]) {
|
||||||
lint_iter_method(cx, args, arg, expr, method_name);
|
lint_iter_method(cx, args, arg, method_name);
|
||||||
}
|
}
|
||||||
} else if method_name == "into_iter" && match_trait_method(cx, arg, &paths::INTO_ITERATOR) {
|
} else if method_name == "into_iter" && match_trait_method(cx, arg, &paths::INTO_ITERATOR) {
|
||||||
let method_call = ty::MethodCall::expr(arg.id);
|
let method_call = ty::MethodCall::expr(arg.id);
|
||||||
@ -684,7 +681,7 @@ fn check_for_loop_arg(cx: &LateContext, pat: &Pat, arg: &Expr, expr: &Expr) {
|
|||||||
let fn_arg_tys = fn_ty.fn_args();
|
let fn_arg_tys = fn_ty.fn_args();
|
||||||
assert_eq!(fn_arg_tys.skip_binder().len(), 1);
|
assert_eq!(fn_arg_tys.skip_binder().len(), 1);
|
||||||
if fn_arg_tys.skip_binder()[0].is_region_ptr() {
|
if fn_arg_tys.skip_binder()[0].is_region_ptr() {
|
||||||
lint_iter_method(cx, args, arg, expr, method_name);
|
lint_iter_method(cx, args, arg, method_name);
|
||||||
} else {
|
} else {
|
||||||
let object = snippet(cx, args[0].span, "_");
|
let object = snippet(cx, args[0].span, "_");
|
||||||
span_lint_and_then(cx,
|
span_lint_and_then(cx,
|
||||||
|
@ -569,6 +569,17 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>(
|
||||||
|
cx: &'a T,
|
||||||
|
lint: &'static Lint,
|
||||||
|
sp: Span,
|
||||||
|
msg: &str,
|
||||||
|
help: &str,
|
||||||
|
sugg: String,
|
||||||
|
) {
|
||||||
|
span_lint_and_then(cx, lint, sp, msg, |db| { db.span_suggestion(sp, help, sugg); });
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a suggestion made from several `span → replacement`.
|
/// Create a suggestion made from several `span → replacement`.
|
||||||
///
|
///
|
||||||
/// Note: in the JSON format (used by `compiletest_rs`), the help message will appear once per
|
/// Note: in the JSON format (used by `compiletest_rs`), the help message will appear once per
|
||||||
|
Loading…
x
Reference in New Issue
Block a user