Skip to content

Commit 55ffeaa

Browse files
committed
Add basic rustfmt implementation & test
1 parent 02e377e commit 55ffeaa

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/tools/rustfmt/src/matches.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ pub(crate) fn rewrite_match(
7272
shape: Shape,
7373
span: Span,
7474
attrs: &[ast::Attribute],
75-
// TODO: Use this
76-
_: MatchKind,
75+
match_kind: MatchKind,
7776
) -> Option<String> {
7877
// Do not take the rhs overhead from the upper expressions into account
7978
// when rewriting match condition.
@@ -133,15 +132,27 @@ pub(crate) fn rewrite_match(
133132
}
134133
} else {
135134
let span_after_cond = mk_sp(cond.span.hi(), span.hi());
136-
Some(format!(
137-
"match {}{}{{\n{}{}{}\n{}}}",
138-
cond_str,
139-
block_sep,
140-
inner_attrs_str,
141-
nested_indent_str,
142-
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
143-
shape.indent.to_string(context.config),
144-
))
135+
136+
match match_kind {
137+
MatchKind::Prefix => Some(format!(
138+
"match {}{}{{\n{}{}{}\n{}}}",
139+
cond_str,
140+
block_sep,
141+
inner_attrs_str,
142+
nested_indent_str,
143+
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
144+
shape.indent.to_string(context.config),
145+
)),
146+
MatchKind::Postfix => Some(format!(
147+
"{}.match{}{{\n{}{}{}\n{}}}",
148+
cond_str,
149+
block_sep,
150+
inner_attrs_str,
151+
nested_indent_str,
152+
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
153+
shape.indent.to_string(context.config),
154+
)),
155+
}
145156
}
146157
}
147158

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(postfix_match)]
2+
3+
fn main() {
4+
let val = Some(42);
5+
6+
val.match {
7+
Some(_) => 2,
8+
_ => 1
9+
};
10+
11+
Some(2).match {
12+
Some(_) => true,
13+
None => false
14+
}.match {
15+
false => "ferris is cute",
16+
true => "I turn cats in to petted cats",
17+
}.match {
18+
_ => (),
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(postfix_match)]
2+
3+
fn main() {
4+
let val = Some(42);
5+
6+
val.match {
7+
Some(_) => 2,
8+
_ => 1,
9+
};
10+
11+
Some(2).match {
12+
Some(_) => true,
13+
None => false,
14+
}.match {
15+
false => "ferris is cute",
16+
true => "I turn cats in to petted cats",
17+
}.match {
18+
_ => (),
19+
}
20+
}

0 commit comments

Comments
 (0)