Skip to content

Commit 7e76a19

Browse files
committed
Apply review comments
1 parent ce06ba3 commit 7e76a19

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

clippy_lints/src/if_let_some_result.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_then};
1+
use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::*;
@@ -50,28 +50,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
5050
then {
5151
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
5252
let mut applicability = Applicability::MachineApplicable;
53-
let trimed_ok_span = op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1)));
53+
// ok_span = `ok`
54+
// op.span = `x.parse() . ok()`
55+
// op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1))) = `x.parse() .`
56+
// op.span.with_lo(ok_span.lo() - BytePos(1)) = ` ok()`
57+
// op.span.with_hi(ok_span.hi() - BytePos(1)) = `x.parse() . o`
5458
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
55-
let trimmed_ok = snippet_with_applicability(cx, trimed_ok_span, "", &mut applicability);
59+
let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability);
5660
let sugg = format!(
5761
"if let Ok({}) = {}",
5862
some_expr_string,
59-
trimmed_ok,
63+
trimmed_ok.trim().trim_end_matches('.'),
6064
);
6165
if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type {
62-
span_lint_and_then(
66+
span_lint_and_sugg(
6367
cx,
6468
IF_LET_SOME_RESULT,
65-
expr.span,
69+
expr.span.with_hi(ok_span.hi() + BytePos(2)),
6670
"Matching on `Some` with `ok()` is redundant",
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-
},
71+
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
72+
sugg,
73+
applicability,
7574
);
7675
}
7776
}

tests/ui/if_let_some_result.fixed

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ fn str_to_int_ok(x: &str) -> i32 {
1818
}
1919
}
2020

21-
fn nested_some_no_else(x: &str) -> i32 {
21+
#[rustfmt::skip]
22+
fn strange_some_no_else(x: &str) -> i32 {
2223
{
23-
if let Ok(y) = x.parse() {
24+
if let Ok(y) = x . parse() {
2425
return y;
2526
};
2627
0
@@ -30,5 +31,5 @@ fn nested_some_no_else(x: &str) -> i32 {
3031
fn main() {
3132
let _ = str_to_int("1");
3233
let _ = str_to_int_ok("2");
33-
let _ = nested_some_no_else("3");
34+
let _ = strange_some_no_else("3");
3435
}

tests/ui/if_let_some_result.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ fn str_to_int_ok(x: &str) -> i32 {
1818
}
1919
}
2020

21-
fn nested_some_no_else(x: &str) -> i32 {
21+
#[rustfmt::skip]
22+
fn strange_some_no_else(x: &str) -> i32 {
2223
{
23-
if let Some(y) = x.parse().ok() {
24+
if let Some(y) = x . parse() . ok() {
2425
return y;
2526
};
2627
0
@@ -30,5 +31,5 @@ fn nested_some_no_else(x: &str) -> i32 {
3031
fn main() {
3132
let _ = str_to_int("1");
3233
let _ = str_to_int_ok("2");
33-
let _ = nested_some_no_else("3");
34+
let _ = strange_some_no_else("3");
3435
}

tests/ui/if_let_some_result.stderr

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
error: Matching on `Some` with `ok()` is redundant
22
--> $DIR/if_let_some_result.rs:6:5
33
|
4-
LL | / if let Some(y) = x.parse().ok() {
5-
LL | | y
6-
LL | | } else {
7-
LL | | 0
8-
LL | | }
9-
| |_____^
4+
LL | if let Some(y) = x.parse().ok() {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106
|
117
= note: `-D clippy::if-let-some-result` implied by `-D warnings`
128
help: Consider matching on `Ok(y)` and removing the call to `ok` instead
@@ -15,17 +11,15 @@ LL | if let Ok(y) = x.parse() {
1511
| ^^^^^^^^^^^^^^^^^^^^^^^^
1612

1713
error: Matching on `Some` with `ok()` is redundant
18-
--> $DIR/if_let_some_result.rs:23:9
14+
--> $DIR/if_let_some_result.rs:24:9
1915
|
20-
LL | / if let Some(y) = x.parse().ok() {
21-
LL | | return y;
22-
LL | | };
23-
| |_________^
16+
LL | if let Some(y) = x . parse() . ok() {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2418
|
2519
help: Consider matching on `Ok(y)` and removing the call to `ok` instead
2620
|
27-
LL | if let Ok(y) = x.parse() {
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^
21+
LL | if let Ok(y) = x . parse() {
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2923

3024
error: aborting due to 2 previous errors
3125

0 commit comments

Comments
 (0)