@@ -31,18 +31,31 @@ use crate::utils::{format_mutability, mk_sp, mk_sp_lo_plus_one, rewrite_ident};
3131/// - `[small, ntp]`
3232/// - unary tuple constructor `([small, ntp])`
3333/// - `&[small]`
34- pub ( crate ) fn is_short_pattern ( pat : & ast:: Pat , pat_str : & str ) -> bool {
34+ pub ( crate ) fn is_short_pattern (
35+ context : & RewriteContext < ' _ > ,
36+ pat : & ast:: Pat ,
37+ pat_str : & str ,
38+ ) -> bool {
3539 // We also require that the pattern is reasonably 'small' with its literal width.
36- pat_str. len ( ) <= 20 && !pat_str. contains ( '\n' ) && is_short_pattern_inner ( pat)
40+ pat_str. len ( ) <= 20 && !pat_str. contains ( '\n' ) && is_short_pattern_inner ( context , pat)
3741}
3842
39- fn is_short_pattern_inner ( pat : & ast:: Pat ) -> bool {
40- match pat. kind {
41- ast:: PatKind :: Rest
42- | ast:: PatKind :: Never
43- | ast:: PatKind :: Wild
44- | ast:: PatKind :: Err ( _)
45- | ast:: PatKind :: Expr ( _) => true ,
43+ fn is_short_pattern_inner ( context : & RewriteContext < ' _ > , pat : & ast:: Pat ) -> bool {
44+ match & pat. kind {
45+ ast:: PatKind :: Rest | ast:: PatKind :: Never | ast:: PatKind :: Wild | ast:: PatKind :: Err ( _) => {
46+ true
47+ }
48+ ast:: PatKind :: Expr ( expr) => match & expr. kind {
49+ ast:: ExprKind :: Lit ( _) => true ,
50+ ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , expr) => match & expr. kind {
51+ ast:: ExprKind :: Lit ( _) => true ,
52+ _ => unreachable ! ( ) ,
53+ } ,
54+ ast:: ExprKind :: ConstBlock ( _) | ast:: ExprKind :: Path ( ..) => {
55+ context. config . style_edition ( ) <= StyleEdition :: Edition2024
56+ }
57+ _ => unreachable ! ( ) ,
58+ } ,
4659 ast:: PatKind :: Ident ( _, _, ref pat) => pat. is_none ( ) ,
4760 ast:: PatKind :: Struct ( ..)
4861 | ast:: PatKind :: MacCall ( ..)
@@ -57,8 +70,8 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
5770 ast:: PatKind :: Box ( ref p)
5871 | PatKind :: Deref ( ref p)
5972 | ast:: PatKind :: Ref ( ref p, _)
60- | ast:: PatKind :: Paren ( ref p) => is_short_pattern_inner ( & * p) ,
61- PatKind :: Or ( ref pats) => pats. iter ( ) . all ( |p| is_short_pattern_inner ( p) ) ,
73+ | ast:: PatKind :: Paren ( ref p) => is_short_pattern_inner ( context , & * p) ,
74+ PatKind :: Or ( ref pats) => pats. iter ( ) . all ( |p| is_short_pattern_inner ( context , p) ) ,
6275 }
6376}
6477
@@ -96,7 +109,7 @@ impl Rewrite for Pat {
96109 let use_mixed_layout = pats
97110 . iter ( )
98111 . zip ( pat_strs. iter ( ) )
99- . all ( |( pat, pat_str) | is_short_pattern ( pat, pat_str) ) ;
112+ . all ( |( pat, pat_str) | is_short_pattern ( context , pat, pat_str) ) ;
100113 let items: Vec < _ > = pat_strs. into_iter ( ) . map ( ListItem :: from_str) . collect ( ) ;
101114 let tactic = if use_mixed_layout {
102115 DefinitiveListTactic :: Mixed
0 commit comments