]> git.lizzy.rs Git - rust.git/commit - src/tools/clippy
Auto merge of #50912 - varkor:exhaustive-integer-matching, r=arielb1
authorbors <bors@rust-lang.org>
Wed, 22 Aug 2018 00:57:00 +0000 (00:57 +0000)
committerbors <bors@rust-lang.org>
Wed, 22 Aug 2018 00:57:00 +0000 (00:57 +0000)
commita79cffb8b8330527d262bdde56387d45ac46dd44
tree449ba4ee567f5dff4b1c5f0d94be3203bae5a535
parent1cbf339626e52e1e66a6df3097051bb981bfa964
parent6971c5d55d06c02c8f0b943b3d0b06ef2611c8ac
Auto merge of #50912 - varkor:exhaustive-integer-matching, r=arielb1

Exhaustive integer matching

This adds a new feature flag `exhaustive_integer_patterns` that enables exhaustive matching of integer types by their values. For example, the following is now accepted:
```rust
#![feature(exhaustive_integer_patterns)]
#![feature(exclusive_range_pattern)]

fn matcher(x: u8) {
  match x { // ok
    0 .. 32 => { /* foo */ }
    32 => { /* bar */ }
    33 ..= 255 => { /* baz */ }
  }
}
```
This matching is permitted on all integer (signed/unsigned and char) types. Sensible error messages are also provided. For example:
```rust
fn matcher(x: u8) {
  match x { //~ ERROR
    0 .. 32 => { /* foo */ }
  }
}
```
results in:
```
error[E0004]: non-exhaustive patterns: `32u8...255u8` not covered
 --> matches.rs:3:9
  |
6 |   match x {
  |         ^ pattern `32u8...255u8` not covered
```

This implements https://github.com/rust-lang/rfcs/issues/1550 for https://github.com/rust-lang/rust/issues/50907. While there hasn't been a full RFC for this feature, it was suggested that this might be a feature that obviously complements the existing exhaustiveness checks (e.g. for `bool`) and so a feature gate would be sufficient for now.
src/librustc/mir/mod.rs
src/librustc_mir/lib.rs
src/libsyntax/feature_gate.rs