Skip to content

Commit ae672b7

Browse files
committed
Use a single if_chain
1 parent 4eba7b2 commit ae672b7

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

clippy_lints/src/match_result_ok.rs

+9-31
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,15 @@ declare_lint_pass!(MatchResultOk => [MATCH_RESULT_OK]);
4747

4848
impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
4949
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
50-
if_chain! {
51-
if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr);
52-
if let ExprKind::MethodCall(_, ok_span, [ref result_types_0, ..], _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
53-
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = let_pat.kind; //get operation
54-
if method_chain_args(let_expr, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
55-
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(result_types_0), sym::result_type);
56-
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some";
57-
58-
then {
59-
60-
let mut applicability = Applicability::MachineApplicable;
61-
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
62-
let trimmed_ok = snippet_with_applicability(cx, let_expr.span.until(ok_span), "", &mut applicability);
63-
let sugg = format!(
64-
"if let Ok({}) = {}",
65-
some_expr_string,
66-
trimmed_ok.trim().trim_end_matches('.'),
67-
);
68-
span_lint_and_sugg(
69-
cx,
70-
MATCH_RESULT_OK,
71-
expr.span.with_hi(let_expr.span.hi()),
72-
"matching on `Some` with `ok()` is redundant",
73-
&format!("consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
74-
sugg,
75-
applicability,
76-
);
77-
}
78-
}
50+
let (let_pat, let_expr, ifwhile) = if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr) {
51+
(let_pat, let_expr, "if")
52+
} else if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr) {
53+
(let_pat, let_expr, "while")
54+
} else {
55+
return
56+
};
7957

8058
if_chain! {
81-
if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr);
8259
if let ExprKind::MethodCall(_, ok_span, [ref result_types_0, ..], _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
8360
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = let_pat.kind; //get operation
8461
if method_chain_args(let_expr, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
@@ -91,7 +68,8 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
9168
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
9269
let trimmed_ok = snippet_with_applicability(cx, let_expr.span.until(ok_span), "", &mut applicability);
9370
let sugg = format!(
94-
"while let Ok({}) = {}",
71+
"{} let Ok({}) = {}",
72+
ifwhile,
9573
some_expr_string,
9674
trimmed_ok.trim().trim_end_matches('.'),
9775
);

0 commit comments

Comments
 (0)