Skip to content

Commit

Permalink
[CI] Fix some clang-tidy-11 warnings (#1703)
Browse files Browse the repository at this point in the history
* Fix clang-tidy-11
* disable WarningsAsErrors
* more auto fixes
* consistent auto*/&
  • Loading branch information
hewigovens authored Oct 21, 2021
1 parent 60a6625 commit a771f38
Show file tree
Hide file tree
Showing 79 changed files with 245 additions and 229 deletions.
20 changes: 17 additions & 3 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
---
Checks: 'clang-diagnostic-*,clang-analyzer-*,modernize-*,portability-*,llvm-*,-llvm-include-order,-modernize-use-nodiscard,-llvm-header-guard'
WarningsAsErrors: 'clang-diagnostic-*,clang-analyzer-*,portability-*,llvm-*'
HeaderFilterRegex: 'TrustWallet|^src|^./src'
Checks: >
clang-diagnostic-*,
clang-analyzer-*,
modernize-*,
portability-*,
llvm-*,
-clang-analyzer-core.uninitialized.Assign,
-llvm-include-order,
-llvm-header-guard,
-llvm-else-after-return,
-modernize-use-using,
-modernize-use-nodiscard,
-modernize-use-trailing-return-type,
-modernize-deprecated-headers,
-modernize-avoid-c-arrays
WarningsAsErrors: false
HeaderFilterRegex: '^src|^./src'
AnalyzeTemporaryDtors: false
FormatStyle: none
InheritParentConfig: false
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/linux-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ jobs:
env:
CC: /usr/bin/clang
CXX: /usr/bin/clang++
- name: Prepare build with lint
- name: CMake (coverage/clang-tidy/clang-asan)
if: github.ref == 'refs/heads/master'
run: |
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCLANG_TIDY=ON -DCODE_COVERAGE=ON -DCLANG_ASAN=ON
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCODE_COVERAGE=ON -DCLANG_TIDY=ON -DCLANG_ASAN=ON
env:
CC: /usr/bin/clang
CXX: /usr/bin/clang++
- name: Prepare build without lint
- name: CMake (coverage)
if: github.ref != 'refs/heads/master'
run: |
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCODE_COVERAGE=ON
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ endif()

option(CLANG_TIDY "Enable static code analysis with (clang-tidy)" OFF)
if(CLANG_TIDY)
find_program(CLANG_TIDY_BIN NAMES clang-tidy)
find_program(CLANG_TIDY_BIN NAMES clang-tidy-11)
if(CLANG_TIDY_BIN)
set(CMAKE_CXX_CLANG_TIDY clang-tidy;)
set(CMAKE_CXX_CLANG_TIDY clang-tidy-11;)
message("clang-tidy ${CMAKE_CXX_CLANG_TIDY} ${CLANG_TIDY_BIN}")
else()
message(FATAL_ERROR "Could not find clang-tidy")
Expand Down
5 changes: 2 additions & 3 deletions src/Aeternity/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ TW::Data Transaction::buildTag(const std::string& address) {
TW::Data Transaction::encodeSafeZero(uint256_t value) {
if (value == 0) {
return Ethereum::RLP::encode(Data{0});
} else {
return Ethereum::RLP::encode(value);
}
}
return Ethereum::RLP::encode(value);
}
1 change: 1 addition & 0 deletions src/Algorand/BinaryCoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace TW::Algorand {

#pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#pragma GCC diagnostic ignored "-Wunused-function"

static inline void encodeString(std::string string, Data& data) {
// encode string header
Expand Down
2 changes: 1 addition & 1 deletion src/Base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Data Base58::decodeCheck(const char* begin, const char* end, Hash::Hasher hasher
}

Data Base58::decode(const char* begin, const char* end) const {
auto it = begin;
const auto* it = begin;

// Skip leading spaces.
it = std::find_if_not(it, end, [](char c) { return std::isspace(c);});
Expand Down
18 changes: 12 additions & 6 deletions src/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ void convertFromBase64Url(string& b) {
size_t n = b.length();
char* start = b.data();
char* end = start + n;
for(auto p = start; p < end; ++p) {
if (*p == '-') { *p = '+'; }
else if (*p == '_') { *p = '/'; }
for (auto* p = start; p < end; ++p) {
if (*p == '-') {
*p = '+';
} else if (*p == '_') {
*p = '/';
}
}
}

Expand All @@ -50,9 +53,12 @@ void convertToBase64Url(string& b) {
size_t n = b.length();
char* start = b.data();
char* end = start + n;
for(auto p = start; p < end; ++p) {
if (*p == '+') { *p = '-'; }
else if (*p == '/') { *p = '_'; }
for (auto* p = start; p < end; ++p) {
if (*p == '+') {
*p = '-';
} else if (*p == '/') {
*p = '_';
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Bitcoin/InputSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ std::vector<TypeWithAmount> InputSelector<TypeWithAmount>::select(int64_t target

// difference from 2x targetValue
auto distFrom2x = [doubleTargetValue](int64_t val) -> int64_t {
if (val > doubleTargetValue)
if (val > doubleTargetValue) {
return val - doubleTargetValue;
else
return doubleTargetValue - val;
}
return doubleTargetValue - val;
};

const int64_t dustThreshold = feeCalculator.calculateSingleInput(byteFee);
Expand Down Expand Up @@ -166,7 +166,7 @@ std::vector<TypeWithAmount> InputSelector<TypeWithAmount>::selectSimple(int64_t
assert(inputs.size() >= 1);

// target value is larger that original, but not by a factor of 2 (optioized for large UTXO cases)
const uint64_t increasedTargetValue = (uint64_t)((double)targetValue * 1.1 + feeCalculator.calculate(inputs.size(), numOutputs, byteFee) + 1000);
const auto increasedTargetValue = (uint64_t)((double)targetValue * 1.1 + feeCalculator.calculate(inputs.size(), numOutputs, byteFee) + 1000);

const int64_t dustThreshold = feeCalculator.calculateSingleInput(byteFee);

Expand Down
8 changes: 4 additions & 4 deletions src/Bitcoin/SignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ Result<void, Common::Proto::SigningError> SignatureBuilder<Transaction>::sign(Sc
uint32_t signatureVersion = [this]() {
if ((input.hashType & TWBitcoinSigHashTypeFork) != 0) {
return WITNESS_V0;
} else {
return BASE;
}
}
return BASE;
}();
auto result = signStep(script, index, utxo, signatureVersion);
if (!result) {
Expand Down Expand Up @@ -274,7 +273,8 @@ std::optional<KeyPair> SignatureBuilder<Transaction>::keyPairForPubKeyHash(const
auto pubKey = pubKeyExtended.compressed();
if (Hash::sha256ripemd(pubKey.bytes.data(), pubKey.bytes.size()) == hash) {
return std::make_tuple(key, pubKey);
} else if (Hash::sha256ripemd(pubKeyExtended.bytes.data(), pubKeyExtended.bytes.size()) == hash) {
}
if (Hash::sha256ripemd(pubKeyExtended.bytes.data(), pubKeyExtended.bytes.size()) == hash) {
return std::make_tuple(key, pubKeyExtended);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bitcoin/SigningInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SigningInput::SigningInput(const Proto::SigningInput& input) {
toAddress = input.to_address();
changeAddress = input.change_address();
for (auto& key: input.private_key()) {
privateKeys.push_back(PrivateKey(key));
privateKeys.emplace_back(PrivateKey(key));
}
for (auto& script: input.scripts()) {
scripts[script.first] = Script(script.second.begin(), script.second.end());
Expand Down
4 changes: 2 additions & 2 deletions src/Bitcoin/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Proto::Transaction Transaction::proto() const {
protoTx.set_locktime(lockTime);

for (const auto& input : inputs) {
auto protoInput = protoTx.add_inputs();
auto* protoInput = protoTx.add_inputs();
protoInput->mutable_previousoutput()->set_hash(input.previousOutput.hash.data(),
input.previousOutput.hash.size());
protoInput->mutable_previousoutput()->set_index(input.previousOutput.index);
Expand All @@ -249,7 +249,7 @@ Proto::Transaction Transaction::proto() const {
}

for (const auto& output : outputs) {
auto protoOutput = protoTx.add_outputs();
auto* protoOutput = protoTx.add_outputs();
protoOutput->set_value(output.value);
protoOutput->set_script(output.script.bytes.data(), output.script.bytes.size());
}
Expand Down
8 changes: 1 addition & 7 deletions src/Cardano/AddressV3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,7 @@ AddressV3::AddressV3(const Data& data) {
std::copy(data.begin() + index, data.begin() + index + len2, groupKey.begin());
}

AddressV3::AddressV3(const AddressV3& other) :
discrimination(other.discrimination),
kind(other.kind),
key1(other.key1),
groupKey(other.groupKey),
legacyAddressV2(other.legacyAddressV2)
{}
AddressV3::AddressV3(const AddressV3& other) = default;

void AddressV3::operator=(const AddressV3& other)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ vector<pair<Decode, Decode>> Decode::getMapElements() const {
auto elems = getCompoundElements(2, MT_map);
vector<pair<Decode, Decode>> map;
for (int i = 0; i < elems.size(); i += 2) {
map.push_back(make_pair(elems[i], elems[i + 1]));
map.emplace_back(make_pair(elems[i], elems[i + 1]));
}
return map;
}
Expand Down
47 changes: 23 additions & 24 deletions src/Coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@
#include "Aeternity/Entry.h"
#include "Aion/Entry.h"
#include "Algorand/Entry.h"
#include "Bitcoin/Entry.h"
#include "Binance/Entry.h"
#include "Bitcoin/Entry.h"
#include "Cardano/Entry.h"
#include "Cosmos/Entry.h"
#include "Decred/Entry.h"
#include "Elrond/Entry.h"
#include "EOS/Entry.h"
#include "Elrond/Entry.h"
#include "Ethereum/Entry.h"
#include "Filecoin/Entry.h"
#include "FIO/Entry.h"
#include "Filecoin/Entry.h"
#include "Groestlcoin/Entry.h"
#include "Harmony/Entry.h"
#include "Icon/Entry.h"
#include "IoTeX/Entry.h"
#include "Kusama/Entry.h"
#include "Nano/Entry.h"
#include "NEAR/Entry.h"
#include "Nebulas/Entry.h"
#include "NEO/Entry.h"
#include "Nimiq/Entry.h"
#include "NULS/Entry.h"
#include "Nano/Entry.h"
#include "Nebulas/Entry.h"
#include "Nimiq/Entry.h"
#include "Oasis/Entry.h"
#include "Ontology/Entry.h"
#include "Polkadot/Entry.h"
#include "Ripple/Entry.h"
#include "Solana/Entry.h"
#include "Stellar/Entry.h"
#include "THORChain/Entry.h"
#include "Tezos/Entry.h"
#include "Theta/Entry.h"
#include "THORChain/Entry.h"
#include "Tron/Entry.h"
#include "TrustWalletCore/TWCoinType.h"
#include "VeChain/Entry.h"
#include "Waves/Entry.h"
#include "Zcash/Entry.h"
#include "Zilliqa/Entry.h"
#include "Oasis/Entry.h"
// end_of_coin_includes_marker_do_not_modify

using namespace TW;
Expand Down Expand Up @@ -188,10 +188,10 @@ CoinEntry* coinDispatcher(TWCoinType coinType) {
bool TW::validateAddress(TWCoinType coin, const std::string& string) {
auto p2pkh = TW::p2pkhPrefix(coin);
auto p2sh = TW::p2shPrefix(coin);
auto hrp = stringForHRP(TW::hrp(coin));
const auto* hrp = stringForHRP(TW::hrp(coin));

// dispatch
auto dispatcher = coinDispatcher(coin);
auto* dispatcher = coinDispatcher(coin);
assert(dispatcher != nullptr);
return dispatcher->validateAddress(coin, string, p2pkh, p2sh, hrp);
}
Expand All @@ -203,7 +203,7 @@ std::string TW::normalizeAddress(TWCoinType coin, const std::string& address) {
}

// dispatch
auto dispatcher = coinDispatcher(coin);
auto* dispatcher = coinDispatcher(coin);
assert(dispatcher != nullptr);
return dispatcher->normalizeAddress(coin, address);
}
Expand All @@ -215,34 +215,34 @@ std::string TW::deriveAddress(TWCoinType coin, const PrivateKey& privateKey) {

std::string TW::deriveAddress(TWCoinType coin, const PublicKey& publicKey) {
auto p2pkh = TW::p2pkhPrefix(coin);
auto hrp = stringForHRP(TW::hrp(coin));
const auto* hrp = stringForHRP(TW::hrp(coin));

// dispatch
auto dispatcher = coinDispatcher(coin);
auto* dispatcher = coinDispatcher(coin);
assert(dispatcher != nullptr);
return dispatcher->deriveAddress(coin, publicKey, p2pkh, hrp);
}

void TW::anyCoinSign(TWCoinType coinType, const Data& dataIn, Data& dataOut) {
auto dispatcher = coinDispatcher(coinType);
auto* dispatcher = coinDispatcher(coinType);
assert(dispatcher != nullptr);
dispatcher->sign(coinType, dataIn, dataOut);
}

std::string TW::anySignJSON(TWCoinType coinType, const std::string& json, const Data& key) {
auto dispatcher = coinDispatcher(coinType);
auto* dispatcher = coinDispatcher(coinType);
assert(dispatcher != nullptr);
return dispatcher->signJSON(coinType, json, key);
}

bool TW::supportsJSONSigning(TWCoinType coinType) {
auto dispatcher = coinDispatcher(coinType);
auto* dispatcher = coinDispatcher(coinType);
assert(dispatcher != nullptr);
return dispatcher->supportsJSONSigning();
}

void TW::anyCoinPlan(TWCoinType coinType, const Data& dataIn, Data& dataOut) {
auto dispatcher = coinDispatcher(coinType);
auto* dispatcher = coinDispatcher(coinType);
assert(dispatcher != nullptr);
dispatcher->plan(coinType, dataIn, dataOut);
}
Expand Down Expand Up @@ -307,37 +307,36 @@ uint32_t TW::slip44Id(TWCoinType coin) {
return getCoinInfo(coin).slip44;
}

TWString *_Nullable TWCoinTypeConfigurationGetSymbol(enum TWCoinType coin) {
TWString* _Nullable TWCoinTypeConfigurationGetSymbol(enum TWCoinType coin) {
return TWStringCreateWithUTF8Bytes(getCoinInfo(coin).symbol);
}

int TWCoinTypeConfigurationGetDecimals(enum TWCoinType coin) {
return getCoinInfo(coin).decimals;
}

TWString *_Nullable TWCoinTypeConfigurationGetTransactionURL(enum TWCoinType coin, TWString *_Nonnull transactionID) {
TWString* _Nullable TWCoinTypeConfigurationGetTransactionURL(enum TWCoinType coin, TWString* _Nonnull transactionID) {
std::string txId = TWStringUTF8Bytes(transactionID);
std::string url = getCoinInfo(coin).explorerTransactionUrl + txId;
return TWStringCreateWithUTF8Bytes(url.c_str());
}

TWString *_Nullable TWCoinTypeConfigurationGetAccountURL(enum TWCoinType coin, TWString *_Nonnull accountID) {
TWString* _Nullable TWCoinTypeConfigurationGetAccountURL(enum TWCoinType coin, TWString* _Nonnull accountID) {
std::string accId = TWStringUTF8Bytes(accountID);
std::string url = getCoinInfo(coin).explorerAccountUrl + accId;
return TWStringCreateWithUTF8Bytes(url.c_str());
}

TWString *_Nonnull TWCoinTypeConfigurationGetID(enum TWCoinType coin) {
TWString* _Nonnull TWCoinTypeConfigurationGetID(enum TWCoinType coin) {
return TWStringCreateWithUTF8Bytes(getCoinInfo(coin).id);
}

TWString *_Nonnull TWCoinTypeConfigurationGetName(enum TWCoinType coin) {
TWString* _Nonnull TWCoinTypeConfigurationGetName(enum TWCoinType coin) {
return TWStringCreateWithUTF8Bytes(getCoinInfo(coin).name);
}

const std::vector<TWCoinType> TW::getSimilarCoinTypes(TWCoinType coinType) {
const auto dispatcher = coinDispatcher(coinType);
const auto* dispatcher = coinDispatcher(coinType);
assert(dispatcher != nullptr);
return dispatcher->coinTypes();
}

4 changes: 2 additions & 2 deletions src/Decred/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Proto::Transaction Transaction::proto() const {
protoTx.set_locktime(lockTime);

for (const auto& input : inputs) {
auto protoInput = protoTx.add_inputs();
auto* protoInput = protoTx.add_inputs();
protoInput->mutable_previousoutput()->set_hash(input.previousOutput.hash.data(),
input.previousOutput.hash.size());
protoInput->mutable_previousoutput()->set_index(input.previousOutput.index);
Expand All @@ -207,7 +207,7 @@ Proto::Transaction Transaction::proto() const {
}

for (const auto& output : outputs) {
auto protoOutput = protoTx.add_outputs();
auto* protoOutput = protoTx.add_outputs();
protoOutput->set_value(output.value);
protoOutput->set_script(output.script.bytes.data(), output.script.bytes.size());
}
Expand Down
4 changes: 2 additions & 2 deletions src/DerivationPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
using namespace TW;

DerivationPath::DerivationPath(const std::string& string) {
auto it = string.data();
const auto end = string.data() + string.size();
const auto* it = string.data();
const auto* end = string.data() + string.size();

if (it != end && *it == 'm') {
++it;
Expand Down
Loading

0 comments on commit a771f38

Please sign in to comment.