Skip to content

Commit a6ff925

Browse files
committed
Reduce boilerplate with the matches! macro
Replaces simple bool `match`es of the form match $expr { $pattern => true _ => false } and their inverse with invocations of the matches! macro.
1 parent 0f9f0b3 commit a6ff925

File tree

13 files changed

+140
-269
lines changed

13 files changed

+140
-269
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,15 @@ impl<'hir> Map<'hir> {
535535
Some(Node::Binding(_)) => (),
536536
_ => return false,
537537
}
538-
match self.find(self.get_parent_node(id)) {
538+
matches!(
539+
self.find(self.get_parent_node(id)),
539540
Some(
540541
Node::Item(_)
541542
| Node::TraitItem(_)
542543
| Node::ImplItem(_)
543544
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. }),
544-
) => true,
545-
_ => false,
546-
}
545+
)
546+
)
547547
}
548548

549549
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
@@ -554,10 +554,10 @@ impl<'hir> Map<'hir> {
554554

555555
/// Whether `hir_id` corresponds to a `mod` or a crate.
556556
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
557-
match self.get_entry(hir_id).node {
558-
Node::Item(Item { kind: ItemKind::Mod(_), .. }) | Node::Crate(..) => true,
559-
_ => false,
560-
}
557+
matches!(
558+
self.get_entry(hir_id).node,
559+
Node::Item(Item { kind: ItemKind::Mod(_), .. }) | Node::Crate(..)
560+
)
561561
}
562562

563563
/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,10 @@ impl<'tcx> TyCtxt<'tcx> {
486486
// `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
487487
// However, formatting code relies on function identity (see #58320), so we only do
488488
// this for generic functions. Lifetime parameters are ignored.
489-
let is_generic = instance.substs.into_iter().any(|kind| match kind.unpack() {
490-
GenericArgKind::Lifetime(_) => false,
491-
_ => true,
492-
});
489+
let is_generic = instance
490+
.substs
491+
.into_iter()
492+
.any(|kind| !matches!(kind.unpack(), GenericArgKind::Lifetime(_)));
493493
if is_generic {
494494
// Get a fresh ID.
495495
let mut alloc_map = self.alloc_map.lock();

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,19 +445,13 @@ impl<'tcx, Tag> Scalar<Tag> {
445445
/// Do not call this method! Dispatch based on the type instead.
446446
#[inline]
447447
pub fn is_bits(self) -> bool {
448-
match self {
449-
Scalar::Raw { .. } => true,
450-
_ => false,
451-
}
448+
matches!(self, Scalar::Raw { .. })
452449
}
453450

454451
/// Do not call this method! Dispatch based on the type instead.
455452
#[inline]
456453
pub fn is_ptr(self) -> bool {
457-
match self {
458-
Scalar::Ptr(_) => true,
459-
_ => false,
460-
}
454+
matches!(self, Scalar::Ptr(_))
461455
}
462456

463457
pub fn to_bool(self) -> InterpResult<'tcx, bool> {

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -935,67 +935,59 @@ impl<'tcx> LocalDecl<'tcx> {
935935
/// - `let x = ...`,
936936
/// - or `match ... { C(x) => ... }`
937937
pub fn can_be_made_mutable(&self) -> bool {
938-
match self.local_info {
939-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
940-
binding_mode: ty::BindingMode::BindByValue(_),
941-
opt_ty_info: _,
942-
opt_match_place: _,
943-
pat_span: _,
944-
})))) => true,
945-
946-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
947-
ImplicitSelfKind::Imm,
948-
)))) => true,
949-
950-
_ => false,
951-
}
938+
matches!(
939+
self.local_info,
940+
Some(box LocalInfo::User(ClearCrossCrate::Set(
941+
BindingForm::Var(VarBindingForm {
942+
binding_mode: ty::BindingMode::BindByValue(_),
943+
opt_ty_info: _,
944+
opt_match_place: _,
945+
pat_span: _,
946+
})
947+
| BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
948+
)))
949+
)
952950
}
953951

954952
/// Returns `true` if local is definitely not a `ref ident` or
955953
/// `ref mut ident` binding. (Such bindings cannot be made into
956954
/// mutable bindings, but the inverse does not necessarily hold).
957955
pub fn is_nonref_binding(&self) -> bool {
958-
match self.local_info {
959-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
960-
binding_mode: ty::BindingMode::BindByValue(_),
961-
opt_ty_info: _,
962-
opt_match_place: _,
963-
pat_span: _,
964-
})))) => true,
965-
966-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(_)))) => true,
967-
968-
_ => false,
969-
}
956+
matches!(
957+
self.local_info,
958+
Some(box LocalInfo::User(ClearCrossCrate::Set(
959+
BindingForm::Var(VarBindingForm {
960+
binding_mode: ty::BindingMode::BindByValue(_),
961+
opt_ty_info: _,
962+
opt_match_place: _,
963+
pat_span: _,
964+
})
965+
| BindingForm::ImplicitSelf(_),
966+
)))
967+
)
970968
}
971969

