Skip to content

Commit

Permalink
match -> matches! for simple 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 ff49cf0 commit 1905ed5
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 117 deletions.
5 changes: 1 addition & 4 deletions config/src/config/vm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ pub enum VMPublishingOption {

impl VMPublishingOption {
pub fn is_open(&self) -> bool {
match self {
VMPublishingOption::Open => true,
_ => false,
}
matches!(self, VMPublishingOption::Open)
}

pub fn get_whitelist_set(&self) -> Option<&HashSet<[u8; SCRIPT_HASH_LENGTH]>> {
Expand Down
20 changes: 4 additions & 16 deletions consensus/src/chained_bft/network_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,12 @@ impl NetworkPlayground {

/// Returns true for proposal messages only.
pub fn proposals_only<T>(msg: &(Author, ConsensusMsg<T>)) -> bool {
match &msg.1 {
ConsensusMsg::ProposalMsg(_) => true,
_ => false,
}
matches!(&msg.1, ConsensusMsg::ProposalMsg(_))
}

/// Returns true for vote messages only.
pub fn votes_only<T>(msg: &(Author, ConsensusMsg<T>)) -> bool {
match &msg.1 {
ConsensusMsg::VoteMsg(_) => true,
_ => false,
}
matches!(&msg.1, ConsensusMsg::VoteMsg(_))
}

/// Returns true for vote messages that carry round signatures only.
Expand All @@ -297,17 +291,11 @@ impl NetworkPlayground {

/// Returns true for sync info messages only.
pub fn sync_info_only<T>(msg: &(Author, ConsensusMsg<T>)) -> bool {
match &msg.1 {
ConsensusMsg::SyncInfo(_) => true,
_ => false,
}
matches!(&msg.1, ConsensusMsg::SyncInfo(_))
}

pub fn epoch_change_only<T>(msg: &(Author, ConsensusMsg<T>)) -> bool {
match &msg.1 {
ConsensusMsg::ValidatorChangeProof(_) => true,
_ => false,
}
matches!(&msg.1, ConsensusMsg::ValidatorChangeProof(_))
}

fn is_message_dropped(&self, src: &Author, net_req: &PeerManagerRequest) -> bool {
Expand Down
10 changes: 2 additions & 8 deletions execution/executor/src/executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,14 +658,8 @@ proptest! {
HashValue::zero(), block.txns, &committed_trees, &committed_trees,
).unwrap();
let retry_iter = output.transaction_data().iter().map(TransactionData::status)
.skip_while(|status| match *status {
TransactionStatus::Keep(_) => true,
_ => false
});
prop_assert_eq!(retry_iter.take_while(|status| match *status {
TransactionStatus::Retry => true,
_ => false
}).count() as u64, num_txns - reconfig_txn_index - 1);
.skip_while(|status| matches!(*status, TransactionStatus::Keep(_)));
prop_assert_eq!(retry_iter.take_while(|status| matches!(*status,TransactionStatus::Retry)).count() as u64, num_txns - reconfig_txn_index - 1);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,11 @@ proptest! {
let errs = unused_entry_checker.verify();

let has_unused_fields = errs.iter().any(|err| {
match err.major_status {
StatusCode::UNUSED_FIELD => true,
_ => false,
}
matches!(err.major_status, StatusCode::UNUSED_FIELD)
});

let has_unused_type_signature = errs.iter().any(|err| {
match err.major_status {
StatusCode::UNUSED_TYPE_SIGNATURE => true,
_ => false,
}
matches!(err.major_status, StatusCode::UNUSED_TYPE_SIGNATURE)
});

prop_assert!(has_unused_fields && has_unused_type_signature);
Expand Down
8 changes: 4 additions & 4 deletions language/compiler/ir-to-bytecode/syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ impl Tok {
/// Return true if the given token is the beginning of a specification directive for the Move
/// prover
pub fn is_spec_directive(self) -> bool {
match self {
Tok::Ensures | Tok::Requires | Tok::SucceedsIf | Tok::AbortsIf => true,
_ => false,
}
matches!(
self,
Tok::Ensures | Tok::Requires | Tok::SucceedsIf | Tok::AbortsIf
)
}
}

Expand Down
10 changes: 2 additions & 8 deletions language/functional-tests/src/checker/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,11 @@ pub enum MatchStatus {

impl MatchStatus {
pub fn is_success(&self) -> bool {
match self {
Self::Success => true,
_ => false,
}
matches!(self, Self::Success)
}

pub fn is_failure(&self) -> bool {
match self {
Self::Failure(_) => true,
_ => false,
}
matches!(self, Self::Failure(_))
}
}

Expand Down
5 changes: 1 addition & 4 deletions language/functional-tests/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ pub enum EvaluationOutput {

impl EvaluationOutput {
pub fn is_error(&self) -> bool {
match self {
Self::Error(_) => true,
_ => false,
}
matches!(self, Self::Error(_))
}
}

Expand Down
5 changes: 1 addition & 4 deletions language/move-core/types/src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ use std::{borrow::Borrow, fmt, ops::Deref};
// TODO: "<SELF>" is coded as an exception. It should be removed once CompiledScript goes away.
fn is_valid(s: &str) -> bool {
fn is_underscore_alpha_or_digit(c: char) -> bool {
match c {
'_' | 'a'..='z' | 'A'..='Z' | '0'..='9' => true,
_ => false,
}
matches!(c, '_' | 'a'..='z' | 'A'..='Z' | '0'..='9')
}

if s == "<SELF>" {
Expand Down
5 changes: 1 addition & 4 deletions language/move-lang/src/hlir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,7 @@ fn needs_freeze(sp!(_, actual): &H::Type, sp!(_, expected): &H::Type) -> Freeze

fn needs_freeze_single(sp!(_, actual): &H::SingleType, sp!(_, expected): &H::SingleType) -> bool {
use H::SingleType_ as T;
match (actual, expected) {
(T::Ref(true, _), T::Ref(false, _)) => true,
_ => false,
}
matches!((actual, expected), (T::Ref(true, _), T::Ref(false, _)))
}

fn freeze(context: &mut Context, result: &mut Block, expected_type: &H::Type, e: H::Exp) -> H::Exp {
Expand Down
5 changes: 1 addition & 4 deletions language/move-lang/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,7 @@ impl BinOp_ {

pub fn is_spec_only(&self) -> bool {
use BinOp_ as B;
match self {
B::Range | B::Implies => true,
_ => false,
}
matches!(self, B::Range | B::Implies)
}
}

Expand Down
13 changes: 5 additions & 8 deletions language/move-lang/src/parser/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,13 +733,13 @@ fn parse_call_args<'input>(tokens: &mut Lexer<'input>) -> Result<Spanned<Vec<Exp
// This is needed, for example, to check for the optional Exp argument to
// a return (where "return" is itself an Exp).
fn at_end_of_exp<'input>(tokens: &mut Lexer<'input>) -> bool {
match tokens.peek() {
matches!(
tokens.peek(),
// These are the tokens that can occur after an Exp. If the grammar
// changes, we need to make sure that these are kept up to date and that
// none of these tokens can occur at the beginning of an Exp.
Tok::Else | Tok::RBrace | Tok::RParen | Tok::Comma | Tok::Colon | Tok::Semicolon => true,
_ => false,
}
Tok::Else | Tok::RBrace | Tok::RParen | Tok::Comma | Tok::Colon | Tok::Semicolon
)
}

// Parse an expression:
Expand Down Expand Up @@ -1179,10 +1179,7 @@ fn parse_function_decl<'input>(
// ("acquires" (<ModuleAccess> ",")* <ModuleAccess> ","?
let mut acquires = vec![];
if match_token(tokens, Tok::Acquires)? {
let follows_acquire = |tok| match tok {
Tok::Semicolon | Tok::LBrace => true,
_ => false,
};
let follows_acquire = |tok| matches!(tok, Tok::Semicolon | Tok::LBrace);
loop {
acquires.push(parse_module_access(tokens, || {
"a resource struct name".to_string()
Expand Down
11 changes: 2 additions & 9 deletions language/move-prover/bytecode-to-boogie/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,12 @@ pub enum GlobalType {
impl GlobalType {
/// Determines whether this is a reference.
pub fn is_reference(&self) -> bool {
match self {
GlobalType::Reference(_) | GlobalType::MutableReference(_) => true,
_ => false,
}
matches!(self, GlobalType::Reference(_) | GlobalType::MutableReference(_))
}

/// Determines whether this is a mutual reference.
pub fn is_mutual_reference(&self) -> bool {
if let GlobalType::MutableReference(_) = self {
true
} else {
false
}
matches!(self, GlobalType::MutableReference(_))
}

/// Instantiates type parameters in this type.
Expand Down
5 changes: 1 addition & 4 deletions language/move-prover/spec-lang/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ impl Type {

/// Determines whether this is a reference.
pub fn is_reference(&self) -> bool {
match self {
Type::Reference(_, _) => true,
_ => false,
}
matches!(self, Type::Reference(_, _))
}

/// Determines whether this is a mutual reference.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ impl StacklessBytecode {
}

pub fn is_conditional_branch(&self) -> bool {
match self {
StacklessBytecode::BrFalse(_, _) | StacklessBytecode::BrTrue(_, _) => true,
_ => false,
}
matches!(
self,
StacklessBytecode::BrFalse(_, _) | StacklessBytecode::BrTrue(_, _)
)
}

pub fn is_branch(&self) -> bool {
Expand Down
5 changes: 1 addition & 4 deletions language/tools/test-generation/src/abstract_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ impl AbstractValue {
/// Create a new struct `AbstractValue` given its type and kind
pub fn new_struct(token: SignatureToken, kind: Kind) -> AbstractValue {
checked_precondition!(
match token {
SignatureToken::Struct(_, _) => true,
_ => false,
},
matches!(token, SignatureToken::Struct(_, _)),
"AbstractValue::new_struct must be applied with a struct type"
);
AbstractValue { token, kind }
Expand Down
25 changes: 5 additions & 20 deletions language/vm/src/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,7 @@ impl Kind {
pub fn is_sub_kind_of(self, k: Kind) -> bool {
use Kind::*;

match (self, k) {
(_, All) | (Resource, Resource) | (Unrestricted, Unrestricted) => true,
_ => false,
}
matches!((self, k), (_, All) | (Resource, Resource) | (Unrestricted, Unrestricted))
}

/// Helper function to determine the kind of a struct instance by taking the kind of a type
Expand Down Expand Up @@ -673,20 +670,14 @@ impl SignatureToken {
pub fn is_reference(&self) -> bool {
use SignatureToken::*;

match self {
Reference(_) | MutableReference(_) => true,
_ => false,
}
matches!(self, Reference(_) | MutableReference(_))
}

/// Returns true if the `SignatureToken` is a mutable reference.
pub fn is_mutable_reference(&self) -> bool {
use SignatureToken::*;

match self {
MutableReference(_) => true,
_ => false,
}
matches!(self, MutableReference(_))
}

/// Set the index to this one. Useful for random testing.
Expand Down Expand Up @@ -1303,19 +1294,13 @@ impl ::std::fmt::Debug for Bytecode {
impl Bytecode {
/// Return true if this bytecode instruction always branches
pub fn is_unconditional_branch(&self) -> bool {
match self {
Bytecode::Ret | Bytecode::Abort | Bytecode::Branch(_) => true,
_ => false,
}
matches!(self, Bytecode::Ret | Bytecode::Abort | Bytecode::Branch(_))
}

/// Return true if the branching behavior of this bytecode instruction depends on a runtime
/// value
pub fn is_conditional_branch(&self) -> bool {
match self {
Bytecode::BrFalse(_) | Bytecode::BrTrue(_) => true,
_ => false,
}
matches!(self, Bytecode::BrFalse(_) | Bytecode::BrTrue(_))
}

/// Returns true if this bytecode instruction is either a conditional or an unconditional branch
Expand Down
5 changes: 1 addition & 4 deletions storage/jellyfish-merkle/src/node_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,7 @@ impl Node {

/// Returns `true` if the node is a leaf node.
pub fn is_leaf(&self) -> bool {
match self {
Node::Leaf(_) => true,
_ => false,
}
matches!(self, Node::Leaf(_))
}

/// Serializes to bytes for physical storage.
Expand Down

0 comments on commit 1905ed5

Please sign in to comment.