about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRoss Smyth <crs2017@gmail.com>2024-02-17 14:01:02 -0500
committerRoss Smyth <crs2017@gmail.com>2024-03-06 00:51:45 -0500
commit1709dd59bd3aa5f9778ef9c03e8792fb8199a313 (patch)
tree846430c8ab3ca7c5fb6aaed3deca8966e92145dd
parentf3892a06cc385a1d8c36f611680a866567b59a0e (diff)
downloadrust-1709dd59bd3aa5f9778ef9c03e8792fb8199a313.tar.gz
rust-1709dd59bd3aa5f9778ef9c03e8792fb8199a313.zip
Add basic rustfmt implementation & test
-rw-r--r--src/expr.rs2
-rw-r--r--src/matches.rs35
-rw-r--r--tests/source/postfix-match/pf-match.rs20
-rw-r--r--tests/target/postfix-match/pf-match.rs20
4 files changed, 64 insertions, 13 deletions
diff --git a/src/expr.rs b/src/expr.rs
index e04290ce87c..053afcc52d4 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -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;
diff --git a/src/matches.rs b/src/matches.rs
index 63e3162be2c..e68903c8715 100644
--- a/src/matches.rs
+++ b/src/matches.rs
@@ -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};
@@ -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.
@@ -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),
+            )),
+        }
     }
 }
 
diff --git a/tests/source/postfix-match/pf-match.rs b/tests/source/postfix-match/pf-match.rs
new file mode 100644
index 00000000000..b2366723631
--- /dev/null
+++ b/tests/source/postfix-match/pf-match.rs
@@ -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 {
+        _ => (),
+    }
+}
\ No newline at end of file
diff --git a/tests/target/postfix-match/pf-match.rs b/tests/target/postfix-match/pf-match.rs
new file mode 100644
index 00000000000..f439f272623
--- /dev/null
+++ b/tests/target/postfix-match/pf-match.rs
@@ -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 {
+        _ => (),
+    }
+}