Skip to content

Commit 27f2120

Browse files
feat: support half_open_range syntax
1 parent b9138e0 commit 27f2120

File tree

6 files changed

+84
-20
lines changed

6 files changed

+84
-20
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ impl Rewrite for ast::Expr {
4242
}
4343
}
4444

45+
impl Rewrite for Option<ptr::P<ast::Expr>> {
46+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
47+
match &self {
48+
None => Some("".to_owned()),
49+
Some(ref exp) => {
50+
exp.rewrite(context, shape)
51+
}
52+
}
53+
}
54+
}
55+
4556
#[derive(Copy, Clone, PartialEq)]
4657
pub(crate) enum ExprType {
4758
Statement,

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

+25-20
Original file line numberDiff line numberDiff line change
@@ -179,28 +179,33 @@ impl Rewrite for Pat {
179179
None
180180
}
181181
}
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 => "..",
182+
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
183+
let infix = match end_kind.node {
184+
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
185+
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
186+
RangeEnd::Excluded => "..",
187+
};
188+
let infix = if context.config.spaces_around_ranges() {
189+
let lhs_spacing = match lhs {
190+
None => "",
191+
Some(_) => " ",
188192
};
189-
let infix = if context.config.spaces_around_ranges() {
190-
format!(" {} ", infix)
191-
} else {
192-
infix.to_owned()
193+
let rhs_spacing = match rhs {
194+
None => "",
195+
Some(_) => " ",
193196
};
194-
rewrite_pair(
195-
&**lhs,
196-
&**rhs,
197-
PairParts::infix(&infix),
198-
context,
199-
shape,
200-
SeparatorPlace::Front,
201-
)
202-
},
203-
(_, _) => unimplemented!(),
197+
format!("{}{}{}", lhs_spacing, infix, rhs_spacing)
198+
} else {
199+
infix.to_owned()
200+
};
201+
rewrite_pair(
202+
lhs,
203+
rhs,
204+
PairParts::infix(&infix),
205+
context,
206+
shape,
207+
SeparatorPlace::Front,
208+
)
204209
}
205210
PatKind::Ref(ref pat, mutability) => {
206211
let prefix = format!("&{}", format_mutability(mutability));

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)