Skip to content

Commit

Permalink
Add basic rustfmt implementation & test
Browse files Browse the repository at this point in the history
  • Loading branch information
RossSmyth committed Mar 6, 2024
1 parent b2ca9d4 commit 87dbd59
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/tools/rustfmt/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ pub(crate) fn rewrite_match(
shape: Shape,
span: Span,
attrs: &[ast::Attribute],
// TODO: Use this
_: MatchKind,
match_kind: MatchKind,
) -> Option<String> {
// Do not take the rhs overhead from the upper expressions into account
// when rewriting match condition.
Expand Down Expand Up @@ -133,15 +132,27 @@ pub(crate) fn rewrite_match(
}
} else {
let span_after_cond = mk_sp(cond.span.hi(), span.hi());
Some(format!(
"match {}{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
))

match match_kind {
MatchKind::Prefix => Some(format!(
"match {}{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
)),
MatchKind::Postfix => Some(format!(
"{}.match{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
)),
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/tools/rustfmt/tests/source/postfix-match/pf-match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![feature(postfix_match)]

fn main() {
let val = Some(42);

val.match {
Some(_) => 2,
_ => 1
};

Some(2).match {
Some(_) => true,
None => false
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}
20 changes: 20 additions & 0 deletions src/tools/rustfmt/tests/target/postfix-match/pf-match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![feature(postfix_match)]

fn main() {
let val = Some(42);

val.match {
Some(_) => 2,
_ => 1,
};

Some(2).match {
Some(_) => true,
None => false,
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}

0 comments on commit 87dbd59

Please sign in to comment.