Skip to content

Commit c9f8d03

Browse files
committed
Treat more strange pattern
1 parent 7e76a19 commit c9f8d03

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

clippy_lints/src/if_let_some_result.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_errors::Applicability;
44
use rustc_hir::*;
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7-
use rustc_span::BytePos;
87

98
declare_clippy_lint! {
109
/// **What it does:*** Checks for unnecessary `ok()` in if let.
@@ -46,33 +45,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
4645
if let ExprKind::MethodCall(_, ok_span, ref result_types) = op.kind; //check is expr.ok() has type Result<T,E>.ok()
4746
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation
4847
if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
48+
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
49+
if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type;
4950

5051
then {
51-
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
5252
let mut applicability = Applicability::MachineApplicable;
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`
5853
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
5954
let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability);
6055
let sugg = format!(
6156
"if let Ok({}) = {}",
6257
some_expr_string,
6358
trimmed_ok.trim().trim_end_matches('.'),
6459
);
65-
if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type {
66-
span_lint_and_sugg(
67-
cx,
68-
IF_LET_SOME_RESULT,
69-
expr.span.with_hi(ok_span.hi() + BytePos(2)),
70-
"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,
74-
);
75-
}
60+
span_lint_and_sugg(
61+
cx,
62+
IF_LET_SOME_RESULT,
63+
expr.span.with_hi(op.span.hi()),
64+
"Matching on `Some` with `ok()` is redundant",
65+
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
66+
sugg,
67+
applicability,
68+
);
7669
}
7770
}
7871
}

tests/ui/if_let_some_result.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn str_to_int_ok(x: &str) -> i32 {
2121
#[rustfmt::skip]
2222
fn strange_some_no_else(x: &str) -> i32 {
2323
{
24-
if let Some(y) = x . parse() . ok() {
24+
if let Some(y) = x . parse() . ok () {
2525
return y;
2626
};
2727
0

tests/ui/if_let_some_result.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ LL | if let Ok(y) = x.parse() {
1313
error: Matching on `Some` with `ok()` is redundant
1414
--> $DIR/if_let_some_result.rs:24:9
1515
|
16-
LL | if let Some(y) = x . parse() . ok() {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | if let Some(y) = x . parse() . ok () {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
|
1919
help: Consider matching on `Ok(y)` and removing the call to `ok` instead
2020
|

0 commit comments

Comments
 (0)