972970
/// Returns `true` if this variable is a named variable or function
973971
/// parameter declared by the user.
974972
#[inline]
975973
pub fn is_user_variable(&self) -> bool {
976-
match self.local_info {
977-
Some(box LocalInfo::User(_)) => true,
978-
_ => false,
979-
}
974+
matches!(self.local_info, Some(box LocalInfo::User(_)))
980975
}
981976

982977
/// Returns `true` if this is a reference to a variable bound in a `match`
983978
/// expression that is used to access said variable for the guard of the
984979
/// match arm.
985980
pub fn is_ref_for_guard(&self) -> bool {
986-
match self.local_info {
987-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard))) => true,
988-
_ => false,
989-
}
981+
matches!(
982+
self.local_info,
983+
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
984+
)
990985
}
991986

992987
/// Returns `Some` if this is a reference to a static item that is used to
993988
/// access that static
994989
pub fn is_ref_to_static(&self) -> bool {
995-
match self.local_info {
996-
Some(box LocalInfo::StaticRef { .. }) => true,
997-
_ => false,
998-
}
990+
matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
999991
}
1000992

1001993
/// Returns `Some` if this is a reference to a static item that is used to
@@ -2124,10 +2116,7 @@ pub enum BinOp {
21242116
impl BinOp {
21252117
pub fn is_checkable(self) -> bool {
21262118
use self::BinOp::*;
2127-
match self {
2128-
Add | Sub | Mul | Shl | Shr => true,
2129-
_ => false,
2130-
}
2119+
matches!(self, Add | Sub | Mul | Shl | Shr)
21312120
}
21322121
}
21332122

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,82 +1164,63 @@ pub enum PlaceContext {
11641164
impl PlaceContext {
11651165
/// Returns `true` if this place context represents a drop.
11661166
pub fn is_drop(&self) -> bool {
1167-
match *self {
1168-
PlaceContext::MutatingUse(MutatingUseContext::Drop) => true,
1169-
_ => false,
1170-
}
1167+
matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
11711168
}
11721169

11731170
/// Returns `true` if this place context represents a borrow.
11741171
pub fn is_borrow(&self) -> bool {
1175-
match *self {
1172+
matches!(
1173+
self,
11761174
PlaceContext::NonMutatingUse(
11771175
NonMutatingUseContext::SharedBorrow
1178-
| NonMutatingUseContext::ShallowBorrow
1179-
| NonMutatingUseContext::UniqueBorrow,
1180-
)
1181-
| PlaceContext::MutatingUse(MutatingUseContext::Borrow) => true,
1182-
_ => false,
1183-
}
1176+
| NonMutatingUseContext::ShallowBorrow
1177+
| NonMutatingUseContext::UniqueBorrow
1178+
) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
1179+
)
11841180
}
11851181

11861182
/// Returns `true` if this place context represents a storage live or storage dead marker.
11871183
pub fn is_storage_marker(&self) -> bool {
1188-
match *self {
1189-
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) => true,
1190-
_ => false,
1191-
}
1184+
matches!(
1185+
self,
1186+
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
1187+
)
11921188
}
11931189

11941190
/// Returns `true` if this place context represents a storage live marker.
11951191
pub fn is_storage_live_marker(&self) -> bool {
1196-
match *self {
1197-
PlaceContext::NonUse(NonUseContext::StorageLive) => true,
1198-
_ => false,
1199-
}
1192+
matches!(self, PlaceContext::NonUse(NonUseContext::StorageLive))
12001193
}
12011194

12021195
/// Returns `true` if this place context represents a storage dead marker.
12031196
pub fn is_storage_dead_marker(&self) -> bool {
1204-
match *self {
1205-
PlaceContext::NonUse(NonUseContext::StorageDead) => true,
1206-
_ => false,
1207-
}
1197+
matches!(self, PlaceContext::NonUse(NonUseContext::StorageDead))
12081198
}
12091199

12101200
/// Returns `true` if this place context represents a use that potentially changes the value.
12111201
pub fn is_mutating_use(&self) -> bool {
1212-
match *self {
1213-
PlaceContext::MutatingUse(..) => true,
1214-
_ => false,
1215-
}
1202+
matches!(self, PlaceContext::MutatingUse(..))
12161203
}
12171204

12181205
/// Returns `true` if this place context represents a use that does not change the value.
12191206
pub fn is_nonmutating_use(&self) -> bool {
1220-
match *self {
1221-
PlaceContext::NonMutatingUse(..) => true,
1222-
_ => false,
1223-
}
1207+
matches!(self, PlaceContext::NonMutatingUse(..))
12241208
}
12251209

12261210
/// Returns `true` if this place context represents a use.
12271211
pub fn is_use(&self) -> bool {
1228-
match *self {
1229-
PlaceContext::NonUse(..) => false,
1230-
_ => true,
1231-
}
1212+
!matches!(self, PlaceContext::NonUse(..))
12321213
}
12331214

12341215
/// Returns `true` if this place context represents an assignment statement.
12351216
pub fn is_place_assignment(&self) -> bool {
1236-
match *self {
1217+
matches!(
1218+
self,
12371219
PlaceContext::MutatingUse(
12381220
MutatingUseContext::Store
1239-
| MutatingUseContext::Call
1240-
| MutatingUseContext::AsmOutput,
1241-
) => true,
1242-
_ => false,
1243-
}
1221+
| MutatingUseContext::Call
1222+
| MutatingUseContext::AsmOutput,
1223+
)
1224+
)
12441225
}
12451226
}

