@@ -25,12 +25,12 @@ use crate::comment::{
2525 contains_comment, CharClasses , FindUncommented , FullCodeCharKind , LineClasses ,
2626} ;
2727use crate :: config:: lists:: * ;
28- use crate :: expr:: { rewrite_array, rewrite_assign_rhs, RhsAssignKind } ;
28+ use crate :: expr:: { choose_separator_tactic , rewrite_array, rewrite_assign_rhs, RhsAssignKind } ;
2929use crate :: lists:: { itemize_list, write_list, ListFormatting } ;
3030use crate :: matches:: rewrite_guard;
3131use crate :: overflow;
3232use crate :: parse:: macros:: lazy_static:: parse_lazy_static;
33- use crate :: parse:: macros:: matches:: MatchesMacroItem ;
33+ use crate :: parse:: macros:: matches:: { parse_matches , MatchesMacroItem } ;
3434use crate :: parse:: macros:: { parse_expr, parse_macro_args, ParsedMacroArgs } ;
3535use crate :: rewrite:: { Rewrite , RewriteContext } ;
3636use crate :: shape:: { Indent , Shape } ;
@@ -232,6 +232,10 @@ fn rewrite_macro_inner(
232232 if let success @ Some ( ..) = format_lazy_static ( context, shape, ts. clone ( ) ) {
233233 return success;
234234 }
235+ } else if macro_name == "matches!" {
236+ if let success @ Some ( ..) = format_matches ( context, shape, & macro_name, mac) {
237+ return success;
238+ }
235239 }
236240
237241 let ParsedMacroArgs {
@@ -1328,6 +1332,46 @@ impl Rewrite for MatchesMacroItem {
13281332 }
13291333}
13301334
1335+ /// Format `matches!` from <https://doc.rust-lang.org/std/macro.matches.html>
1336+ ///
1337+ /// # Expected syntax
1338+ ///
1339+ /// ```text
1340+ /// matches!(expr, pat)
1341+ /// matches!(expr, pat if expr)
1342+ /// ```
1343+ fn format_matches (
1344+ context : & RewriteContext < ' _ > ,
1345+ shape : Shape ,
1346+ name : & str ,
1347+ mac : & ast:: MacCall ,
1348+ ) -> Option < String > {
1349+ let span = mac. span ( ) ;
1350+ let matches = parse_matches ( context, mac. args . tokens . clone ( ) ) ?. items ( ) ;
1351+ let force_separator_tactic = choose_separator_tactic ( context, span) ;
1352+ match mac. args . delim {
1353+ ast:: MacDelimiter :: Parenthesis => overflow:: rewrite_with_parens (
1354+ context,
1355+ name,
1356+ matches. iter ( ) ,
1357+ shape,
1358+ span,
1359+ shape. width ,
1360+ force_separator_tactic,
1361+ ) ,
1362+ ast:: MacDelimiter :: Bracket => overflow:: rewrite_with_square_brackets (
1363+ context,
1364+ name,
1365+ matches. iter ( ) ,
1366+ shape,
1367+ span,
1368+ force_separator_tactic,
1369+ None ,
1370+ ) ,
1371+ ast:: MacDelimiter :: Brace => None ,
1372+ }
1373+ }
1374+
13311375/// Format `lazy_static!` from <https://crates.io/crates/lazy_static>.
13321376///
13331377/// # Expected syntax
0 commit comments