Skip to content

Commit 8c43b43

Browse files
feat: support half_open_range syntax
1 parent 19abe8d commit 8c43b43

File tree

5 files changed

+87
-21
lines changed

5 files changed

+87
-21
lines changed

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

+39-21
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 {
@@ -179,29 +192,34 @@ impl Rewrite for Pat {
179192
None
180193
}
181194
}
182-
PatKind::Range(ref lhs, ref rhs, ref end_kind) => match (lhs, rhs) {
183-
(Some(lhs), Some(rhs)) => {
184-
let infix = match end_kind.node {
185-
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
186-
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
187-
RangeEnd::Excluded => "..",
195+
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
196+
let infix = match end_kind.node {
197+
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
198+
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
199+
RangeEnd::Excluded => "..",
200+
};
201+
let infix = if context.config.spaces_around_ranges() {
202+
let lhs_spacing = match lhs {
203+
None => "",
204+
Some(_) => " ",
188205
};
189-
let infix = if context.config.spaces_around_ranges() {
190-
format!(" {} ", infix)
191-
} else {
192-
infix.to_owned()
206+
let rhs_spacing = match rhs {
207+
None => "",
208+
Some(_) => " ",
193209
};
194-
rewrite_pair(
195-
&**lhs,
196-
&**rhs,
197-
PairParts::infix(&infix),
198-
context,
199-
shape,
200-
SeparatorPlace::Front,
201-
)
202-
}
203-
(_, _) => unimplemented!(),
204-
},
210+
format!("{}{}{}", lhs_spacing, infix, rhs_spacing)
211+
} else {
212+
infix.to_owned()
213+
};
214+
rewrite_pair(
215+
&RangeOperand(lhs),
216+
&RangeOperand(rhs),
217+
PairParts::infix(&infix),
218+
context,
219+
shape,
220+
SeparatorPlace::Front,
221+
)
222+
}
205223
PatKind::Ref(ref pat, mutability) => {
206224
let prefix = format!("&{}", format_mutability(mutability));
207225
rewrite_unary_prefix(context, &prefix, &**pat, shape)

rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/false.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5 .. 4, 99 .. 105, 43 .. 44] {
26+
[_, 99 .., _] => {}
27+
[_, .. 105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..= 5 = 0 {}
32+
if let .. 5 = 0 {}
33+
if let 5 .. = 0 {}
34+
}

rustfmt-core/rustfmt-lib/tests/source/configs/spaces_around_ranges/true.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5..4, 99..105, 43..44] {
26+
[_, 99.., _] => {}
27+
[_, ..105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..=5 = 0 {}
32+
if let ..5 = 0 {}
33+
if let 5.. = 0 {}
34+
}

rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/false.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5..4, 99..105, 43..44] {
26+
[_, 99.., _] => {}
27+
[_, ..105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..=5 = 0 {}
32+
if let ..5 = 0 {}
33+
if let 5.. = 0 {}
34+
}

rustfmt-core/rustfmt-lib/tests/target/configs/spaces_around_ranges/true.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5 .. 4, 99 .. 105, 43 .. 44] {
26+
[_, 99 .., _] => {}
27+
[_, .. 105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..= 5 = 0 {}
32+
if let .. 5 = 0 {}
33+
if let 5 .. = 0 {}
34+
}

0 commit comments

Comments
 (0)