Skip to content

Commit d90f26c

Browse files
committed
Reduce span range
1 parent d83d6cc commit d90f26c

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

clippy_lints/src/if_let_some_result.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{match_type, method_chain_args, paths, snippet, snippet_with_applicability, span_lint_and_sugg};
1+
use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_then};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::*;
@@ -42,7 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
4242
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
4343
if_chain! { //begin checking variables
4444
if let ExprKind::Match(ref op, ref body, source) = expr.kind; //test if expr is a match
45-
if let MatchSource::IfLetDesugar { contains_else_clause } = source; //test if it is an If Let
45+
if let MatchSource::IfLetDesugar { .. } = source; //test if it is an If Let
4646
if let ExprKind::MethodCall(_, ok_span, ref result_types) = op.kind; //check is expr.ok() has type Result<T,E>.ok()
4747
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation
4848
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 {
5353
let trimed_ok_span = op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1)));
5454
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
5555
let trimmed_ok = snippet_with_applicability(cx, trimed_ok_span, "", &mut applicability);
56-
let mut sugg = format!(
57-
"if let Ok({}) = {} {}",
56+
let sugg = format!(
57+
"if let Ok({}) = {}",
5858
some_expr_string,
5959
trimmed_ok,
60-
snippet(cx, body[0].span, ".."),
6160
);
62-
if contains_else_clause {
63-
sugg = format!("{} else {}", sugg, snippet(cx, body[1].span, ".."));
64-
}
6561
if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type {
66-
span_lint_and_sugg(
62+
span_lint_and_then(
6763
cx,
6864
IF_LET_SOME_RESULT,
6965
expr.span,
7066
"Matching on `Some` with `ok()` is redundant",
71-
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
72-
sugg,
73-
applicability,
67+
|db| {
68+
db.span_suggestion(
69+
expr.span.shrink_to_lo().to(ok_span.with_hi(ok_span.hi() + BytePos(2))),
70+
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
71+
sugg,
72+
applicability,
73+
);
74+
},
7475
);
7576
}
7677
}

tests/ui/if_let_some_result.stderr

+2-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ LL | | }
1212
help: Consider matching on `Ok(y)` and removing the call to `ok` instead
1313
|
1414
LL | if let Ok(y) = x.parse() {
15-
LL | y
16-
LL | } else {
17-
LL | 0
18-
LL | }
19-
|
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^
2016

2117
error: Matching on `Some` with `ok()` is redundant
2218
--> $DIR/if_let_some_result.rs:23:9
@@ -29,9 +25,7 @@ LL | | };
2925
help: Consider matching on `Ok(y)` and removing the call to `ok` instead
3026
|
3127
LL | if let Ok(y) = x.parse() {
32-
LL | return y;
33-
LL | };
34-
|
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^
3529

3630
error: aborting due to 2 previous errors
3731

0 commit comments

Comments
 (0)