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 78b3bf9 commit d4ba888
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/tools/rustfmt/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cmp::min;

use itertools::Itertools;
use rustc_ast::token::{Delimiter, Lit, LitKind};
use rustc_ast::{ast, MatchKind, ptr, token, ForLoopKind};
use rustc_ast::{ast, ptr, token, ForLoopKind, MatchKind};
use rustc_span::{BytePos, Span};

use crate::chains::rewrite_chain;
Expand Down
35 changes: 23 additions & 12 deletions src/tools/rustfmt/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::iter::repeat;

use rustc_ast::{ast, MatchKind, ptr};
use rustc_ast::{ast, ptr, MatchKind};
use rustc_span::{BytePos, Span};

use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
Expand Down 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 d4ba888

Please sign in to comment.