Reduce span range
This commit is contained in:
parent
ae872fe1c7
commit
aab64a21cc
@ -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 @@ 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,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user