Skip to content

Commit d50349b

Browse files
authored
Rollup merge of #76995 - LingMan:middle_matches, r=varkor
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. Limited to rustc_middle for now to get my feet wet.
2 parents 97ee62c + a6ff925 commit d50349b

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
@@ -971,67 +971,59 @@ impl<'tcx> LocalDecl<'tcx> {
971971
/// - `let x = ...`,
972972
/// - or `match ... { C(x) => ... }`
973973
pub fn can_be_made_mutable(&self) -> bool {
974-
match self.local_info {
975-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
976-
binding_mode: ty::BindingMode::BindByValue(_),
977-
opt_ty_info: _,
978-
opt_match_place: _,
979-
pat_span: _,
980-
})))) => true,
981-
982-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
983-
ImplicitSelfKind::Imm,
984-
)))) => true,
985-
986-
_ => false,
987-
}
974+
matches!(
975+
self.local_info,
976+
Some(box LocalInfo::User(ClearCrossCrate::Set(
977+
BindingForm::Var(VarBindingForm {
978+
binding_mode: ty::BindingMode::BindByValue(_),
979+
opt_ty_info: _,
980+
opt_match_place: _,
981+
pat_span: _,
982+
})
983+
| BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
984+
)))
985+
)
988986
}
989987

990988
/// Returns `true` if local is definitely not a `ref ident` or
991989
/// `ref mut ident` binding. (Such bindings cannot be made into
992990
/// mutable bindings, but the inverse does not necessarily hold).
993991
pub fn is_nonref_binding(&self) -> bool {
994-
match self.local_info {
995-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
996-
binding_mode: ty::BindingMode::BindByValue(_),
997-
opt_ty_info: _,
998-
opt_match_place: _,
999-
pat_span: _,
1000-
})))) => true,
1001-
1002-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(_)))) => true,
1003-
1004-
_ => false,
1005-
}
992+
matches!(
993+
self.local_info,
994+
Some(box LocalInfo::User(ClearCrossCrate::Set(
995+
BindingForm::Var(VarBindingForm {
996+
binding_mode: ty::BindingMode::BindByValue(_),
997+
opt_ty_info: _,
998+
opt_match_place: _,
999+
pat_span: _,
1000+
})
1001+
| BindingForm::ImplicitSelf(_),
1002+
)))
1003+
)
10061004
}
10071005

10081006
/// Returns `true` if this variable is a named variable or function
10091007
/// parameter declared by the user.
10101008
#[inline]
10111009
pub fn is_user_variable(&self) -> bool {
1012-
match self.local_info {
1013-
Some(box LocalInfo::User(_)) => true,
1014-
_ => false,
1015-
}
1010+
matches!(self.local_info, Some(box LocalInfo::User(_)))
10161011
}
10171012

10181013
/// Returns `true` if this is a reference to a variable bound in a `match`
10191014
/// expression that is used to access said variable for the guard of the
10201015
/// match arm.
10211016
pub fn is_ref_for_guard(&self) -> bool {
1022-
match self.local_info {
1023-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard))) => true,
1024-
_ => false,
1025-
}
1017+
matches!(
1018+
self.local_info,
1019+
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
1020+
)
10261021
}
10271022

10281023
/// Returns `Some` if this is a reference to a static item that is used to
10291024
/// access that static
10301025
pub fn is_ref_to_static(&self) -> bool {
1031-
match self.local_info {
1032-
Some(box LocalInfo::StaticRef { .. }) => true,
1033-
_ => false,
1034-
}
1026+
matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
10351027
}
10361028

10371029
/// Returns `Some` if this is a reference to a static item that is used to
@@ -2164,10 +2156,7 @@ pub enum BinOp {
21642156
impl BinOp {
21652157
pub fn is_checkable(self) -> bool {
21662158
use self::BinOp::*;
2167-
match self {
2168-
Add | Sub | Mul | Shl | Shr => true,
2169-
_ => false,
2170-
}
2159+
matches!(self, Add | Sub | Mul | Shl | Shr)
21712160
}
21722161
}
21732162

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
@@ -184,10 +184,10 @@ impl<'tcx> InstanceDef<'tcx> {
184184
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
185185
_ => return true,
186186
};
187-
match tcx.def_key(def_id).disambiguated_data.data {
188-
DefPathData::Ctor | DefPathData::ClosureExpr => true,
189-
_ => false,
190-
}
187+
matches!(
188+
tcx.def_key(def_id).disambiguated_data.data,
189+
DefPathData::Ctor | DefPathData::ClosureExpr
190+
)
191191
}
192192

193193
/// 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
@@ -2610,10 +2610,7 @@ where
26102610
target.target_os == "linux" && target.arch == "sparc64" && target_env_gnu_like;
26112611
let linux_powerpc_gnu_like =
26122612
target.target_os == "linux" && target.arch == "powerpc" && target_env_gnu_like;
2613-
let rust_abi = match sig.abi {
2614-
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => true,
2615-
_ => false,
2616-
};
2613+
let rust_abi = matches!(sig.abi, RustIntrinsic | PlatformIntrinsic | Rust | RustCall);
26172614

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

0 commit comments

Comments
 (0)