Skip to content

Commit 72df99a

Browse files
committed
Auto merge of rust-lang#83302 - camsteffen:write-piece-unchecked, r=dtolnay
Get piece unchecked in `write` We already use specialized `zip`, but it seems like we can do a little better by not checking `pieces` length at all. `Arguments` constructors are now unsafe. So the `format_args!` expansion now includes an `unsafe` block. <details> <summary>Local Bench Diff</summary> ```text name before ns/iter after ns/iter diff ns/iter diff % speedup fmt::write_str_macro1 22,967 19,718 -3,249 -14.15% x 1.16 fmt::write_str_macro2 35,527 32,654 -2,873 -8.09% x 1.09 fmt::write_str_macro_debug 571,953 575,973 4,020 0.70% x 0.99 fmt::write_str_ref 9,579 9,459 -120 -1.25% x 1.01 fmt::write_str_value 9,573 9,572 -1 -0.01% x 1.00 fmt::write_u128_max 176 173 -3 -1.70% x 1.02 fmt::write_u128_min 138 134 -4 -2.90% x 1.03 fmt::write_u64_max 139 136 -3 -2.16% x 1.02 fmt::write_u64_min 129 135 6 4.65% x 0.96 fmt::write_vec_macro1 24,401 22,273 -2,128 -8.72% x 1.10 fmt::write_vec_macro2 37,096 35,602 -1,494 -4.03% x 1.04 fmt::write_vec_macro_debug 588,291 589,575 1,284 0.22% x 1.00 fmt::write_vec_ref 9,568 9,732 164 1.71% x 0.98 fmt::write_vec_value 9,516 9,625 109 1.15% x 0.99 ``` </details>
2 parents 4c847c0 + d5e51db commit 72df99a

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

clippy_utils/src/higher.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,28 @@ impl FormatArgsExpn<'tcx> {
469469
if let ExpnKind::Macro(_, name) = expr.span.ctxt().outer_expn_data().kind;
470470
let name = name.as_str();
471471
if name.ends_with("format_args") || name.ends_with("format_args_nl");
472-
if let ExprKind::Call(_, args) = expr.kind;
473-
if let Some((strs_ref, args, fmt_expr)) = match args {
472+
473+
if let ExprKind::Match(inner_match, [arm], _) = expr.kind;
474+
475+
// `match match`, if you will
476+
if let ExprKind::Match(args, [inner_arm], _) = inner_match.kind;
477+
if let ExprKind::Tup(value_args) = args.kind;
478+
if let Some(value_args) = value_args
479+
.iter()
480+
.map(|e| match e.kind {
481+
ExprKind::AddrOf(_, _, e) => Some(e),
482+
_ => None,
483+
})
484+
.collect();
485+
if let ExprKind::Array(args) = inner_arm.body.kind;
486+
487+
if let ExprKind::Block(Block { stmts: [], expr: Some(expr), .. }, _) = arm.body.kind;
488+
if let ExprKind::Call(_, call_args) = expr.kind;
489+
if let Some((strs_ref, fmt_expr)) = match call_args {
474490
// Arguments::new_v1
475-
[strs_ref, args] => Some((strs_ref, args, None)),
491+
[strs_ref, _] => Some((strs_ref, None)),
476492
// Arguments::new_v1_formatted
477-
[strs_ref, args, fmt_expr] => Some((strs_ref, args, Some(fmt_expr))),
493+
[strs_ref, _, fmt_expr] => Some((strs_ref, Some(fmt_expr))),
478494
_ => None,
479495
};
480496
if let ExprKind::AddrOf(BorrowKind::Ref, _, strs_arr) = strs_ref.kind;
@@ -490,17 +506,6 @@ impl FormatArgsExpn<'tcx> {
490506
None
491507
})
492508
.collect();
493-
if let ExprKind::AddrOf(BorrowKind::Ref, _, args) = args.kind;
494-
if let ExprKind::Match(args, [arm], _) = args.kind;
495-
if let ExprKind::Tup(value_args) = args.kind;
496-
if let Some(value_args) = value_args
497-
.iter()
498-
.map(|e| match e.kind {
499-
ExprKind::AddrOf(_, _, e) => Some(e),
500-
_ => None,
501-
})
502-
.collect();
503-
if let ExprKind::Array(args) = arm.body.kind;
504509
then {
505510
Some(FormatArgsExpn {
506511
format_string_span: strs_ref.span,

0 commit comments

Comments
 (0)