Skip to content

Commit

Permalink
Rollup merge of rust-lang#67966 - popzxc:core-std-matches, r=Centril
Browse files Browse the repository at this point in the history
Use matches macro in libcore and libstd

This PR replaces matches like

```rust
match var {
    value => true,
    _ => false,
}
```

with use of `matches!` macro.

r? @Centril
  • Loading branch information
Centril committed Jan 8, 2020
2 parents 11f0013 + f720469 commit 5ea6978
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 118 deletions.
20 changes: 4 additions & 16 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn lt(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) {
Some(Less) => true,
_ => false,
}
matches!(self.partial_cmp(other), Some(Less))
}

/// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
Expand All @@ -843,10 +840,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn le(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) {
Some(Less) | Some(Equal) => true,
_ => false,
}
matches!(self.partial_cmp(other), Some(Less) | Some(Equal))
}

/// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
Expand All @@ -864,10 +858,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn gt(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) {
Some(Greater) => true,
_ => false,
}
matches!(self.partial_cmp(other), Some(Greater))
}

/// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
Expand All @@ -886,10 +877,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn ge(&self, other: &Rhs) -> bool {
match self.partial_cmp(other) {
Some(Greater) | Some(Equal) => true,
_ => false,
}
matches!(self.partial_cmp(other), Some(Greater) | Some(Equal))
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/libcore/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2968,10 +2968,7 @@ pub trait Iterator {
Self::Item: PartialOrd<I::Item>,
Self: Sized,
{
match self.partial_cmp(other) {
Some(Ordering::Less) | Some(Ordering::Equal) => true,
_ => false,
}
matches!(self.partial_cmp(other), Some(Ordering::Less) | Some(Ordering::Equal))
}

/// Determines if the elements of this `Iterator` are lexicographically
Expand Down Expand Up @@ -3011,10 +3008,7 @@ pub trait Iterator {
Self::Item: PartialOrd<I::Item>,
Self: Sized,
{
match self.partial_cmp(other) {
Some(Ordering::Greater) | Some(Ordering::Equal) => true,
_ => false,
}
matches!(self.partial_cmp(other), Some(Ordering::Greater) | Some(Ordering::Equal))
}

/// Checks if the elements of this iterator are sorted.
Expand Down
50 changes: 10 additions & 40 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4286,10 +4286,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_alphabetic(&self) -> bool {
match *self {
b'A'..=b'Z' | b'a'..=b'z' => true,
_ => false,
}
matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
}

/// Checks if the value is an ASCII uppercase character:
Expand Down Expand Up @@ -4321,10 +4318,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_uppercase(&self) -> bool {
match *self {
b'A'..=b'Z' => true,
_ => false,
}
matches!(*self, b'A'..=b'Z')
}

/// Checks if the value is an ASCII lowercase character:
Expand Down Expand Up @@ -4356,10 +4350,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_lowercase(&self) -> bool {
match *self {
b'a'..=b'z' => true,
_ => false,
}
matches!(*self, b'a'..=b'z')
}

/// Checks if the value is an ASCII alphanumeric character:
Expand Down Expand Up @@ -4394,10 +4385,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_alphanumeric(&self) -> bool {
match *self {
b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' => true,
_ => false,
}
matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
}

/// Checks if the value is an ASCII decimal digit:
Expand Down Expand Up @@ -4429,10 +4417,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_digit(&self) -> bool {
match *self {
b'0'..=b'9' => true,
_ => false,
}
matches!(*self, b'0'..=b'9')
}

/// Checks if the value is an ASCII hexadecimal digit:
Expand Down Expand Up @@ -4467,10 +4452,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_hexdigit(&self) -> bool {
match *self {
b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f' => true,
_ => false,
}
matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
}

/// Checks if the value is an ASCII punctuation character:
Expand Down Expand Up @@ -4506,10 +4488,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_punctuation(&self) -> bool {
match *self {
b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~' => true,
_ => false,
}
matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
}

/// Checks if the value is an ASCII graphic character:
Expand Down Expand Up @@ -4541,10 +4520,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_graphic(&self) -> bool {
match *self {
b'!'..=b'~' => true,
_ => false,
}
matches!(*self, b'!'..=b'~')
}

/// Checks if the value is an ASCII whitespace character:
Expand Down Expand Up @@ -4593,10 +4569,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_whitespace(&self) -> bool {
match *self {
b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' => true,
_ => false,
}
matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
}

/// Checks if the value is an ASCII control character:
Expand Down Expand Up @@ -4630,10 +4603,7 @@ impl u8 {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_control(&self) -> bool {
match *self {
b'\0'..=b'\x1F' | b'\x7F' => true,
_ => false,
}
matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ impl<T> Option<T> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_some(&self) -> bool {
match *self {
Some(_) => true,
None => false,
}
matches!(*self, Some(_))
}

/// Returns `true` if the option is a [`None`] value.
Expand Down
5 changes: 1 addition & 4 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,7 @@ impl<T, E> Result<T, E> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn is_ok(&self) -> bool {
match *self {
Ok(_) => true,
Err(_) => false,
}
matches!(*self, Ok(_))
}

/// Returns `true` if the result is [`Err`].
Expand Down
10 changes: 2 additions & 8 deletions src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ pub trait Pattern<'a>: Sized {
/// Checks whether the pattern matches at the front of the haystack
#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
match self.into_searcher(haystack).next() {
SearchStep::Match(0, _) => true,
_ => false,
}
matches!(self.into_searcher(haystack).next(), SearchStep::Match(0, _))
}

/// Checks whether the pattern matches at the back of the haystack
Expand All @@ -58,10 +55,7 @@ pub trait Pattern<'a>: Sized {
where
Self::Searcher: ReverseSearcher<'a>,
{
match self.into_searcher(haystack).next_back() {
SearchStep::Match(_, j) if haystack.len() == j => true,
_ => false,
}
matches!(self.into_searcher(haystack).next_back(), SearchStep::Match(_, j) if haystack.len() == j)
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/libcore/task/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ impl<T> Poll<T> {
#[inline]
#[stable(feature = "futures_api", since = "1.36.0")]
pub fn is_ready(&self) -> bool {
match *self {
Poll::Ready(_) => true,
Poll::Pending => false,
}
matches!(*self, Poll::Ready(_))
}

/// Returns `true` if this is `Poll::Pending`
Expand Down
10 changes: 2 additions & 8 deletions src/libstd/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ impl SocketAddr {
/// ```
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
pub fn is_ipv4(&self) -> bool {
match *self {
SocketAddr::V4(_) => true,
SocketAddr::V6(_) => false,
}
matches!(*self, SocketAddr::V4(_))
}

/// Returns [`true`] if the [IP address] in this `SocketAddr` is an
Expand All @@ -252,10 +249,7 @@ impl SocketAddr {
/// ```
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
pub fn is_ipv6(&self) -> bool {
match *self {
SocketAddr::V4(_) => false,
SocketAddr::V6(_) => true,
}
matches!(*self, SocketAddr::V6(_))
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,7 @@ impl IpAddr {
/// ```
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv4(&self) -> bool {
match self {
IpAddr::V4(_) => true,
IpAddr::V6(_) => false,
}
matches!(self, IpAddr::V4(_))
}

/// Returns [`true`] if this address is an [IPv6 address], and [`false`] otherwise.
Expand All @@ -303,10 +300,7 @@ impl IpAddr {
/// ```
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv6(&self) -> bool {
match self {
IpAddr::V4(_) => false,
IpAddr::V6(_) => true,
}
matches!(self, IpAddr::V6(_))
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,12 @@ impl<'a> Prefix<'a> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_verbatim(&self) -> bool {
use self::Prefix::*;
match *self {
Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..) => true,
_ => false,
}
matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
}

#[inline]
fn is_drive(&self) -> bool {
match *self {
Prefix::Disk(_) => true,
_ => false,
}
matches!(*self, Prefix::Disk(_))
}

#[inline]
Expand Down
5 changes: 1 addition & 4 deletions src/libstd/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ mod tests {

// At this point, all spawned threads should be blocked,
// so we shouldn't get anything from the port
assert!(match rx.try_recv() {
Err(TryRecvError::Empty) => true,
_ => false,
});
assert!(matches!(rx.try_recv(), Err(TryRecvError::Empty)));

let mut leader_found = barrier.wait().is_leader();

Expand Down
7 changes: 1 addition & 6 deletions src/libstd/sync/mpsc/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ impl<T> Packet<T> {
// Just tests whether this channel has been sent on or not, this is only
// safe to use from the sender.
pub fn sent(&self) -> bool {
unsafe {
match *self.upgrade.get() {
NothingSent => false,
_ => true,
}
}
unsafe { !matches!(*self.upgrade.get(), NothingSent) }
}

pub fn recv(&self, deadline: Option<Instant>) -> Result<T, Failure<T>> {
Expand Down

0 comments on commit 5ea6978

Please sign in to comment.