Skip to content

Commit 14da660

Browse files
refactor: new type for range operands
1 parent a4533eb commit 14da660

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

rustfmt-core/rustfmt-lib/src/patterns.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
5555
}
5656
}
5757

58+
struct RangeOperand<'a>(&'a Option<ptr::P<ast::Expr>>);
59+
60+
impl<'a> Rewrite for RangeOperand<'a> {
61+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
62+
match &self.0 {
63+
None => Some("".to_owned()),
64+
Some(ref exp) => {
65+
exp.rewrite(context, shape)
66+
}
67+
}
68+
}
69+
}
70+
5871
impl Rewrite for Pat {
5972
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
6073
match self.kind {
@@ -180,16 +193,6 @@ impl Rewrite for Pat {
180193
}
181194
}
182195
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
183-
impl Rewrite for Option<ptr::P<ast::Expr>> {
184-
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
185-
match &self {
186-
None => Some("".to_owned()),
187-
Some(ref exp) => {
188-
exp.rewrite(context, shape)
189-
}
190-
}
191-
}
192-
}
193196
let infix = match end_kind.node {
194197
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
195198
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
@@ -209,8 +212,8 @@ impl Rewrite for Pat {
209212
infix.to_owned()
210213
};
211214
rewrite_pair(
212-
lhs,
213-
rhs,
215+
&RangeOperand(lhs),
216+
&RangeOperand(rhs),
214217
PairParts::infix(&infix),
215218
context,
216219
shape,

0 commit comments

Comments
 (0)