]> git.lizzy.rs Git - rust.git/commitdiff
Address some review comments
authorPhil Ellison <phil.j.ellison@gmail.com>
Sun, 14 Jan 2018 18:18:09 +0000 (18:18 +0000)
committerPhil Ellison <phil.j.ellison@gmail.com>
Sun, 14 Jan 2018 18:18:09 +0000 (18:18 +0000)
clippy_lints/src/methods.rs
clippy_lints/src/utils/mod.rs
tests/ui/methods.rs
tests/ui/methods.stderr

index 138dbed31358d9c7fbdc3ca3c80d298fe2d8727e..9661a6011bd86f849fe89caf6d7f918661597f90 100644 (file)
@@ -1153,17 +1153,18 @@ fn lint_fold_any(cx: &LateContext, expr: &hir::Expr, fold_args: &[hir::Expr]) {
         then {
             let right_source = snippet(cx, right_expr.span, "EXPR");
 
-            span_lint(
+            span_lint_and_sugg(
                 cx,
                 FOLD_ANY,
                 expr.span,
                 // TODO: don't suggest .any(|x| f(x)) if we can suggest .any(f)
-                &format!(
-                    ".fold(false, |{f}, {s}| {f} || {r})) is more succinctly expressed as .any(|{s}| {r})",
-                    f = first_arg_ident,
+                "this `.fold` can more succintly be expressed as `.any`",
+                "try",
+                format!(
+                    ".any(|{s}| {r})",
                     s = second_arg_ident,
                     r = right_source
-                ),
+                )
             );
         }
     }
index 94d2caf9c50f6c135bd567a7cfc513e6cc4f76b8..44ebc5aa60018e8eca84a9e0dd7f16069f1d2249 100644 (file)
@@ -596,6 +596,20 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
     db.docs_link(lint);
 }
 
+/// Add a span lint with a suggestion on how to fix it.
+///
+/// These suggestions can be parsed by rustfix to allow it to automatically fix your code.
+/// In the example below, `help` is `"try"` and `sugg` is the suggested replacement `".any(|x| x > 2)"`.
+///
+/// <pre>
+/// error: This `.fold` can be more succinctly expressed as `.any`
+/// --> $DIR/methods.rs:390:13
+///     |
+/// 390 |     let _ = (0..3).fold(false, |acc, x| acc || x > 2);
+///     |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
+///     |
+///     = note: `-D fold-any` implied by `-D warnings`
+/// </pre>
 pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>(
     cx: &'a T,
     lint: &'static Lint,
index 0dd4ff47fa9097fcafaf75ee68876ebbe5f8c5fa..8cffbf76924fab2f2a5af662dee4e0494151f142 100644 (file)
@@ -391,10 +391,15 @@ fn fold_any() {
 }
 
 /// Checks implementation of the `FOLD_ANY` lint
-fn fold_any_ignore_initial_value_of_true() {
+fn fold_any_ignores_initial_value_of_true() {
     let _ = (0..3).fold(true, |acc, x| acc || x > 2);
 }
 
+/// Checks implementation of the `FOLD_ANY` lint
+fn fold_any_ignores_non_boolean_accumalator() {
+    let _ = (0..3).fold(0, |acc, x| acc + if x > 2 { 1 } else { 0 });
+}
+
 #[allow(similar_names)]
 fn main() {
     let opt = Some(0);
index ef24e4a8e22de892c807657ea97447b1b93c5d5f..f1746354380ffef539e2e786e3dcf0ed6bd3334d 100644 (file)
@@ -493,18 +493,18 @@ error: called `skip(x).next()` on an iterator. This is more succinctly expressed
 382 |     let _ = &some_vec[..].iter().skip(3).next();
     |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: .fold(false, |acc, x| acc || x > 2)) is more succinctly expressed as .any(|x| x > 2)
+error: this `.fold` can more succintly be expressed as `.any`
    --> $DIR/methods.rs:390:13
     |
 390 |     let _ = (0..3).fold(false, |acc, x| acc || x > 2);
-    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
     |
     = note: `-D fold-any` implied by `-D warnings`
 
 error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
-   --> $DIR/methods.rs:401:13
+   --> $DIR/methods.rs:406:13
     |
-401 |     let _ = opt.unwrap();
+406 |     let _ = opt.unwrap();
     |             ^^^^^^^^^^^^
     |
     = note: `-D option-unwrap-used` implied by `-D warnings`