Skip to content

Commit 1b8a646

Browse files
Seulgi Kimmajecty
Seulgi Kim
authored andcommitted
Update clippy and rustfmt
This patch * prefers to use cmp * makes code return early * removes redundant cloning * removes redundant lifetime annotation * changes some other miscellaneous changes to follow clippy update.
1 parent 9a71f32 commit 1b8a646

File tree

30 files changed

+237
-238
lines changed

30 files changed

+237
-238
lines changed

Diff for: .github/workflows/cargo-test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
- uses: actions/checkout@v1
1111
- uses: actions-rs/toolchain@v1
1212
with:
13-
toolchain: nightly-2019-05-17
13+
toolchain: nightly-2019-10-13
1414
override: true
1515
- run: rustup component add clippy
1616
- run: cargo fetch --verbose
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@v1
2424
- uses: actions-rs/toolchain@v1
2525
with:
26-
toolchain: nightly-2019-05-17
26+
toolchain: nightly-2019-10-13
2727
override: true
2828
- run: rustup component add rustfmt
2929
- run: cargo fmt -- --check

Diff for: core/src/blockchain/extras.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl Add for TransactionAddresses {
150150
type Output = Self;
151151

152152
fn add(self, rhs: Self) -> <Self as Add>::Output {
153-
let mut s = self.clone();
153+
let mut s = self;
154154
s += rhs;
155155
s
156156
}
@@ -168,7 +168,7 @@ impl Sub for TransactionAddresses {
168168
type Output = Self;
169169

170170
fn sub(self, rhs: Self) -> <Self as Add>::Output {
171-
let mut s = self.clone();
171+
let mut s = self;
172172
s -= rhs;
173173
s
174174
}

Diff for: core/src/blockchain/headerchain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ impl HeaderChain {
276276
};
277277
match route.retracted.len() {
278278
0 => BestHeaderChanged::CanonChainAppended {
279-
best_header: new_best_header.clone(),
279+
best_header: new_best_header,
280280
},
281281
_ => BestHeaderChanged::BranchBecomingCanonChain {
282282
tree_route: route,
283-
best_header: new_best_header.clone(),
283+
best_header: new_best_header,
284284
},
285285
}
286286
} else {

Diff for: core/src/client/importer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl Importer {
199199
let mut batch = DBTransaction::new();
200200

201201
block.state().journal_under(&mut batch, number).expect("DB commit failed");
202-
let route = chain.insert_block(&mut batch, block_data, invoices.clone(), self.engine.borrow());
202+
let route = chain.insert_block(&mut batch, block_data, invoices, self.engine.borrow());
203203

204204
// Final commit to the DB
205205
client.db().write_buffered(batch);

Diff for: core/src/consensus/bit_set.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use std::cmp::PartialEq;
17+
use std::cmp::{Ordering, PartialEq};
1818
use std::convert::TryFrom;
1919
use std::fmt;
2020
use std::ops::Sub;
@@ -126,20 +126,20 @@ impl Decodable for BitSet {
126126
rlp.decoder().decode_value(|bytes| {
127127
let expected = BITSET_SIZE;
128128
let got = bytes.len();
129-
if got > expected {
130-
Err(DecoderError::RlpIsTooBig {
129+
match got.cmp(&expected) {
130+
Ordering::Greater => Err(DecoderError::RlpIsTooBig {
131131
expected,
132132
got,
133-
})
134-
} else if got < expected {
135-
Err(DecoderError::RlpIsTooShort {
133+
}),
134+
Ordering::Less => Err(DecoderError::RlpIsTooShort {
136135
expected,
137136
got,
138-
})
139-
} else {
140-
let mut bit_set = BitSet::new();
141-
bit_set.0.copy_from_slice(bytes);
142-
Ok(bit_set)
137+
}),
138+
Ordering::Equal => {
139+
let mut bit_set = BitSet::new();
140+
bit_set.0.copy_from_slice(bytes);
141+
Ok(bit_set)
142+
}
143143
}
144144
})
145145
}

Diff for: core/src/consensus/stake/action_data.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use std::cmp::Ordering;
1718
use std::collections::btree_map::{BTreeMap, Entry};
1819
use std::collections::btree_set::{self, BTreeSet};
1920
use std::collections::{btree_map, HashMap, HashSet};
@@ -203,12 +204,16 @@ impl<'a> Delegation<'a> {
203204
}
204205

205206
if let Entry::Occupied(mut entry) = self.delegatees.entry(delegatee) {
206-
if *entry.get() > quantity {
207-
*entry.get_mut() -= quantity;
208-
return Ok(())
209-
} else if *entry.get() == quantity {
210-
entry.remove();
211-
return Ok(())
207+
match entry.get().cmp(&quantity) {
208+
Ordering::Greater => {
209+
*entry.get_mut() -= quantity;
210+
return Ok(())
211+
}
212+
Ordering::Equal => {
213+
entry.remove();
214+
return Ok(())
215+
}
216+
Ordering::Less => {}
212217
}
213218
}
214219

Diff for: core/src/consensus/tendermint/worker.rs

+88-84
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use std::cmp::Ordering;
1718
use std::iter::Iterator;
1819
use std::mem;
1920
use std::sync::{Arc, Weak};
@@ -690,34 +691,33 @@ impl Worker {
690691
cinfo!(ENGINE, "move_to_step: Propose.");
691692
// If there are multiple proposals, use the first proposal.
692693
if let Some(hash) = self.votes.get_block_hashes(&vote_step).first() {
693-
if self.client().block(&BlockId::Hash(*hash)).is_some() {
694-
self.proposal = Proposal::new_imported(*hash);
695-
self.move_to_step(TendermintState::Prevote, is_restoring);
696-
} else {
694+
if self.client().block(&BlockId::Hash(*hash)).is_none() {
697695
cwarn!(ENGINE, "Proposal is received but not imported");
698696
// Proposal is received but is not verified yet.
699697
// Wait for verification.
700698
return
701699
}
702-
} else {
703-
let parent_block_hash = self.prev_block_hash();
704-
if self.is_signer_proposer(&parent_block_hash) {
705-
if let TwoThirdsMajority::Lock(lock_view, locked_block_hash) = self.last_two_thirds_majority {
706-
cinfo!(ENGINE, "I am a proposer, I'll re-propose a locked block");
707-
match self.locked_proposal_block(lock_view, locked_block_hash) {
708-
Ok(block) => self.repropose_block(block),
709-
Err(error_msg) => cwarn!(ENGINE, "{}", error_msg),
710-
}
711-
} else {
712-
cinfo!(ENGINE, "I am a proposer, I'll create a block");
713-
self.update_sealing(parent_block_hash);
714-
self.step = TendermintState::ProposeWaitBlockGeneration {
715-
parent_hash: parent_block_hash,
716-
};
717-
}
718-
} else {
719-
self.request_proposal_to_any(vote_step.height, vote_step.view);
700+
self.proposal = Proposal::new_imported(*hash);
701+
self.move_to_step(TendermintState::Prevote, is_restoring);
702+
return
703+
}
704+
let parent_block_hash = self.prev_block_hash();
705+
if !self.is_signer_proposer(&parent_block_hash) {
706+
self.request_proposal_to_any(vote_step.height, vote_step.view);
707+
return
708+
}
709+
if let TwoThirdsMajority::Lock(lock_view, locked_block_hash) = self.last_two_thirds_majority {
710+
cinfo!(ENGINE, "I am a proposer, I'll re-propose a locked block");
711+
match self.locked_proposal_block(lock_view, locked_block_hash) {
712+
Ok(block) => self.repropose_block(block),
713+
Err(error_msg) => cwarn!(ENGINE, "{}", error_msg),
720714
}
715+
} else {
716+
cinfo!(ENGINE, "I am a proposer, I'll create a block");
717+
self.update_sealing(parent_block_hash);
718+
self.step = TendermintState::ProposeWaitBlockGeneration {
719+
parent_hash: parent_block_hash,
720+
};
721721
}
722722
}
723723
Step::Prevote => {
@@ -1562,7 +1562,7 @@ impl Worker {
15621562
on,
15631563
};
15641564

1565-
self.votes.collect(vote.clone()).expect("Must not attempt double vote on proposal");;
1565+
self.votes.collect(vote.clone()).expect("Must not attempt double vote on proposal");
15661566
cinfo!(ENGINE, "Voted {:?} as {}th proposer.", vote, signer_index);
15671567
Ok(vote)
15681568
}
@@ -1811,7 +1811,7 @@ impl Worker {
18111811
);
18121812
}
18131813

1814-
if let Err(double) = self.votes.collect(message.clone()) {
1814+
if let Err(double) = self.votes.collect(message) {
18151815
cerror!(ENGINE, "Double Vote found {:?}", double);
18161816
self.report_double_vote(&double);
18171817
return None
@@ -1869,17 +1869,15 @@ impl Worker {
18691869

18701870
let current_step = current_vote_step.step;
18711871
if current_step == Step::Prevote || current_step == Step::Precommit {
1872-
let peer_known_votes = if current_vote_step == peer_vote_step {
1873-
peer_known_votes
1874-
} else if current_vote_step < peer_vote_step {
1872+
let peer_known_votes = match current_vote_step.cmp(&peer_vote_step) {
1873+
Ordering::Equal => peer_known_votes,
18751874
// We don't know which votes peer has.
18761875
// However the peer knows more than 2/3 of votes.
18771876
// So request all votes.
1878-
BitSet::all_set()
1879-
} else {
1877+
Ordering::Less => BitSet::all_set(),
18801878
// If peer's state is less than my state,
18811879
// the peer does not know any useful votes.
1882-
BitSet::new()
1880+
Ordering::Greater => BitSet::new(),
18831881
};
18841882

18851883
let difference = &peer_known_votes - &self.votes_received;
@@ -1969,45 +1967,47 @@ impl Worker {
19691967
return
19701968
}
19711969

1972-
if height == self.height - 1 {
1973-
let block = self.client().block(&height.into()).expect("Parent block should exist");
1974-
let block_hash = block.hash();
1975-
let finalized_view = self.finalized_view_of_previous_block;
1976-
1977-
let votes = self
1978-
.votes
1979-
.get_all_votes_in_round(&VoteStep {
1980-
height,
1981-
view: finalized_view,
1982-
step: Step::Precommit,
1983-
})
1984-
.into_iter()
1985-
.filter(|vote| vote.on.block_hash == Some(block_hash))
1986-
.collect();
1987-
1988-
self.send_commit(block, votes, &result);
1989-
} else if height < self.height - 1 {
1990-
let block = self.client().block(&height.into()).expect("Parent block should exist");
1991-
let child_block = self.client().block(&(height + 1).into()).expect("Parent block should exist");
1992-
let child_block_header_seal = child_block.header().seal();
1993-
let child_block_seal_view = TendermintSealView::new(&child_block_header_seal);
1994-
let parent_block_finalized_view =
1995-
child_block_seal_view.parent_block_finalized_view().expect("Verified block");
1996-
let on = VoteOn {
1997-
step: VoteStep::new(height, parent_block_finalized_view, Step::Precommit),
1998-
block_hash: Some(block.hash()),
1999-
};
2000-
let mut votes = Vec::new();
2001-
for (index, signature) in child_block_seal_view.signatures().expect("The block is verified") {
2002-
let message = ConsensusMessage {
2003-
signature,
2004-
signer_index: index,
2005-
on: on.clone(),
1970+
match height.cmp(&(self.height - 1)) {
1971+
Ordering::Equal => {
1972+
let block = self.client().block(&height.into()).expect("Parent block should exist");
1973+
let block_hash = block.hash();
1974+
let finalized_view = self.finalized_view_of_previous_block;
1975+
1976+
let votes = self
1977+
.votes
1978+
.get_all_votes_in_round(&VoteStep {
1979+
height,
1980+
view: finalized_view,
1981+
step: Step::Precommit,
1982+
})
1983+
.into_iter()
1984+
.filter(|vote| vote.on.block_hash == Some(block_hash))
1985+
.collect();
1986+
1987+
self.send_commit(block, votes, &result);
1988+
}
1989+
Ordering::Less => {
1990+
let block = self.client().block(&height.into()).expect("Parent block should exist");
1991+
let child_block = self.client().block(&(height + 1).into()).expect("Parent block should exist");
1992+
let child_block_header_seal = child_block.header().seal();
1993+
let child_block_seal_view = TendermintSealView::new(&child_block_header_seal);
1994+
let parent_block_finalized_view =
1995+
child_block_seal_view.parent_block_finalized_view().expect("Verified block");
1996+
let on = VoteOn {
1997+
step: VoteStep::new(height, parent_block_finalized_view, Step::Precommit),
1998+
block_hash: Some(block.hash()),
20061999
};
2007-
votes.push(message);
2000+
let mut votes = Vec::new();
2001+
for (index, signature) in child_block_seal_view.signatures().expect("The block is verified") {
2002+
let message = ConsensusMessage {
2003+
signature,
2004+
signer_index: index,
2005+
on: on.clone(),
2006+
};
2007+
votes.push(message);
2008+
}
20082009
}
2009-
2010-
self.send_commit(block, votes, &result);
2010+
Ordering::Greater => {}
20112011
}
20122012
}
20132013

@@ -2049,23 +2049,27 @@ impl Worker {
20492049
return None
20502050
}
20512051

2052-
if commit_height < self.height {
2053-
cdebug!(
2054-
ENGINE,
2055-
"Received commit message is old. Current height is {} but commit messages is for height {}",
2056-
self.height,
2057-
commit_height,
2058-
);
2059-
return None
2060-
} else if commit_height > self.height {
2061-
cwarn!(
2062-
ENGINE,
2063-
"Invalid commit message received: precommit on height {} but current height is {}",
2064-
commit_height,
2065-
self.height
2066-
);
2067-
return None
2068-
}
2052+
match commit_height.cmp(&self.height) {
2053+
Ordering::Less => {
2054+
cdebug!(
2055+
ENGINE,
2056+
"Received commit message is old. Current height is {} but commit messages is for height {}",
2057+
self.height,
2058+
commit_height,
2059+
);
2060+
return None
2061+
}
2062+
Ordering::Greater => {
2063+
cwarn!(
2064+
ENGINE,
2065+
"Invalid commit message received: precommit on height {} but current height is {}",
2066+
commit_height,
2067+
self.height
2068+
);
2069+
return None
2070+
}
2071+
Ordering::Equal => {}
2072+
};
20692073

20702074
let prev_block_hash = self
20712075
.client()

Diff for: core/src/miner/mem_pool.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ impl MemPool {
960960

961961
/// Returns Some(true) if the given transaction is local and None for not found.
962962
pub fn is_local_transaction(&self, tx_hash: H256) -> Option<bool> {
963-
self.by_hash.get(&tx_hash).and_then(|found_item| Some(found_item.origin.is_local()))
963+
self.by_hash.get(&tx_hash).map(|found_item| found_item.origin.is_local())
964964
}
965965

966966
/// Checks the given timelock with the current time/timestamp.
@@ -1479,7 +1479,7 @@ pub mod test {
14791479
inputs.push(create_mempool_input_with_pay(7u64, keypair, no_timelock));
14801480
mem_pool.add(inputs, inserted_block_number, inserted_timestamp, &fetch_account);
14811481

1482-
let mut mem_pool_recovered = MemPool::with_limits(8192, usize::max_value(), 3, db.clone());
1482+
let mut mem_pool_recovered = MemPool::with_limits(8192, usize::max_value(), 3, db);
14831483
mem_pool_recovered.recover_from_db(&test_client);
14841484

14851485
assert_eq!(mem_pool_recovered.first_seqs, mem_pool.first_seqs);
@@ -1549,7 +1549,7 @@ pub mod test {
15491549
let test_client = TestBlockChainClient::new();
15501550

15511551
let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
1552-
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db.clone());
1552+
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db);
15531553

15541554
let fetch_account = |p: &Public| -> AccountDetails {
15551555
let address = public_to_address(p);

Diff for: keystore/src/accounts_dir/disk.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ mod test {
329329
directory.insert_with_filename(account.clone(), "foo".to_string(), dedup).unwrap();
330330
let file1 = directory.insert_with_filename(account.clone(), filename.clone(), dedup).unwrap().filename.unwrap();
331331
let file2 = directory.insert_with_filename(account.clone(), filename.clone(), dedup).unwrap().filename.unwrap();
332-
let file3 = directory.insert_with_filename(account.clone(), filename.clone(), dedup).unwrap().filename.unwrap();
332+
let file3 = directory.insert_with_filename(account, filename.clone(), dedup).unwrap().filename.unwrap();
333333

334334
// then
335335
// the first file should have the original names

0 commit comments

Comments
 (0)