Skip to content

Commit

Permalink
chore: cppcheck useStlAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Mar 7, 2023
1 parent cbf8b2e commit 6b1ce14
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 39 deletions.
3 changes: 2 additions & 1 deletion CMakeModules/cppcheck.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ else ()
--error-exitcode=1
--enable=all
--suppress=missingInclude
#--suppress=useStlAlgorithm
# find_if - useless here
--suppress=useStlAlgorithm:${PROJECT_SOURCE_DIR}/*/pbft_sync_packet_handler.cpp
--suppress=noExplicitConstructor
--suppress=unknownMacro
# false positive
Expand Down
3 changes: 2 additions & 1 deletion libraries/aleth/libdevcore/FixedHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class FixedHash {
/// Populate with random data.
template <class Engine>
void randomize(Engine& _eng) {
for (auto& i : m_data) i = (uint8_t)std::uniform_int_distribution<uint16_t>(0, 255)(_eng);
std::generate(m_data.begin(), m_data.end(),
[&]() { return (uint8_t)std::uniform_int_distribution<uint16_t>(0, 255)(_eng); });
}

/// @returns a random valued object.
Expand Down
3 changes: 2 additions & 1 deletion libraries/aleth/libdevcore/RLP.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ class RLP {
std::vector<T> ret;
if (isList()) {
ret.reserve(itemCount());
for (auto const i : *this) ret.push_back(i.convert<T>(_flags));
std::transform((*this).begin(), (*this).end(), std::back_inserter(ret),
[_flags](const auto i) { return i.template convert<T>(_flags); });
} else if (_flags & ThrowOnFail)
BOOST_THROW_EXCEPTION(BadCast());
return ret;
Expand Down
5 changes: 1 addition & 4 deletions libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,7 @@ DagBlock DagBlockProposer::createDagBlock(DagFrontier&& frontier, level_t level,
trx_hashes.push_back(trx->getHash());
}

uint64_t block_estimation = 0;
for (const auto& e : estimations) {
block_estimation += e;
}
const int64_t block_estimation = std::accumulate(estimations.begin(), estimations.end(), 0);

// If number of tips is over the limit filter by producer and level
if (frontier.tips.size() > kDagBlockMaxTips || (frontier.tips.size() + 1) > kPbftGasLimit / kDagGasLimit) {
Expand Down
6 changes: 3 additions & 3 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ class FinalChainImpl final : public FinalChain {
for (auto const& r : exec_results) {
LogEntries logs;
logs.reserve(r.logs.size());
for (auto const& l : r.logs) {
logs.emplace_back(LogEntry{l.address, l.topics, l.data});
}
std::transform(r.logs.cbegin(), r.logs.cend(), std::back_inserter(logs), [](const auto& l) {
return LogEntry{l.address, l.topics, l.data};
});
receipts.emplace_back(TransactionReceipt{
r.code_err.empty() && r.consensus_err.empty(),
r.gas_used,
Expand Down
7 changes: 3 additions & 4 deletions libraries/core_libs/consensus/src/pbft/pbft_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1838,10 +1838,9 @@ void PbftManager::periodDataQueuePush(PeriodData &&period_data, dev::p2p::NodeID
size_t PbftManager::periodDataQueueSize() const { return sync_queue_.size(); }

bool PbftManager::checkBlockWeight(const std::vector<DagBlock> &dag_blocks) const {
u256 total_weight = 0;
for (const auto &dag_block : dag_blocks) {
total_weight += dag_block.getGasEstimation();
}
const u256 total_weight =
std::accumulate(dag_blocks.begin(), dag_blocks.end(), u256(0),
[](u256 value, const auto &dag_block) { return value + dag_block.getGasEstimation(); });
if (total_weight > config_.gas_limit) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ uint64_t VoteManager::getVerifiedVotesSize() const {
for (auto const& period : verified_votes_) {
for (auto const& round : period.second) {
for (auto const& step : round.second.step_votes) {
for (auto const& voted_value : step.second.votes) {
size += voted_value.second.second.size();
}
size += std::accumulate(
step.second.votes.begin(), step.second.votes.end(), 0,
[](uint64_t value, const auto& voted_value) { return value + voted_value.second.second.size(); });
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions libraries/core_libs/network/rpc/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ Json::Value Test::send_coin_transactions(const Json::Value &param1) {
auto gas = dev::jsToInt(param1["gas"].asString());
auto transactions_count = param1["transaction_count"].asUInt64();
std::vector<addr_t> receivers;
for (auto rec : param1["receiver"]) {
receivers.emplace_back(addr_t(rec.asString()));
}
std::transform(param1["receiver"].begin(), param1["receiver"].end(), std::back_inserter(receivers),
[](const auto rec) { return addr_t(rec.asString()); });
for (uint32_t i = 0; i < transactions_count; i++) {
auto trx = std::make_shared<Transaction>(nonce, value, gas_price, gas, bytes(), sk,
receivers[i % receivers.size()], kChainId);
Expand Down
12 changes: 3 additions & 9 deletions libraries/core_libs/network/rpc/eth/LogFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ LogFilter::LogFilter(EthBlockNumber from_block, std::optional<EthBlockNumber> to
if (!addresses_.empty()) {
return;
}
for (const auto& t : topics_) {
if (!t.empty()) {
return;
}
}
is_range_only_ = true;
is_range_only_ = std::all_of(topics_.cbegin(), topics_.cend(), [](const auto& t) { return t.empty(); });
}

std::vector<LogBloom> LogFilter::bloomPossibilities() const {
Expand Down Expand Up @@ -51,9 +46,8 @@ std::vector<LogBloom> LogFilter::bloomPossibilities() const {
// blooms = [a0, a1];
//
if (ret.empty()) {
for (const auto& i : addresses_) {
ret.push_back(LogBloom().shiftBloom<3>(sha3(i)));
}
std::transform(addresses_.cbegin(), addresses_.cend(), std::back_inserter(ret),
[](const auto& i) { return LogBloom().shiftBloom<3>(sha3(i)); });
}

// 3rd case, there are no addresses, at least create blooms from topics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,7 @@ std::optional<PacketData> PriorityQueue::pop() {
}

bool PriorityQueue::empty() const {
for (const auto& queue : packets_queues_) {
if (!queue.empty()) {
return false;
}
}

return true;
return std::all_of(packets_queues_.cbegin(), packets_queues_.cend(), [](const auto& queue) { return queue.empty(); });
}

void PriorityQueue::updateDependenciesStart(const PacketData& packet) {
Expand Down
6 changes: 3 additions & 3 deletions libraries/types/transaction/src/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <libdevcore/CommonJS.h>

#include <algorithm>
#include <string>
#include <utility>

Expand All @@ -22,9 +23,8 @@ uint64_t toChainID(u256 const &val) {
TransactionHashes hashes_from_transactions(const SharedTransactions &transactions) {
TransactionHashes trx_hashes;
trx_hashes.reserve(transactions.size());
for (auto const &trx : transactions) {
trx_hashes.push_back(trx->getHash());
}
std::transform(transactions.cbegin(), transactions.cend(), std::back_inserter(trx_hashes),
[](const auto &trx) { return trx->getHash(); });
return trx_hashes;
}

Expand Down

0 comments on commit 6b1ce14

Please sign in to comment.