Skip to content

Commit 08680cf

Browse files
committed
Reduce span range
1 parent d54cb12 commit 08680cf

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::declare_lint_pass;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -43,7 +43,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
4343
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
4444
if_chain! { //begin checking variables
4545
if let ExprKind::Match(ref op, ref body, source) = expr.kind; //test if expr is a match
46-
if let MatchSource::IfLetDesugar { contains_else_clause } = source; //test if it is an If Let
46+
if let MatchSource::IfLetDesugar { .. } = source; //test if it is an If Let
4747
if let ExprKind::MethodCall(_, ok_span, ref result_types) = op.kind; //check is expr.ok() has type Result<T,E>.ok()
4848
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation
4949
if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
@@ -54,24 +54,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
5454
let trimed_ok_span = op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1)));
5555
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
5656
let trimmed_ok = snippet_with_applicability(cx, trimed_ok_span, "", &mut applicability);
57-
let mut sugg = format!(
58-
"if let Ok({}) = {} {}",
57+
let sugg = format!(
58+
"if let Ok({}) = {}",
5959
some_expr_string,
6060
trimmed_ok,
61-
snippet(cx, body[0].span, ".."),
6261
);
63-
if contains_else_clause {
64-
sugg = format!("{} else {}", sugg, snippet(cx, body[1].span, ".."));
65-
}
6662
if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type {
67-
span_lint_and_sugg(
63+
span_lint_and_then(
6864
cx,
6965
IF_LET_SOME_RESULT,
7066
expr.span,
7167
"Matching on `Some` with `ok()` is redundant",
72-
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
73-
sugg,
74-
applicability,
68+
|db| {
69+
db.span_suggestion(
70+
expr.span.shrink_to_lo().to(ok_span.with_hi(ok_span.hi() + BytePos(2))),
71+
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
72+
sugg,
73+
applicability,
74+
);
75+
},
7576
);
7677
}
7778
}

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)