Merge pull request #2383 from davidalber/range-spaces-in-match

Extending `spaces_around_ranges` to ranges in match arm patterns
This commit is contained in:
Seiichi Uchida 2018-01-26 12:32:28 +09:00 committed by GitHub
commit dfae6a99c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 110 additions and 5 deletions

View File

@ -1468,7 +1468,7 @@ struct Foo {
## `spaces_around_ranges`
Put spaces around the .. and ... range operators
Put spaces around the .., ..=, and ... range operators
- **Default value**: `false`
- **Possible values**: `true`, `false`
@ -1477,13 +1477,49 @@ Put spaces around the .. and ... range operators
#### `false` (default):
```rust
let lorem = 0..10;
fn main() {
let lorem = 0..10;
let ipsum = 0..=10;
match lorem {
1..5 => foo(),
_ => bar,
}
match lorem {
1..=5 => foo(),
_ => bar,
}
match lorem {
1...5 => foo(),
_ => bar,
}
}
```
#### `true`:
```rust
let lorem = 0 .. 10;
fn main() {
let lorem = 0 .. 10;
let ipsum = 0 ..= 10;
match lorem {
1 .. 5 => foo(),
_ => bar,
}
match lorem {
1 ..= 5 => foo(),
_ => bar,
}
match lorem {
1 ... 5 => foo(),
_ => bar,
}
}
```
## `spaces_within_parens_and_brackets`

View File

@ -65,10 +65,15 @@ impl Rewrite for Pat {
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
RangeEnd::Excluded => "..",
};
let infix = if context.config.spaces_around_ranges() {
format!(" {} ", infix)
} else {
infix.to_owned()
};
rewrite_pair(
&**lhs,
&**rhs,
PairParts::new("", infix, ""),
PairParts::new("", &infix, ""),
context,
shape,
SeparatorPlace::Front,

View File

@ -2,5 +2,21 @@
// Spaces around ranges
fn main() {
let lorem = 0..10;
let lorem = 0 .. 10;
let ipsum = 0 ..= 10;
match lorem {
1 .. 5 => foo(),
_ => bar,
}
match lorem {
1 ..= 5 => foo(),
_ => bar,
}
match lorem {
1 ... 5 => foo(),
_ => bar,
}
}

View File

@ -3,4 +3,20 @@
fn main() {
let lorem = 0..10;
let ipsum = 0..=10;
match lorem {
1..5 => foo(),
_ => bar,
}
match lorem {
1..=5 => foo(),
_ => bar,
}
match lorem {
1...5 => foo(),
_ => bar,
}
}

View File

@ -3,4 +3,20 @@
fn main() {
let lorem = 0..10;
let ipsum = 0..=10;
match lorem {
1..5 => foo(),
_ => bar,
}
match lorem {
1..=5 => foo(),
_ => bar,
}
match lorem {
1...5 => foo(),
_ => bar,
}
}

View File

@ -3,4 +3,20 @@
fn main() {
let lorem = 0 .. 10;
let ipsum = 0 ..= 10;
match lorem {
1 .. 5 => foo(),
_ => bar,
}
match lorem {
1 ..= 5 => foo(),
_ => bar,
}
match lorem {
1 ... 5 => foo(),
_ => bar,
}
}