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:
commit
d50349ba8d
13 changed files with 140 additions and 269 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue