Skip to content

Commit

Permalink
Reduce boilerplate with the matches! macro
Browse files Browse the repository at this point in the history
Replaces simple bool `match`es of the form

    match $expr {
        $pattern => true
        _ => false
    }

and their inverse with invocations of the matches! macro.
  • Loading branch information
LingMan committed Sep 21, 2020
1 parent 0f9f0b3 commit a6ff925
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 269 deletions.
16 changes: 8 additions & 8 deletions compiler/rustc_middle/src/hir/map/mod.rs
Expand Up @@ -535,15 +535,15 @@ impl<'hir> Map<'hir> {
Some(Node::Binding(_)) => (),
_ => return false,
}
match self.find(self.get_parent_node(id)) {
matches!(
self.find(self.get_parent_node(id)),
Some(
Node::Item(_)
| Node::TraitItem(_)
| Node::ImplItem(_)
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. }),
) => true,
_ => false,
}
)
)
}

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

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

/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/mir/interpret/mod.rs
Expand Up @@ -486,10 +486,10 @@ impl<'tcx> TyCtxt<'tcx> {
// `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
// However, formatting code relies on function identity (see #58320), so we only do
// this for generic functions. Lifetime parameters are ignored.
let is_generic = instance.substs.into_iter().any(|kind| match kind.unpack() {
GenericArgKind::Lifetime(_) => false,
_ => true,
});
let is_generic = instance
.substs
.into_iter()
.any(|kind| !matches!(kind.unpack(), GenericArgKind::Lifetime(_)));
if is_generic {
// Get a fresh ID.
let mut alloc_map = self.alloc_map.lock();
Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_middle/src/mir/interpret/value.rs
Expand Up @@ -445,19 +445,13 @@ impl<'tcx, Tag> Scalar<Tag> {
/// Do not call this method! Dispatch based on the type instead.
#[inline]
pub fn is_bits(self) -> bool {
match self {
Scalar::Raw { .. } => true,
_ => false,
}
matches!(self, Scalar::Raw { .. })
}

/// Do not call this method! Dispatch based on the type instead.
#[inline]
pub fn is_ptr(self) -> bool {
match self {
Scalar::Ptr(_) => true,
_ => false,
}
matches!(self, Scalar::Ptr(_))
}

pub fn to_bool(self) -> InterpResult<'tcx, bool> {
Expand Down
73 changes: 31 additions & 42 deletions compiler/rustc_middle/src/mir/mod.rs
Expand Up @@ -935,67 +935,59 @@ impl<'tcx> LocalDecl<'tcx> {
/// - `let x = ...`,
/// - or `match ... { C(x) => ... }`
pub fn can_be_made_mutable(&self) -> bool {
match self.local_info {
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
binding_mode: ty::BindingMode::BindByValue(_),
opt_ty_info: _,
opt_match_place: _,
pat_span: _,
})))) => true,

Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
ImplicitSelfKind::Imm,
)))) => true,

_ => false,
}
matches!(
self.local_info,
Some(box LocalInfo::User(ClearCrossCrate::Set(
BindingForm::Var(VarBindingForm {
binding_mode: ty::BindingMode::BindByValue(_),
opt_ty_info: _,
opt_match_place: _,
pat_span: _,
})
| BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
)))
)
}

/// Returns `true` if local is definitely not a `ref ident` or
/// `ref mut ident` binding. (Such bindings cannot be made into
/// mutable bindings, but the inverse does not necessarily hold).
pub fn is_nonref_binding(&self) -> bool {
match self.local_info {
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
binding_mode: ty::BindingMode::BindByValue(_),
opt_ty_info: _,
opt_match_place: _,
pat_span: _,
})))) => true,

Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(_)))) => true,

_ => false,
}
matches!(
self.local_info,
Some(box LocalInfo::User(ClearCrossCrate::Set(
BindingForm::Var(VarBindingForm {
binding_mode: ty::BindingMode::BindByValue(_),
opt_ty_info: _,
opt_match_place: _,
pat_span: _,
})
| BindingForm::ImplicitSelf(_),
)))
)
}

/// Returns `true` if this variable is a named variable or function
/// parameter declared by the user.
#[inline]
pub fn is_user_variable(&self) -> bool {
match self.local_info {
Some(box LocalInfo::User(_)) => true,
_ => false,
}
matches!(self.local_info, Some(box LocalInfo::User(_)))
}

/// Returns `true` if this is a reference to a variable bound in a `match`
/// expression that is used to access said variable for the guard of the
/// match arm.
pub fn is_ref_for_guard(&self) -> bool {
match self.local_info {
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard))) => true,
_ => false,
}
matches!(
self.local_info,
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
)
}

/// Returns `Some` if this is a reference to a static item that is used to
/// access that static
pub fn is_ref_to_static(&self) -> bool {
match self.local_info {
Some(box LocalInfo::StaticRef { .. }) => true,
_ => false,
}
matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
}

/// Returns `Some` if this is a reference to a static item that is used to
Expand Down Expand Up @@ -2124,10 +2116,7 @@ pub enum BinOp {
impl BinOp {
pub fn is_checkable(self) -> bool {
use self::BinOp::*;
match self {
Add | Sub | Mul | Shl | Shr => true,
_ => false,
}
matches!(self, Add | Sub | Mul | Shl | Shr)
}
}

Expand Down
63 changes: 22 additions & 41 deletions compiler/rustc_middle/src/mir/visit.rs
Expand Up @@ -1164,82 +1164,63 @@ pub enum PlaceContext {
impl PlaceContext {
/// Returns `true` if this place context represents a drop.
pub fn is_drop(&self) -> bool {
match *self {
PlaceContext::MutatingUse(MutatingUseContext::Drop) => true,
_ => false,
}
matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
}

/// Returns `true` if this place context represents a borrow.
pub fn is_borrow(&self) -> bool {
match *self {
matches!(
self,
PlaceContext::NonMutatingUse(
NonMutatingUseContext::SharedBorrow
| NonMutatingUseContext::ShallowBorrow
| NonMutatingUseContext::UniqueBorrow,
)
| PlaceContext::MutatingUse(MutatingUseContext::Borrow) => true,
_ => false,
}
| NonMutatingUseContext::ShallowBorrow
| NonMutatingUseContext::UniqueBorrow
) | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
)
}

/// Returns `true` if this place context represents a storage live or storage dead marker.
pub fn is_storage_marker(&self) -> bool {
match *self {
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) => true,
_ => false,
}
matches!(
self,
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
)
}

/// Returns `true` if this place context represents a storage live marker.
pub fn is_storage_live_marker(&self) -> bool {
match *self {
PlaceContext::NonUse(NonUseContext::StorageLive) => true,
_ => false,
}
matches!(self, PlaceContext::NonUse(NonUseContext::StorageLive))
}

/// Returns `true` if this place context represents a storage dead marker.
pub fn is_storage_dead_marker(&self) -> bool {
match *self {
PlaceContext::NonUse(NonUseContext::StorageDead) => true,
_ => false,
}
matches!(self, PlaceContext::NonUse(NonUseContext::StorageDead))
}

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

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

/// Returns `true` if this place context represents a use.
pub fn is_use(&self) -> bool {
match *self {
PlaceContext::NonUse(..) => false,
_ => true,
}
!matches!(self, PlaceContext::NonUse(..))
}

/// Returns `true` if this place context represents an assignment statement.
pub fn is_place_assignment(&self) -> bool {
match *self {
matches!(
self,
PlaceContext::MutatingUse(
MutatingUseContext::Store
| MutatingUseContext::Call
| MutatingUseContext::AsmOutput,
) => true,
_ => false,
}
| MutatingUseContext::Call
| MutatingUseContext::AsmOutput,
)
)
}
}
5 changes: 1 addition & 4 deletions compiler/rustc_middle/src/traits/specialization_graph.rs
Expand Up @@ -79,10 +79,7 @@ pub enum Node {

impl<'tcx> Node {
pub fn is_from_trait(&self) -> bool {
match *self {
Node::Trait(..) => true,
_ => false,
}
matches!(self, Node::Trait(..))
}

/// Iterate over the items defined directly by the given (impl or trait) node.
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_middle/src/ty/adjustment.rs
Expand Up @@ -85,10 +85,7 @@ pub struct Adjustment<'tcx> {

impl Adjustment<'tcx> {
pub fn is_region_borrow(&self) -> bool {
match self.kind {
Adjust::Borrow(AutoBorrow::Ref(..)) => true,
_ => false,
}
matches!(self.kind, Adjust::Borrow(AutoBorrow::Ref(..)))
}
}

Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -588,10 +588,7 @@ impl<'tcx> TypeckResults<'tcx> {
return false;
}

match self.type_dependent_defs().get(expr.hir_id) {
Some(Ok((DefKind::AssocFn, _))) => true,
_ => false,
}
matches!(self.type_dependent_defs().get(expr.hir_id), Some(Ok((DefKind::AssocFn, _))))
}

pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> {
Expand Down
32 changes: 16 additions & 16 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Expand Up @@ -11,21 +11,16 @@ use rustc_hir::{QPath, TyKind, WhereBoundPredicate, WherePredicate};
impl<'tcx> TyS<'tcx> {
/// Similar to `TyS::is_primitive`, but also considers inferred numeric values to be primitive.
pub fn is_primitive_ty(&self) -> bool {
match self.kind() {
Bool
| Char
| Str
| Int(_)
| Uint(_)
| Float(_)
matches!(
self.kind(),
Bool | Char | Str | Int(_) | Uint(_) | Float(_)
| Infer(
InferTy::IntVar(_)
| InferTy::FloatVar(_)
| InferTy::FreshIntTy(_)
| InferTy::FreshFloatTy(_),
) => true,
_ => false,
}
| InferTy::FreshFloatTy(_)
)
)
}

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

/// Whether the type can be safely suggested during error recovery.
pub fn is_suggestable(&self) -> bool {
match self.kind() {
Opaque(..) | FnDef(..) | FnPtr(..) | Dynamic(..) | Closure(..) | Infer(..)
| Projection(..) => false,
_ => true,
}
!matches!(
self.kind(),
Opaque(..)
| FnDef(..)
| FnPtr(..)
| Dynamic(..)
| Closure(..)
| Infer(..)
| Projection(..)
)
}
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/ty/instance.rs
Expand Up @@ -183,10 +183,10 @@ impl<'tcx> InstanceDef<'tcx> {
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
_ => return true,
};
match tcx.def_key(def_id).disambiguated_data.data {
DefPathData::Ctor | DefPathData::ClosureExpr => true,
_ => false,
}
matches!(
tcx.def_key(def_id).disambiguated_data.data,
DefPathData::Ctor | DefPathData::ClosureExpr
)
}

/// Returns `true` if the machine code for this instance is instantiated in
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_middle/src/ty/layout.rs
Expand Up @@ -2628,10 +2628,7 @@ where
target.target_os == "linux" && target.arch == "sparc64" && target_env_gnu_like;
let linux_powerpc_gnu_like =
target.target_os == "linux" && target.arch == "powerpc" && target_env_gnu_like;
let rust_abi = match sig.abi {
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => true,
_ => false,
};
let rust_abi = matches!(sig.abi, RustIntrinsic | PlatformIntrinsic | Rust | RustCall);

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

0 comments on commit a6ff925

Please sign in to comment.