compiler/rustc_middle/src/traits/specialization_graph.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ pub enum Node {
7979

8080
impl<'tcx> Node {
8181
pub fn is_from_trait(&self) -> bool {
82-
match *self {
83-
Node::Trait(..) => true,
84-
_ => false,
85-
}
82+
matches!(self, Node::Trait(..))
8683
}
8784

8885
/// Iterate over the items defined directly by the given (impl or trait) node.

compiler/rustc_middle/src/ty/adjustment.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ pub struct Adjustment<'tcx> {
8585

8686
impl Adjustment<'tcx> {
8787
pub fn is_region_borrow(&self) -> bool {
88-
match self.kind {
89-
Adjust::Borrow(AutoBorrow::Ref(..)) => true,
90-
_ => false,
91-
}
88+
matches!(self.kind, Adjust::Borrow(AutoBorrow::Ref(..)))
9289
}
9390
}
9491

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,7 @@ impl<'tcx> TypeckResults<'tcx> {
588588
return false;
589589
}
590590

591-
match self.type_dependent_defs().get(expr.hir_id) {
592-
Some(Ok((DefKind::AssocFn, _))) => true,
593-
_ => false,
594-
}
591+
matches!(self.type_dependent_defs().get(expr.hir_id), Some(Ok((DefKind::AssocFn, _))))
595592
}
596593

597594
pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> {

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ use rustc_hir::{QPath, TyKind, WhereBoundPredicate, WherePredicate};
1111
impl<'tcx> TyS<'tcx> {
1212
/// Similar to `TyS::is_primitive`, but also considers inferred numeric values to be primitive.
1313
pub fn is_primitive_ty(&self) -> bool {
14-
match self.kind() {
15-
Bool
16-
| Char
17-
| Str
18-
| Int(_)
19-
| Uint(_)
20-
| Float(_)
14+
matches!(
15+
self.kind(),
16+
Bool | Char | Str | Int(_) | Uint(_) | Float(_)
2117
| Infer(
2218
InferTy::IntVar(_)
2319
| InferTy::FloatVar(_)
2420
| InferTy::FreshIntTy(_)
25-
| InferTy::FreshFloatTy(_),
26-
) => true,
27-
_ => false,
28-
}
21+
| InferTy::FreshFloatTy(_)
22+
)
23+
)
2924
}
3025

3126
/// Whether the type is succinctly representable as a type instead of just referred to with a
@@ -64,11 +59,16 @@ impl<'tcx> TyS<'tcx> {
6459

6560
/// Whether the type can be safely suggested during error recovery.
6661
pub fn is_suggestable(&self) -> bool {
67-
match self.kind() {
68-
Opaque(..) | FnDef(..) | FnPtr(..) | Dynamic(..) | Closure(..) | Infer(..)
69-
| Projection(..) => false,
70-
_ => true,
71-
}
62+
!matches!(
63+
self.kind(),
64+
Opaque(..)
65+
| FnDef(..)
66+
| FnPtr(..)
67+
| Dynamic(..)
68+
| Closure(..)
69+
| Infer(..)
70+
| Projection(..)
71+
)
7272
}
7373
}
7474

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ impl<'tcx> InstanceDef<'tcx> {
183183
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
184184
_ => return true,
185185
};
186-
match tcx.def_key(def_id).disambiguated_data.data {
187-
DefPathData::Ctor | DefPathData::ClosureExpr => true,
188-
_ => false,
189-
}
186+
matches!(
187+
tcx.def_key(def_id).disambiguated_data.data,
188+
DefPathData::Ctor | DefPathData::ClosureExpr
189+
)
190190
}
191191

192192
/// Returns `true` if the machine code for this instance is instantiated in

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,10 +2628,7 @@ where
26282628
target.target_os == "linux" && target.arch == "sparc64" && target_env_gnu_like;
26292629
let linux_powerpc_gnu_like =
26302630
target.target_os == "linux" && target.arch == "powerpc" && target_env_gnu_like;
2631-
let rust_abi = match sig.abi {
2632-
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => true,
2633-
_ => false,
2634-
};
2631+
let rust_abi = matches!(sig.abi, RustIntrinsic | PlatformIntrinsic | Rust | RustCall);
26352632

26362633
// Handle safe Rust thin and fat pointers.
26372634
let adjust_for_rust_scalar = |attrs: &mut ArgAttributes,

0 commit comments

Comments
 (0)