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.
This commit is contained in:
Yuki Okushi 2020-10-06 16:25:58 +09:00 committed by GitHub
commit d50349ba8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 140 additions and 269 deletions

View File

@ -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.
@ -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

View File

@ -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();

View File

@ -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> {

View File

@ -971,67 +971,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
@ -2164,10 +2156,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)
}
}

View File

@ -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,
)
)
}
}

View File

@ -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.

View File

@ -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(..)))
}
}

View File

@ -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> {

View File

@ -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
@ -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(..)
)
}
}

View File

@ -184,10 +184,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

View File

@ -2610,10 +2610,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,

View File

@ -2681,15 +2681,15 @@ impl<'tcx> ClosureKind {
/// Returns `true` if a type that impls this closure kind
/// must also implement `other`.
pub fn extends(self, other: ty::ClosureKind) -> bool {
match (self, other) {
(ClosureKind::Fn, ClosureKind::Fn) => true,
(ClosureKind::Fn, ClosureKind::FnMut) => true,
(ClosureKind::Fn, ClosureKind::FnOnce) => true,
(ClosureKind::FnMut, ClosureKind::FnMut) => true,
(ClosureKind::FnMut, ClosureKind::FnOnce) => true,
(ClosureKind::FnOnce, ClosureKind::FnOnce) => true,
_ => false,
}
matches!(
(self, other),
(ClosureKind::Fn, ClosureKind::Fn)
| (ClosureKind::Fn, ClosureKind::FnMut)
| (ClosureKind::Fn, ClosureKind::FnOnce)
| (ClosureKind::FnMut, ClosureKind::FnMut)
| (ClosureKind::FnMut, ClosureKind::FnOnce)
| (ClosureKind::FnOnce, ClosureKind::FnOnce)
)
}
/// Returns the representative scalar type for this closure kind.
@ -2815,15 +2815,15 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn opt_associated_item(self, def_id: DefId) -> Option<&'tcx AssocItem> {
let is_associated_item = if let Some(def_id) = def_id.as_local() {
match self.hir().get(self.hir().local_def_id_to_hir_id(def_id)) {
Node::TraitItem(_) | Node::ImplItem(_) => true,
_ => false,
}
matches!(
self.hir().get(self.hir().local_def_id_to_hir_id(def_id)),
Node::TraitItem(_) | Node::ImplItem(_)
)
} else {
match self.def_kind(def_id) {
DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy => true,
_ => false,
}
matches!(
self.def_kind(def_id),
DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy
)
};
is_associated_item.then(|| self.associated_item(def_id))

View File

@ -1763,10 +1763,7 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_never(&self) -> bool {
match self.kind() {
Never => true,
_ => false,
}
matches!(self.kind(), Never)
}
/// Checks whether a type is definitely uninhabited. This is
@ -1823,34 +1820,22 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_adt(&self) -> bool {
match self.kind() {
Adt(..) => true,
_ => false,
}
matches!(self.kind(), Adt(..))
}
#[inline]
pub fn is_ref(&self) -> bool {
match self.kind() {
Ref(..) => true,
_ => false,
}
matches!(self.kind(), Ref(..))
}
#[inline]
pub fn is_ty_var(&self) -> bool {
match self.kind() {
Infer(TyVar(_)) => true,
_ => false,
}
matches!(self.kind(), Infer(TyVar(_)))
}
#[inline]
pub fn is_ty_infer(&self) -> bool {
match self.kind() {
Infer(_) => true,
_ => false,
}
matches!(self.kind(), Infer(_))
}
#[inline]
@ -1880,20 +1865,14 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_slice(&self) -> bool {
match self.kind() {
RawPtr(TypeAndMut { ty, .. }) | Ref(_, ty, _) => match ty.kind() {
Slice(_) | Str => true,
_ => false,
},
RawPtr(TypeAndMut { ty, .. }) | Ref(_, ty, _) => matches!(ty.kind(), Slice(_) | Str),
_ => false,
}
}
#[inline]
pub fn is_array(&self) -> bool {
match self.kind() {
Array(..) => true,
_ => false,
}
matches!(self.kind(), Array(..))
}
#[inline]
@ -1940,27 +1919,21 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_region_ptr(&self) -> bool {
match self.kind() {
Ref(..) => true,
_ => false,
}
matches!(self.kind(), Ref(..))
}
#[inline]
pub fn is_mutable_ptr(&self) -> bool {
match self.kind() {
matches!(
self.kind(),
RawPtr(TypeAndMut { mutbl: hir::Mutability::Mut, .. })
| Ref(_, _, hir::Mutability::Mut) => true,
_ => false,
}
| Ref(_, _, hir::Mutability::Mut)
)
}
#[inline]
pub fn is_unsafe_ptr(&self) -> bool {
match self.kind() {
RawPtr(_) => true,
_ => false,
}
matches!(self.kind(), RawPtr(_))
}
/// Tests if this is any kind of primitive pointer type (reference, raw pointer, fn pointer).
@ -1990,35 +1963,22 @@ impl<'tcx> TyS<'tcx> {
/// contents are abstract to rustc.)
#[inline]
pub fn is_scalar(&self) -> bool {
match self.kind() {
Bool
| Char
| Int(_)
| Float(_)
| Uint(_)
matches!(
self.kind(),
Bool | Char | Int(_) | Float(_) | Uint(_) | FnDef(..) | FnPtr(_) | RawPtr(_)
| Infer(IntVar(_) | FloatVar(_))
| FnDef(..)
| FnPtr(_)
| RawPtr(_) => true,
_ => false,
}
)
}
/// Returns `true` if this type is a floating point type.
#[inline]
pub fn is_floating_point(&self) -> bool {
match self.kind() {
Float(_) | Infer(FloatVar(_)) => true,
_ => false,
}
matches!(self.kind(), Float(_) | Infer(FloatVar(_)))
}
#[inline]
pub fn is_trait(&self) -> bool {
match self.kind() {
Dynamic(..) => true,
_ => false,
}
matches!(self.kind(), Dynamic(..))
}
#[inline]
@ -2031,52 +1991,32 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_closure(&self) -> bool {
match self.kind() {
Closure(..) => true,
_ => false,
}
matches!(self.kind(), Closure(..))
}
#[inline]
pub fn is_generator(&self) -> bool {
match self.kind() {
Generator(..) => true,
_ => false,
}
matches!(self.kind(), Generator(..))
}
#[inline]
pub fn is_integral(&self) -> bool {
match self.kind() {
Infer(IntVar(_)) | Int(_) | Uint(_) => true,
_ => false,
}
matches!(self.kind(), Infer(IntVar(_)) | Int(_) | Uint(_))
}
#[inline]
pub fn is_fresh_ty(&self) -> bool {
match self.kind() {
Infer(FreshTy(_)) => true,
_ => false,
}
matches!(self.kind(), Infer(FreshTy(_)))
}
#[inline]
pub fn is_fresh(&self) -> bool {
match self.kind() {
Infer(FreshTy(_)) => true,
Infer(FreshIntTy(_)) => true,
Infer(FreshFloatTy(_)) => true,
_ => false,
}
matches!(self.kind(), Infer(FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_)))
}
#[inline]
pub fn is_char(&self) -> bool {
match self.kind() {
Char => true,
_ => false,
}
matches!(self.kind(), Char)
}
#[inline]
@ -2086,34 +2026,22 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_signed(&self) -> bool {
match self.kind() {
Int(_) => true,
_ => false,
}
matches!(self.kind(), Int(_))
}
#[inline]
pub fn is_ptr_sized_integral(&self) -> bool {
match self.kind() {
Int(ast::IntTy::Isize) | Uint(ast::UintTy::Usize) => true,
_ => false,
}
matches!(self.kind(), Int(ast::IntTy::Isize) | Uint(ast::UintTy::Usize))
}
#[inline]
pub fn is_machine(&self) -> bool {
match self.kind() {
Int(..) | Uint(..) | Float(..) => true,
_ => false,
}
matches!(self.kind(), Int(..) | Uint(..) | Float(..))
}
#[inline]
pub fn has_concrete_skeleton(&self) -> bool {
match self.kind() {
Param(_) | Infer(_) | Error(_) => false,
_ => true,
}
!matches!(self.kind(), Param(_) | Infer(_) | Error(_))
}
/// Returns the type and mutability of `*ty`.
@ -2156,26 +2084,17 @@ impl<'tcx> TyS<'tcx> {
#[inline]
pub fn is_fn(&self) -> bool {
match self.kind() {
FnDef(..) | FnPtr(_) => true,
_ => false,
}
matches!(self.kind(), FnDef(..) | FnPtr(_))
}
#[inline]
pub fn is_fn_ptr(&self) -> bool {
match self.kind() {
FnPtr(_) => true,
_ => false,
}
matches!(self.kind(), FnPtr(_))
}
#[inline]
pub fn is_impl_trait(&self) -> bool {
match self.kind() {
Opaque(..) => true,
_ => false,
}
matches!(self.kind(), Opaque(..))
}
#[inline]