Skip to content

Commit f720469

Browse files
committed
Use matches macro in libcore and libstd
1 parent aa0769b commit f720469

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
@@ -4283,10 +4283,7 @@ impl u8 {
42834283
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
42844284
#[inline]
42854285
pub fn is_ascii_alphabetic(&self) -> bool {
4286-
match *self {
4287-
b'A'..=b'Z' | b'a'..=b'z' => true,
4288-
_ => false,
4289-
}
4286+
matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
42904287
}
42914288

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

43274321
/// Checks if the value is an ASCII lowercase character:
@@ -4353,10 +4347,7 @@ impl u8 {
43534347
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
43544348
#[inline]
43554349
pub fn is_ascii_lowercase(&self) -> bool {
4356-
match *self {
4357-
b'a'..=b'z' => true,
4358-
_ => false,
4359-
}
4350+
matches!(*self, b'a'..=b'z')
43604351
}
43614352

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

44004388
/// Checks if the value is an ASCII decimal digit:
@@ -4426,10 +4414,7 @@ impl u8 {
44264414
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
44274415
#[inline]
44284416
pub fn is_ascii_digit(&self) -> bool {
4429-
match *self {
4430-
b'0'..=b'9' => true,
4431-
_ => false,
4432-
}
4417+
matches!(*self, b'0'..=b'9')
44334418
}
44344419

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

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

45124491
/// Checks if the value is an ASCII graphic character:
@@ -4538,10 +4517,7 @@ impl u8 {
45384517
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
45394518
#[inline]
45404519
pub fn is_ascii_graphic(&self) -> bool {
4541-
match *self {
4542-
b'!'..=b'~' => true,
4543-
_ => false,
4544-
}
4520+
matches!(*self, b'!'..=b'~')
45454521
}
45464522

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

45994572
/// Checks if the value is an ASCII control character:
@@ -4627,10 +4600,7 @@ impl u8 {
46274600
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
46284601
#[inline]
46294602
pub fn is_ascii_control(&self) -> bool {
4630-
match *self {
4631-
b'\0'..=b'\x1F' | b'\x7F' => true,
4632-
_ => false,
4633-
}
4603+
matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
46344604
}
46354605
}
46364606

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)