Skip to content

Commit 907aebc

Browse files
committed
Use more if-chains
1 parent 4130dfb commit 907aebc

File tree

1 file changed

+42
-36
lines changed
  • clippy_lints/src/methods

1 file changed

+42
-36
lines changed

clippy_lints/src/methods/mod.rs

+42-36
Original file line numberDiff line numberDiff line change
@@ -1396,18 +1396,20 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
13961396
a: &hir::Expr,
13971397
applicability: &mut Applicability,
13981398
) -> Vec<String> {
1399-
if let hir::ExprKind::AddrOf(_, ref format_arg) = a.node {
1400-
if let hir::ExprKind::Match(ref format_arg_expr, _, _) = format_arg.node {
1401-
if let hir::ExprKind::Tup(ref format_arg_expr_tup) = format_arg_expr.node {
1402-
return format_arg_expr_tup
1403-
.iter()
1404-
.map(|a| snippet_with_applicability(cx, a.span, "..", applicability).into_owned())
1405-
.collect();
1406-
}
1407-
}
1408-
};
1399+
if_chain! {
1400+
if let hir::ExprKind::AddrOf(_, ref format_arg) = a.node;
1401+
if let hir::ExprKind::Match(ref format_arg_expr, _, _) = format_arg.node;
1402+
if let hir::ExprKind::Tup(ref format_arg_expr_tup) = format_arg_expr.node;
14091403

1410-
unreachable!()
1404+
then {
1405+
format_arg_expr_tup
1406+
.iter()
1407+
.map(|a| snippet_with_applicability(cx, a.span, "..", applicability).into_owned())
1408+
.collect()
1409+
} else {
1410+
unreachable!()
1411+
}
1412+
}
14111413
}
14121414

14131415
fn is_call(node: &hir::ExprKind) -> bool {
@@ -1671,20 +1673,22 @@ fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, source: &hir:
16711673
}
16721674

16731675
fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, iter_args: &'tcx [hir::Expr]) {
1674-
if match_type(cx, cx.tables.expr_ty(expr), &paths::VEC) {
1675-
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])) {
1676-
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite()) {
1677-
span_lint_and_sugg(
1678-
cx,
1679-
ITER_CLONED_COLLECT,
1680-
to_replace,
1681-
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
1682-
more readable",
1683-
"try",
1684-
".to_vec()".to_string(),
1685-
Applicability::MachineApplicable,
1686-
);
1687-
}
1676+
if_chain! {
1677+
if match_type(cx, cx.tables.expr_ty(expr), &paths::VEC);
1678+
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0]));
1679+
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
1680+
1681+
then {
1682+
span_lint_and_sugg(
1683+
cx,
1684+
ITER_CLONED_COLLECT,
1685+
to_replace,
1686+
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
1687+
more readable",
1688+
"try",
1689+
".to_vec()".to_string(),
1690+
Applicability::MachineApplicable,
1691+
);
16881692
}
16891693
}
16901694
}
@@ -1944,18 +1948,20 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, unwrap_args: &[hir::E
19441948

19451949
/// lint use of `ok().expect()` for `Result`s
19461950
fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr, ok_args: &[hir::Expr]) {
1947-
// lint if the caller of `ok()` is a `Result`
1948-
if match_type(cx, cx.tables.expr_ty(&ok_args[0]), &paths::RESULT) {
1951+
if_chain! {
1952+
// lint if the caller of `ok()` is a `Result`
1953+
if match_type(cx, cx.tables.expr_ty(&ok_args[0]), &paths::RESULT);
19491954
let result_type = cx.tables.expr_ty(&ok_args[0]);
1950-
if let Some(error_type) = get_error_type(cx, result_type) {
1951-
if has_debug_impl(error_type, cx) {
1952-
span_lint(
1953-
cx,
1954-
OK_EXPECT,
1955-
expr.span,
1956-
"called `ok().expect()` on a Result value. You can call `expect` directly on the `Result`",
1957-
);
1958-
}
1955+
if let Some(error_type) = get_error_type(cx, result_type);
1956+
if has_debug_impl(error_type, cx);
1957+
1958+
then {
1959+
span_lint(
1960+
cx,
1961+
OK_EXPECT,
1962+
expr.span,
1963+
"called `ok().expect()` on a Result value. You can call `expect` directly on the `Result`",
1964+
);
19591965
}
19601966
}
19611967
}

0 commit comments

Comments
 (0)