Skip to content

Commit f2505df

Browse files
committed
Rust: Refactor reconstruct and split_arguments
`split_argument` didn't do much for `wrap`s that where not `Wrap::WriteMethod`. We refactor `reconstruct` to only apply when `split_argument` for `Wrap::WriteMethod`. This simplifies `split_arguments` and avoids the `expect` call in `reconstruct`.
1 parent 8156dc2 commit f2505df

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

rust/extractor/src/translate/format_args.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ pub(crate) fn reconstruct(
5656
input: &tt::TopSubtree,
5757
call_site: Span,
5858
) -> Option<tt::TopSubtree> {
59-
let (writer, content) = split_arguments(wrap, input)?;
6059
let emitter = Emitter {
6160
call_site,
6261
format_args_parens: input.view().top_subtree().delimiter,
6362
};
6463

6564
let mut builder = tt::TopSubtreeBuilder::new(tt::Delimiter::invisible_spanned(call_site));
6665
match wrap {
67-
Wrap::Bare => emitter.push_format_args(&mut builder, content),
66+
Wrap::Bare => emitter.push_format_args(&mut builder, input.view().token_trees()),
6867
Wrap::Call(path) => {
6968
emitter.push_path(&mut builder, path);
70-
emitter.push_parenthesized_format_args(&mut builder, content);
69+
emitter.push_parenthesized_format_args(&mut builder, input.view().token_trees());
7170
}
7271
Wrap::WriteMethod => {
73-
builder.extend_with_tt(writer.expect("write! split always yields a writer"));
72+
let (writer, content) = split_arguments(input)?;
73+
builder.extend_with_tt(writer);
7474
builder.push(emitter.punct('.', tt::Spacing::Alone));
7575
builder.push(emitter.ident("write_fmt"));
7676
emitter.push_parenthesized_format_args(&mut builder, content);
@@ -82,12 +82,8 @@ pub(crate) fn reconstruct(
8282
/// Splits the macro argument list into the leading writer (for `write!`/`writeln!`,
8383
/// everything up to the first top-level comma) and the format arguments.
8484
fn split_arguments<'a>(
85-
wrap: Wrap,
8685
input: &'a tt::TopSubtree,
87-
) -> Option<(Option<tt::TokenTreesView<'a>>, tt::TokenTreesView<'a>)> {
88-
if wrap != Wrap::WriteMethod {
89-
return Some((None, input.view().token_trees()));
90-
}
86+
) -> Option<(tt::TokenTreesView<'a>, tt::TokenTreesView<'a>)> {
9187
let mut iter = input.view().iter();
9288
let start = iter.savepoint();
9389
let mut found_comma = false;
@@ -105,7 +101,7 @@ fn split_arguments<'a>(
105101
}
106102
let writer = iter.from_savepoint(start);
107103
iter.next(); // consume the comma
108-
Some((Some(writer), iter.remaining()))
104+
Some((writer, iter.remaining()))
109105
}
110106

111107
/// Emits the synthesized tokens, tagging them with the macro call site span.

0 commit comments

Comments
 (0)