Skip to content

Commit 5b11d2e

Browse files
committed
Merge pull request #892 from marcusklaas/closed-ranges
Format closed ranges
2 parents 19768da + f941646 commit 5b11d2e

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

Cargo.lock

Lines changed: 11 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/expr.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -176,48 +176,48 @@ impl Rewrite for ast::Expr {
176176
ast::ExprKind::Cast(ref expr, ref ty) => {
177177
rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset)
178178
}
179-
// TODO(#848): Handle type ascription; rust tracking issue
180-
// https://github.com/rust-lang/rust/issues/23416
181-
ast::ExprKind::Type(_, _) => unimplemented!(),
182179
ast::ExprKind::Index(ref expr, ref index) => {
183180
rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset)
184181
}
185182
ast::ExprKind::Repeat(ref expr, ref repeats) => {
186183
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
187184
}
188-
// TODO(#890): Handle closed ranges; rust tracking issue
189-
// https://github.com/rust-lang/rust/issues/28237
190-
ast::ExprKind::Range(Some(ref lhs), Some(ref rhs), _range_limits) => {
191-
rewrite_pair(&**lhs, &**rhs, "", "..", "", context, width, offset)
192-
}
193-
ast::ExprKind::Range(None, Some(ref rhs), _range_limits) => {
194-
rewrite_unary_prefix(context, "..", &**rhs, width, offset)
195-
}
196-
ast::ExprKind::Range(Some(ref lhs), None, _range_limits) => {
197-
Some(format!("{}..",
198-
try_opt!(lhs.rewrite(context,
199-
try_opt!(width.checked_sub(2)),
200-
offset))))
201-
}
202-
ast::ExprKind::Range(None, None, _range_limits) => {
203-
if width >= 2 {
204-
Some("..".into())
205-
} else {
206-
None
185+
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
186+
let delim = match limits {
187+
ast::RangeLimits::HalfOpen => "..",
188+
ast::RangeLimits::Closed => "...",
189+
};
190+
191+
match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
192+
(Some(ref lhs), Some(ref rhs)) => {
193+
rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset)
194+
}
195+
(None, Some(ref rhs)) => {
196+
rewrite_unary_prefix(context, delim, &**rhs, width, offset)
197+
}
198+
(Some(ref lhs), None) => {
199+
Some(format!("{}{}",
200+
try_opt!(lhs.rewrite(context,
201+
try_opt!(width.checked_sub(delim.len())),
202+
offset)),
203+
delim))
204+
}
205+
(None, None) => wrap_str(delim.into(), context.config.max_width, width, offset),
207206
}
208207
}
209208
// We do not format these expressions yet, but they should still
210209
// satisfy our width restrictions.
211210
ast::ExprKind::InPlace(..) |
212-
ast::ExprKind::InlineAsm(..) => {
211+
ast::ExprKind::InlineAsm(..) |
212+
// TODO(#848): Handle type ascription
213+
ast::ExprKind::Type(_, _) |
214+
// TODO(#867): Handle try shorthand
215+
ast::ExprKind::Try(_) => {
213216
wrap_str(context.snippet(self.span),
214217
context.config.max_width,
215218
width,
216219
offset)
217220
}
218-
// TODO(#867): Handle type ascription; rust tracking issue
219-
// https://github.com/rust-lang/rust/issues/31436
220-
ast::ExprKind::Try(_) => unimplemented!(),
221221
};
222222
result.and_then(|res| recover_comment_removed(res, self.span, context, width, offset))
223223
}

tests/source/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,15 @@ fn issue767() {
244244
} else if let false = false {
245245
}
246246
}
247+
248+
fn ranges() {
249+
let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
250+
let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
251+
let z = ... x ;
252+
let infi_range_2 = ... ;
253+
254+
a ... b
255+
256+
// the expr below won't compile for some reason...
257+
// let a = 0 ... ;
258+
}

tests/target/expr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,16 @@ fn issue767() {
267267
} else if let false = false {
268268
}
269269
}
270+
271+
fn ranges() {
272+
let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
273+
let y =
274+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
275+
let z = ...x;
276+
let infi_range_2 = ...;
277+
278+
a...b
279+
280+
// the expr below won't compile for some reason...
281+
// let a = 0 ... ;
282+
}

0 commit comments

Comments
 (0)