Skip to content

Commit 3526f32

Browse files
support formatting half open ranges (#4044)
1 parent 19abe8d commit 3526f32

File tree

6 files changed

+87
-26
lines changed

6 files changed

+87
-26
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,8 @@ impl<'a> ControlFlow<'a> {
833833
.span_after(self.span, self.connector.trim());
834834
let comments_span = mk_sp(comments_lo, expr.span.lo());
835835

836-
let missing_comments = match rewrite_missing_comment(
837-
comments_span,
838-
cond_shape,
839-
context,
840-
) {
836+
let missing_comments = match rewrite_missing_comment(comments_span, cond_shape, context)
837+
{
841838
None => "".to_owned(),
842839
Some(comment) if self.connector.is_empty() || comment.is_empty() => comment,
843840
// Handle same-line block comments:

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

+37-21
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ 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) => exp.rewrite(context, shape),
65+
}
66+
}
67+
}
68+
5869
impl Rewrite for Pat {
5970
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
6071
match self.kind {
@@ -179,29 +190,34 @@ impl Rewrite for Pat {
179190
None
180191
}
181192
}
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 => "..",
193+
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
194+
let infix = match end_kind.node {
195+
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
196+
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
197+
RangeEnd::Excluded => "..",
198+
};
199+
let infix = if context.config.spaces_around_ranges() {
200+
let lhs_spacing = match lhs {
201+
None => "",
202+
Some(_) => " ",
188203
};
189-
let infix = if context.config.spaces_around_ranges() {
190-
format!(" {} ", infix)
191-
} else {
192-
infix.to_owned()
204+
let rhs_spacing = match rhs {
205+
None => "",
206+
Some(_) => " ",
193207
};
194-
rewrite_pair(
195-
&**lhs,
196-
&**rhs,
197-
PairParts::infix(&infix),
198-
context,
199-
shape,
200-
SeparatorPlace::Front,
201-
)
202-
}
203-
(_, _) => unimplemented!(),
204-
},
208+
format!("{}{}{}", lhs_spacing, infix, rhs_spacing)
209+
} else {
210+
infix.to_owned()
211+
};
212+
rewrite_pair(
213+
&RangeOperand(lhs),
214+
&RangeOperand(rhs),
215+
PairParts::infix(&infix),
216+
context,
217+
shape,
218+
SeparatorPlace::Front,
219+
)
220+
}
205221
PatKind::Ref(ref pat, mutability) => {
206222
let prefix = format!("&{}", format_mutability(mutability));
207223
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)