Skip to content

Commit

Permalink
Reduce span range
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed Jan 11, 2020
1 parent 4ff21f7 commit 7a682aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
25 changes: 13 additions & 12 deletions clippy_lints/src/if_let_some_result.rs
Original file line number Diff line number Diff line change
@@ -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::lint::{LateContext, LateLintPass};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
);
},
);
}
}
Expand Down
10 changes: 2 additions & 8 deletions tests/ui/if_let_some_result.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 7a682aa

Please sign in to comment.