Skip to content

Commit e16c56b

Browse files
authored
Merge pull request #2000 from Badel2/dotdoteq
Support for `..=` syntax
2 parents 22eb524 + feb7a6a commit e16c56b

File tree

6 files changed

+28
-36
lines changed

6 files changed

+28
-36
lines changed

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub fn format_expr(
232232
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
233233
let delim = match limits {
234234
ast::RangeLimits::HalfOpen => "..",
235-
ast::RangeLimits::Closed => "...",
235+
ast::RangeLimits::Closed => "..=",
236236
};
237237

238238
fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool {

src/patterns.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd};
11+
use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSyntax};
1212
use syntax::codemap::{self, BytePos, Span};
1313
use syntax::ptr;
1414

@@ -58,31 +58,23 @@ impl Rewrite for Pat {
5858
} else {
5959
None
6060
},
61-
PatKind::Range(ref lhs, ref rhs, ref end_kind) => match *end_kind {
62-
RangeEnd::Excluded => rewrite_pair(
63-
&**lhs,
64-
&**rhs,
65-
"",
66-
"..",
67-
"",
68-
context,
69-
shape,
70-
SeparatorPlace::Front,
71-
),
72-
// FIXME: Change _ to RangeEnd::Included(RangeSyntax::DotDotDot)
73-
// and add RangeEnd::Included(RangeSyntax::DotDotEq)
74-
// once rust PR #44709 gets merged
75-
_ => rewrite_pair(
61+
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
62+
let infix = match *end_kind {
63+
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
64+
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
65+
RangeEnd::Excluded => "..",
66+
};
67+
rewrite_pair(
7668
&**lhs,
7769
&**rhs,
7870
"",
79-
"...",
71+
infix,
8072
"",
8173
context,
8274
shape,
8375
SeparatorPlace::Front,
84-
),
85-
},
76+
)
77+
}
8678
PatKind::Ref(ref pat, mutability) => {
8779
let prefix = format!("&{}", format_mutability(mutability));
8880
rewrite_unary_prefix(context, &prefix, &**pat, shape)

tests/source/expr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,17 @@ fn issue767() {
250250

251251
fn ranges() {
252252
let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
253-
let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
254-
let z = ... x ;
253+
let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ..= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
254+
let z = ..= x ;
255255

256256
// #1766
257257
let x = [0. ..10.0];
258-
let x = [0. ...10.0];
258+
let x = [0. ..=10.0];
259259

260-
a ... b
260+
a ..= b
261261

262-
// the expr below won't compile for some reason...
263-
// let a = 0 ... ;
262+
// the expr below won't compile because inclusive ranges need a defined end
263+
// let a = 0 ..= ;
264264
}
265265

266266
fn if_else() {

tests/source/spaces-around-ranges.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ fn bar(v: &[u8]) {}
44

55
fn foo() {
66
let a = vec![0; 20];
7-
for j in 0...20 {
7+
for j in 0..=20 {
88
for i in 0..3 {
99
bar(a[i..j]);
1010
bar(a[i..]);
1111
bar(a[..j]);
12-
bar(a[...(j + 1)]);
12+
bar(a[..=(j + 1)]);
1313
}
1414
}
1515
}

tests/target/expr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,17 @@ fn issue767() {
316316
fn ranges() {
317317
let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
318318
let y =
319-
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
320-
let z = ...x;
319+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
320+
let z = ..=x;
321321

322322
// #1766
323323
let x = [0. ..10.0];
324-
let x = [0. ...10.0];
324+
let x = [0. ..=10.0];
325325

326-
a...b
326+
a..=b
327327

328-
// the expr below won't compile for some reason...
329-
// let a = 0 ... ;
328+
// the expr below won't compile because inclusive ranges need a defined end
329+
// let a = 0 ..= ;
330330
}
331331

332332
fn if_else() {

tests/target/spaces-around-ranges.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ fn bar(v: &[u8]) {}
44

55
fn foo() {
66
let a = vec![0; 20];
7-
for j in 0 ... 20 {
7+
for j in 0 ..= 20 {
88
for i in 0 .. 3 {
99
bar(a[i .. j]);
1010
bar(a[i ..]);
1111
bar(a[.. j]);
12-
bar(a[... (j + 1)]);
12+
bar(a[..= (j + 1)]);
1313
}
1414
}
1515
}

0 commit comments

Comments
 (0)