Skip to content

Commit 5ea6978

Browse files
authoredJan 8, 2020
Rollup merge of rust-lang#67966 - popzxc:core-std-matches, r=Centril
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
2 parents 11f0013 + f720469 commit 5ea6978

File tree

12 files changed

+29
-118
lines changed

12 files changed

+29
-118
lines changed
 

‎src/libcore/cmp.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
821821
#[must_use]
822822
#[stable(feature = "rust1", since = "1.0.0")]
823823
fn lt(&self, other: &Rhs) -> bool {
824-
match self.partial_cmp(other) {
825-
Some(Less) => true,
826-
_ => false,
827-
}
824+
matches!(self.partial_cmp(other), Some(Less))
828825
}
829826

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

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

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

‎src/libcore/iter/traits/iterator.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -2968,10 +2968,7 @@ pub trait Iterator {
29682968
Self::Item: PartialOrd<I::Item>,
29692969
Self: Sized,
29702970
{
2971-
match self.partial_cmp(other) {
2972-
Some(Ordering::Less) | Some(Ordering::Equal) => true,
2973-
_ => false,
2974-
}
2971+
matches!(self.partial_cmp(other), Some(Ordering::Less) | Some(Ordering::Equal))
29752972
}
29762973

29772974
/// Determines if the elements of this `Iterator` are lexicographically
@@ -3011,10 +3008,7 @@ pub trait Iterator {
30113008
Self::Item: PartialOrd<I::Item>,
30123009
Self: Sized,
30133010
{
3014-
match self.partial_cmp(other) {
3015-
Some(Ordering::Greater) | Some(Ordering::Equal) => true,
3016-
_ => false,
3017-
}
3011+
matches!(self.partial_cmp(other), Some(Ordering::Greater) | Some(Ordering::Equal))
30183012
}
30193013

30203014
/// Checks if the elements of this iterator are sorted.

‎src/libcore/num/mod.rs

+10-40
Original file line numberDiff line numberDiff line change
@@ -4286,10 +4286,7 @@ impl u8 {
42864286
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
42874287
#[inline]
42884288
pub fn is_ascii_alphabetic(&self) -> bool {
4289-
match *self {
4290-
b'A'..=b'Z' | b'a'..=b'z' => true,
4291-
_ => false,
4292-
}
4289+
matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
42934290
}
42944291

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

43304324
/// Checks if the value is an ASCII lowercase character:
@@ -4356,10 +4350,7 @@ impl u8 {
43564350
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
43574351
#[inline]
43584352
pub fn is_ascii_lowercase(&self) -> bool {
4359-
match *self {
4360-
b'a'..=b'z' => true,
4361-
_ => false,
4362-
}
4353+
matches!(*self, b'a'..=b'z')
43634354
}
43644355

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

44034391
/// Checks if the value is an ASCII decimal digit:
@@ -4429,10 +4417,7 @@ impl u8 {
44294417
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
44304418
#[inline]
44314419
pub fn is_ascii_digit(&self) -> bool {
4432-
match *self {
4433-
b'0'..=b'9' => true,
4434-
_ => false,
4435-
}
4420+
matches!(*self, b'0'..=b'9')
44364421
}
44374422

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

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

45154494
/// Checks if the value is an ASCII graphic character:
@@ -4541,10 +4520,7 @@ impl u8 {
45414520
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
45424521
#[inline]
45434522
pub fn is_ascii_graphic(&self) -> bool {
4544-
match *self {
4545-
b'!'..=b'~' => true,
4546-
_ => false,
4547-
}
4523+
matches!(*self, b'!'..=b'~')
45484524
}
45494525

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

46024575
/// Checks if the value is an ASCII control character:
@@ -4630,10 +4603,7 @@ impl u8 {
46304603
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
46314604
#[inline]
46324605
pub fn is_ascii_control(&self) -> bool {
4633-
match *self {
4634-
b'\0'..=b'\x1F' | b'\x7F' => true,
4635-
_ => false,
4636-
}
4606+
matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
46374607
}
46384608
}
46394609

‎src/libcore/option.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ impl<T> Option<T> {
187187
#[inline]
188188
#[stable(feature = "rust1", since = "1.0.0")]
189189
pub fn is_some(&self) -> bool {
190-
match *self {
191-
Some(_) => true,
192-
None => false,
193-
}
190+
matches!(*self, Some(_))
194191
}
195192

196193
/// Returns `true` if the option is a [`None`] value.

‎src/libcore/result.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,7 @@ impl<T, E> Result<T, E> {
282282
#[inline]
283283
#[stable(feature = "rust1", since = "1.0.0")]
284284
pub const fn is_ok(&self) -> bool {
285-
match *self {
286-
Ok(_) => true,
287-
Err(_) => false,
288-
}
285+
matches!(*self, Ok(_))
289286
}
290287

291288
/// Returns `true` if the result is [`Err`].

‎src/libcore/str/pattern.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ pub trait Pattern<'a>: Sized {
4646
/// Checks whether the pattern matches at the front of the haystack
4747
#[inline]
4848
fn is_prefix_of(self, haystack: &'a str) -> bool {
49-
match self.into_searcher(haystack).next() {
50-
SearchStep::Match(0, _) => true,
51-
_ => false,
52-
}
49+
matches!(self.into_searcher(haystack).next(), SearchStep::Match(0, _))
5350
}
5451

5552
/// Checks whether the pattern matches at the back of the haystack
@@ -58,10 +55,7 @@ pub trait Pattern<'a>: Sized {
5855
where
5956
Self::Searcher: ReverseSearcher<'a>,
6057
{
61-
match self.into_searcher(haystack).next_back() {
62-
SearchStep::Match(_, j) if haystack.len() == j => true,
63-
_ => false,
64-
}
58+
matches!(self.into_searcher(haystack).next_back(), SearchStep::Match(_, j) if haystack.len() == j)
6559
}
6660
}
6761

‎src/libcore/task/poll.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ impl<T> Poll<T> {
3939
#[inline]
4040
#[stable(feature = "futures_api", since = "1.36.0")]
4141
pub fn is_ready(&self) -> bool {
42-
match *self {
43-
Poll::Ready(_) => true,
44-
Poll::Pending => false,
45-
}
42+
matches!(*self, Poll::Ready(_))
4643
}
4744

4845
/// Returns `true` if this is `Poll::Pending`

‎src/libstd/net/addr.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,7 @@ impl SocketAddr {
227227
/// ```
228228
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
229229
pub fn is_ipv4(&self) -> bool {
230-
match *self {
231-
SocketAddr::V4(_) => true,
232-
SocketAddr::V6(_) => false,
233-
}
230+
matches!(*self, SocketAddr::V4(_))
234231
}
235232

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

‎src/libstd/net/ip.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,7 @@ impl IpAddr {
281281
/// ```
282282
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
283283
pub fn is_ipv4(&self) -> bool {
284-
match self {
285-
IpAddr::V4(_) => true,
286-
IpAddr::V6(_) => false,
287-
}
284+
matches!(self, IpAddr::V4(_))
288285
}
289286

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

‎src/libstd/path.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,12 @@ impl<'a> Prefix<'a> {
224224
#[stable(feature = "rust1", since = "1.0.0")]
225225
pub fn is_verbatim(&self) -> bool {
226226
use self::Prefix::*;
227-
match *self {
228-
Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..) => true,
229-
_ => false,
230-
}
227+
matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
231228
}
232229

233230
#[inline]
234231
fn is_drive(&self) -> bool {
235-
match *self {
236-
Prefix::Disk(_) => true,
237-
_ => false,
238-
}
232+
matches!(*self, Prefix::Disk(_))
239233
}
240234

241235
#[inline]

‎src/libstd/sync/barrier.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ mod tests {
199199

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

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

‎src/libstd/sync/mpsc/oneshot.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ impl<T> Packet<T> {
118118
// Just tests whether this channel has been sent on or not, this is only
119119
// safe to use from the sender.
120120
pub fn sent(&self) -> bool {
121-
unsafe {
122-
match *self.upgrade.get() {
123-
NothingSent => false,
124-
_ => true,
125-
}
126-
}
121+
unsafe { !matches!(*self.upgrade.get(), NothingSent) }
127122
}
128123

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

0 commit comments

Comments
 (0)
Please sign in to comment.