Skip to content

Commit

Permalink
match -> matches! for more complicated patterns
Browse files Browse the repository at this point in the history
Closes: diem#2922
Approved by: davidiw
  • Loading branch information
mimoo authored and bors-libra committed Mar 17, 2020
1 parent 1905ed5 commit e1a8942
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 53 deletions.
8 changes: 4 additions & 4 deletions consensus/src/chained_bft/network_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ impl NetworkPlayground {

/// Returns true for vote messages that carry round signatures only.
pub fn timeout_votes_only<T>(msg: &(Author, ConsensusMsg<T>)) -> bool {
match &msg.1 {
matches!(
&msg.1,
// Timeout votes carry non-empty round signatures.
ConsensusMsg::VoteMsg(vote_msg) => vote_msg.vote().timeout_signature().is_some(),
_ => false,
}
ConsensusMsg::VoteMsg(vote_msg) if vote_msg.vote().timeout_signature().is_some()
)
}

/// Returns true for sync info messages only.
Expand Down
8 changes: 4 additions & 4 deletions language/functional-tests/src/config/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ pub enum Entry {

impl Entry {
pub fn is_validator(&self) -> bool {
match self {
matches!(
self,
Entry::AccountDefinition(AccountDefinition {
role: Some(Role::Validator),
..
}) => true,
_ => false,
}
})
)
}
}

Expand Down
5 changes: 1 addition & 4 deletions language/libra-vm/src/unit_tests/block_chunking_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ proptest! {
let check = txns.iter().zip(result.iter()).all(|(l, r)| {
if let Transaction::UserTransaction(txn) = l {
if let TransactionPayload::WriteSet(ws_l) = txn.payload() {
return match r {
Transaction::WriteSet(ws_r) => ws_l == ws_r,
_ => false,
}
return matches!(r, Transaction::WriteSet(ws_r) if ws_l == ws_r);
}
}
l == r
Expand Down
8 changes: 4 additions & 4 deletions language/move-lang/src/cfgir/liveness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ mod last_usage {
// Non-references might still be borrowed
// Switching such non-locals to a copy is an optimization and not
// needed for this refinement
let is_reference = match &parent_e.ty.value {
Type_::Single(sp!(_, SingleType_::Ref(_, _))) => true,
_ => false,
};
let is_reference = matches!(
&parent_e.ty.value,
Type_::Single(sp!(_, SingleType_::Ref(_, _)))
);
if var_is_dead && is_reference && !*from_user {
parent_e.exp.value = E::Move {
var: var.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ fn remove_fall_through(blocks: &mut IR::BytecodeBlocks) -> bool {
for idx in 0..(blocks.len() - 1) {
let next_block = blocks.get(idx + 1).unwrap().0.clone();
let (_, block) = blocks.get_mut(idx).unwrap();
let remove_last = match &block.last().unwrap().value {
B::Branch(lbl) if lbl == &next_block => true,
_ => false,
};
let remove_last =
matches!(&block.last().unwrap().value, B::Branch(lbl) if lbl == &next_block);
if remove_last {
changed = true;
block.pop();
Expand Down
6 changes: 1 addition & 5 deletions language/move-lang/src/typing/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,7 @@ impl Context {
}

pub fn is_current_function(&self, m: &ModuleIdent, f: &FunctionName) -> bool {
self.is_current_module(m)
&& match &self.current_function {
Some(curf) => curf == f,
_ => false,
}
self.is_current_module(m) && matches!(&self.current_function, Some(curf) if curf == f)
}

fn module_info(&self, m: &ModuleIdent) -> &ModuleInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ pub enum StacklessBytecode {

impl StacklessBytecode {
pub fn is_unconditional_branch(&self) -> bool {
match self {
StacklessBytecode::Ret(_)
| StacklessBytecode::Abort(_)
| StacklessBytecode::Branch(_) => true,
_ => false,
}
matches!(
self,
StacklessBytecode::Ret(_) | StacklessBytecode::Abort(_) | StacklessBytecode::Branch(_)
)
}

pub fn is_conditional_branch(&self) -> bool {
Expand Down
30 changes: 17 additions & 13 deletions language/tools/cost-synthesis/src/stack_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,23 @@ impl<'txn> RandomStackGenerator<'txn> {
// transitions, or from the type signatures available in the module(s).
fn is_module_specific_op(&self) -> bool {
use Bytecode::*;
match self.op {
MoveToSender(_, _)
| MoveFrom(_, _)
| ImmBorrowGlobal(_, _)
| MutBorrowGlobal(_, _)
| Exists(_, _)
| Unpack(_, _)
| Pack(_, _)
| Call(_, _) => true,
CopyLoc(_) | MoveLoc(_) | StLoc(_) | MutBorrowLoc(_) | ImmBorrowLoc(_)
| ImmBorrowField(_) | MutBorrowField(_) => true,
_ => false,
}
matches!(self.op,
MoveToSender(_, _) |
MoveFrom(_, _) |
ImmBorrowGlobal(_, _) |
MutBorrowGlobal(_, _) |
Exists(_, _) |
Unpack(_, _) |
Pack(_, _) |
Call(_, _) |
CopyLoc(_) |
MoveLoc(_) |
StLoc(_) |
MutBorrowLoc(_) |
ImmBorrowLoc(_) |
ImmBorrowField(_) |
MutBorrowField(_)
)
}

// TODO: merge the following three.
Expand Down
6 changes: 1 addition & 5 deletions language/tools/test-generation/src/abstract_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ impl AbstractValue {
/// Create a new reference `AbstractValue` given its type and kind
pub fn new_reference(token: SignatureToken, kind: Kind) -> AbstractValue {
checked_precondition!(
match token {
SignatureToken::Reference(_) => true,
SignatureToken::MutableReference(_) => true,
_ => false,
},
matches!(token, SignatureToken::Reference(_) | SignatureToken::MutableReference(_)),
"AbstractValue::new_reference must be applied with a reference type"
);
AbstractValue { token, kind }
Expand Down
5 changes: 1 addition & 4 deletions language/tools/test-generation/tests/struct_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ fn bytecode_movefrom() {
);
let struct_value = state2.stack_peek(0).expect("struct not added to stack");
assert!(
match struct_value.token {
SignatureToken::Struct(struct_handle, _) => struct_handle == struct_def.struct_handle,
_ => false,
},
matches!(struct_value.token, SignatureToken::Struct(struct_handle, _) if struct_handle == struct_def.struct_handle),
"stack type postcondition not met"
);
}
Expand Down

0 comments on commit e1a8942

Please sign in to comment.