From aab64a21cca6e73cee0a7d6c203aa3ac14a8ea8d Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sat, 11 Jan 2020 04:34:01 +0900 Subject: [PATCH] Reduce span range --- clippy_lints/src/if_let_some_result.rs | 25 +++++++++++++------------ tests/ui/if_let_some_result.stderr | 10 ++-------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/clippy_lints/src/if_let_some_result.rs b/clippy_lints/src/if_let_some_result.rs index dc129a28e37..860caad2858 100644 --- a/clippy_lints/src/if_let_some_result.rs +++ b/clippy_lints/src/if_let_some_result.rs @@ -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.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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet { 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, + ); + }, ); } } diff --git a/tests/ui/if_let_some_result.stderr b/tests/ui/if_let_some_result.stderr index 6925dfa0bf9..0084260b22a 100644 --- a/tests/ui/if_let_some_result.stderr +++ b/tests/ui/if_let_some_result.stderr @@ -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