Skip to content

Commit fbcc07a

Browse files
committed
Better encapsulate guard rewrite logic in relation to pattern rewirte
The guard rewrite rules vary based on how the pattern was rewritten. That logic was previously written in `rewrite_match_arm`, but now it's fully encapsulates in `rewrite_guard`.
1 parent 735efa6 commit fbcc07a

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/matches.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,7 @@ fn rewrite_match_arm(
251251
let pats_str = arm.pat.rewrite(context, pat_shape)?;
252252

253253
// Guard
254-
let block_like_pat = trimmed_last_line_width(&pats_str) <= context.config.tab_spaces();
255-
let new_line_guard = pats_str.contains('\n') && !block_like_pat;
256-
let guard_str = rewrite_guard(
257-
context,
258-
&arm.guard,
259-
shape,
260-
trimmed_last_line_width(&pats_str),
261-
new_line_guard,
262-
)?;
254+
let guard_str = rewrite_guard(context, &arm.guard, shape, &pats_str)?;
263255

264256
let lhs_str = combine_strs_with_missing_comments(
265257
context,
@@ -512,8 +504,26 @@ fn rewrite_match_body(
512504
}
513505
}
514506

507+
pub(crate) fn rewrite_guard(
508+
context: &RewriteContext<'_>,
509+
guard: &Option<ptr::P<ast::Expr>>,
510+
shape: Shape,
511+
pattern_str: &str,
512+
) -> Option<String> {
513+
let last_line_pattern_width = trimmed_last_line_width(pattern_str);
514+
let block_like_pat = last_line_pattern_width <= context.config.tab_spaces();
515+
let new_line_guard = pattern_str.contains('\n') && !block_like_pat;
516+
rewrite_guard_inner(
517+
context,
518+
guard,
519+
shape,
520+
last_line_pattern_width,
521+
new_line_guard,
522+
)
523+
}
524+
515525
// The `if ...` guard on a match arm.
516-
fn rewrite_guard(
526+
fn rewrite_guard_inner(
517527
context: &RewriteContext<'_>,
518528
guard: &Option<ptr::P<ast::Expr>>,
519529
shape: Shape,

0 commit comments

Comments
 (0)