]> git.lizzy.rs Git - rust.git/commitdiff
cleanup and create another helper function that we should use more often
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Mon, 30 Jan 2017 11:43:27 +0000 (12:43 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 21 Feb 2017 11:01:41 +0000 (12:01 +0100)
clippy_lints/src/loops.rs
clippy_lints/src/utils/mod.rs

index d7aa379894049b0b018656f264a85d372fbbf57a..e802c99cd374315b11709f66bed1f7c6d7aeb1ec 100644 (file)
@@ -16,7 +16,7 @@
 
 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,
-            last_path_segment};
+            last_path_segment, span_lint_and_sugg};
 use utils::paths;
 
 /// **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 suggestion = format!("&{}{}",
-                             if method_name == "iter_mut" {
-                                 "mut "
-                             } else {
-                                 ""
-                             },
-                             object);
-    span_lint_and_then(cx,
+    let muta = if method_name == "iter_mut" {
+        "mut "
+    } else {
+        ""
+    };
+    span_lint_and_sugg(cx,
                        EXPLICIT_ITER_LOOP,
-                       expr.span,
-                       &format!("it is more idiomatic to loop over `{}` instead of `{}.{}()`",
-                                suggestion,
-                                object,
-                                method_name),
-                       |db| db.span_suggestion(arg.span, "to write this more concisely, try looping over", suggestion));
+                       arg.span,
+                       "it is more idiomatic to loop over references to containers instead of using explicit \
+                        iteration methods",
+                       "to write this more concisely, try looping over",
+                       format!("&{}{}", muta, object))
 }
 
 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
             if method_name == "iter" || method_name == "iter_mut" {
                 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) {
                 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();
                 assert_eq!(fn_arg_tys.skip_binder().len(), 1);
                 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 {
                     let object = snippet(cx, args[0].span, "_");
                     span_lint_and_then(cx,
index ce581febfe41a45689463c0fa25b7930a5c308ee..19fec7e03f6fec686ef0b93b803807cede703228 100644 (file)
@@ -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`.
 ///
 /// Note: in the JSON format (used by `compiletest_rs`), the help message will appear once per