]> git.lizzy.rs Git - rust.git/commitdiff
Reduce span range
authorYuki Okushi <huyuumi.dev@gmail.com>
Fri, 10 Jan 2020 19:34:01 +0000 (04:34 +0900)
committerYuki Okushi <huyuumi.dev@gmail.com>
Sun, 19 Jan 2020 01:06:08 +0000 (10:06 +0900)
clippy_lints/src/if_let_some_result.rs
tests/ui/if_let_some_result.stderr

index dc129a28e3707bd5d2dd1277e2201a43e70c8543..860caad285865508503292d6fdbfe35527e72408 100644 (file)
@@ -1,4 +1,4 @@
-use crate::utils::{match_type, method_chain_args, paths, snippet, snippet_with_applicability, span_lint_and_sugg};
+use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_then};
 use if_chain::if_chain;
 use rustc_errors::Applicability;
 use rustc_hir::*;
@@ -42,7 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
         if_chain! { //begin checking variables
             if let ExprKind::Match(ref op, ref body, source) = expr.kind; //test if expr is a match
-            if let MatchSource::IfLetDesugar { contains_else_clause } = source; //test if it is an If Let
+            if let MatchSource::IfLetDesugar { .. } = source; //test if it is an If Let
             if let ExprKind::MethodCall(_, ok_span, ref result_types) = op.kind; //check is expr.ok() has type Result<T,E>.ok()
             if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _)  = body[0].pat.kind; //get operation
             if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
@@ -53,24 +53,25 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
                 let trimed_ok_span = op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1)));
                 let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
                 let trimmed_ok = snippet_with_applicability(cx, trimed_ok_span, "", &mut applicability);
-                let mut sugg = format!(
-                    "if let Ok({}) = {} {}",
+                let sugg = format!(
+                    "if let Ok({}) = {}",
                     some_expr_string,
                     trimmed_ok,
-                    snippet(cx, body[0].span, ".."),
                 );
-                if contains_else_clause {
-                    sugg = format!("{} else {}", sugg, snippet(cx, body[1].span, ".."));
-                }
                 if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type {
-                    span_lint_and_sugg(
+                    span_lint_and_then(
                         cx,
                         IF_LET_SOME_RESULT,
                         expr.span,
                         "Matching on `Some` with `ok()` is redundant",
-                        &format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
-                        sugg,
-                        applicability,
+                        |db| {
+                            db.span_suggestion(
+                                expr.span.shrink_to_lo().to(ok_span.with_hi(ok_span.hi() + BytePos(2))),
+                                &format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
+                                sugg,
+                                applicability,
+                            );
+                        },
                     );
                 }
             }
index 6925dfa0bf9b7235326534dfd9f21baaf3ea2ed1..0084260b22afebb1a7cc9313adcf5419d689b6d5 100644 (file)
@@ -12,11 +12,7 @@ LL | |     }
 help: Consider matching on `Ok(y)` and removing the call to `ok` instead
    |
 LL |     if let Ok(y) = x.parse() {
-LL |         y
-LL |     } else {
-LL |         0
-LL |     }
-   |
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: Matching on `Some` with `ok()` is redundant
   --> $DIR/if_let_some_result.rs:23:9
@@ -29,9 +25,7 @@ LL | |         };
 help: Consider matching on `Ok(y)` and removing the call to `ok` instead
    |
 LL |         if let Ok(y) = x.parse() {
-LL |             return y;
-LL |         };
-   |
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: aborting due to 2 previous errors