Skip to content

Commit 5c3f1e6

Browse files
committed
Auto merge of #8370 - smoelius:master, r=flip1995
Format `if_chain` invocations in clippy_utils Not realizing it was [already obsolete](#8360), I built a [tool to format inside `if_chain` invocations](https://crates.io/crates/rustfmt_if_chain). This PR applies the tool to clippy_utils. (If you apply it to clippy_lints, the changes are extensive.) Anyway, I'm making it known here in case anyone wants to use it while `if-let` chain support is developed for `rustfmt`. (There could be a few Clippy PRs between now and then, and IMHO, the code looks better with the `if_chain` invocations formatted.) Cheers.
2 parents 327768c + e7922f7 commit 5c3f1e6

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

clippy_utils/src/consts.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,16 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
331331
let def_path: Vec<&str> = def_path.iter().take(4).map(Symbol::as_str).collect();
332332
if let ["core", "num", int_impl, "max_value"] = *def_path;
333333
then {
334-
let value = match int_impl {
335-
"<impl i8>" => i8::MAX as u128,
336-
"<impl i16>" => i16::MAX as u128,
337-
"<impl i32>" => i32::MAX as u128,
338-
"<impl i64>" => i64::MAX as u128,
339-
"<impl i128>" => i128::MAX as u128,
340-
_ => return None,
341-
};
342-
Some(Constant::Int(value))
343-
}
344-
else {
334+
let value = match int_impl {
335+
"<impl i8>" => i8::MAX as u128,
336+
"<impl i16>" => i16::MAX as u128,
337+
"<impl i32>" => i32::MAX as u128,
338+
"<impl i64>" => i64::MAX as u128,
339+
"<impl i128>" => i128::MAX as u128,
340+
_ => return None,
341+
};
342+
Some(Constant::Int(value))
343+
} else {
345344
None
346345
}
347346
}

clippy_utils/src/higher.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ impl<'a> VecArgs<'a> {
284284
return if match_def_path(cx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 {
285285
// `vec![elem; size]` case
286286
Some(VecArgs::Repeat(&args[0], &args[1]))
287-
}
288-
else if match_def_path(cx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 {
287+
} else if match_def_path(cx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 {
289288
// `vec![a, b, c]` case
290289
if_chain! {
291290
if let hir::ExprKind::Box(boxed) = args[0].kind;
@@ -296,11 +295,9 @@ impl<'a> VecArgs<'a> {
296295
}
297296

298297
None
299-
}
300-
else if match_def_path(cx, fun_def_id, &paths::VEC_NEW) && args.is_empty() {
298+
} else if match_def_path(cx, fun_def_id, &paths::VEC_NEW) && args.is_empty() {
301299
Some(VecArgs::Vec(&[]))
302-
}
303-
else {
300+
} else {
304301
None
305302
};
306303
}
@@ -456,7 +453,7 @@ pub fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -
456453
if let ExprKind::Lit(lit) = &arg.kind;
457454
if let LitKind::Int(num, _) = lit.node;
458455
then {
459-
return Some(VecInitKind::WithLiteralCapacity(num.try_into().ok()?))
456+
return Some(VecInitKind::WithLiteralCapacity(num.try_into().ok()?));
460457
}
461458
}
462459
return Some(VecInitKind::WithExprCapacity(arg.hir_id));

clippy_utils/src/lib.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,9 @@ pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, def_id: LocalDefId) ->
603603
if parent_impl != CRATE_DEF_ID;
604604
if let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent_impl);
605605
if let hir::ItemKind::Impl(impl_) = &item.kind;
606-
then { return impl_.of_trait.as_ref(); }
606+
then {
607+
return impl_.of_trait.as_ref();
608+
}
607609
}
608610
None
609611
}
@@ -713,12 +715,7 @@ pub fn is_default_equivalent_call(cx: &LateContext<'_>, repl_func: &Expr<'_>) ->
713715
if let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
714716
if is_diag_trait_item(cx, repl_def_id, sym::Default)
715717
|| is_default_equivalent_ctor(cx, repl_def_id, repl_func_qpath);
716-
then {
717-
true
718-
}
719-
else {
720-
false
721-
}
718+
then { true } else { false }
722719
}
723720
}
724721

@@ -1553,8 +1550,7 @@ pub fn is_try<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Option<&'tc
15531550
if arms.len() == 2;
15541551
if arms[0].guard.is_none();
15551552
if arms[1].guard.is_none();
1556-
if (is_ok(cx, &arms[0]) && is_err(cx, &arms[1])) ||
1557-
(is_ok(cx, &arms[1]) && is_err(cx, &arms[0]));
1553+
if (is_ok(cx, &arms[0]) && is_err(cx, &arms[1])) || (is_ok(cx, &arms[1]) && is_err(cx, &arms[0]));
15581554
then {
15591555
return Some(expr);
15601556
}
@@ -1644,7 +1640,7 @@ pub fn match_function_call<'tcx>(
16441640
if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
16451641
if match_def_path(cx, fun_def_id, path);
16461642
then {
1647-
return Some(args)
1643+
return Some(args);
16481644
}
16491645
};
16501646
None

clippy_utils/src/macros.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,11 @@ impl<'tcx> FormatArgsExpn<'tcx> {
452452
if let Ok(i) = usize::try_from(position);
453453
if let Some(&(j, format_trait)) = self.formatters.get(i);
454454
then {
455-
Some(FormatArgsArg { value: self.value_args[j], format_trait, spec: Some(spec) })
455+
Some(FormatArgsArg {
456+
value: self.value_args[j],
457+
format_trait,
458+
spec: Some(spec),
459+
})
456460
} else {
457461
None
458462
}

0 commit comments

Comments
 (0)