Skip to content

Commit 9cc0230

Browse files
Add NODISCARD to all {Decode,Parse}[...](...) functions returning bool. Sort includes.
1 parent 579497e commit 9cc0230

File tree

11 files changed

+58
-26
lines changed

11 files changed

+58
-26
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ endif
9595
BITCOIN_CORE_H = \
9696
addrdb.h \
9797
addrman.h \
98+
attributes.h \
9899
base58.h \
99100
bech32.h \
100101
bloom.h \

src/attributes.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2018 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_ATTRIBUTES_H
7+
#define BITCOIN_ATTRIBUTES_H
8+
9+
#if defined(__has_cpp_attribute)
10+
# if __has_cpp_attribute(nodiscard)
11+
# define NODISCARD [[nodiscard]]
12+
# endif
13+
#endif
14+
#ifndef NODISCARD
15+
# if defined(_MSC_VER) && _MSC_VER >= 1700
16+
# define NODISCARD _Check_return_
17+
# else
18+
# define NODISCARD __attribute__((warn_unused_result))
19+
# endif
20+
#endif
21+
22+
#endif // BITCOIN_ATTRIBUTES_H

src/base58.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#ifndef BITCOIN_BASE58_H
1515
#define BITCOIN_BASE58_H
1616

17+
#include <attributes.h>
18+
1719
#include <string>
1820
#include <vector>
1921

@@ -33,13 +35,13 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch);
3335
* return true if decoding is successful.
3436
* psz cannot be nullptr.
3537
*/
36-
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
38+
NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
3739

3840
/**
3941
* Decode a base58-encoded string (str) into a byte vector (vchRet).
4042
* return true if decoding is successful.
4143
*/
42-
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
44+
NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
4345

4446
/**
4547
* Encode a byte vector into a base58-encoded string, including checksum
@@ -50,12 +52,12 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
5052
* Decode a base58-encoded string (psz) that includes a checksum into a byte
5153
* vector (vchRet), return true if decoding is successful
5254
*/
53-
bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
55+
NODISCARD bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
5456

5557
/**
5658
* Decode a base58-encoded string (str) that includes a checksum into a byte
5759
* vector (vchRet), return true if decoding is successful
5860
*/
59-
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
61+
NODISCARD bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
6062

6163
#endif // BITCOIN_BASE58_H

src/core_io.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_CORE_IO_H
77

88
#include <amount.h>
9+
#include <attributes.h>
910

1011
#include <string>
1112
#include <vector>
@@ -22,8 +23,8 @@ class UniValue;
2223
// core_read.cpp
2324
CScript ParseScript(const std::string& s);
2425
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode = false);
25-
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true);
26-
bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
26+
NODISCARD bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true);
27+
NODISCARD bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
2728
bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
2829

2930
/**
@@ -36,7 +37,7 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
3637
*/
3738
bool ParseHashStr(const std::string& strHex, uint256& result);
3839
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
39-
bool DecodePSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error);
40+
NODISCARD bool DecodePSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error);
4041
int ParseSighashString(const UniValue& sighash);
4142

4243
// core_write.cpp

src/outputtype.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_OUTPUTTYPE_H
77
#define BITCOIN_OUTPUTTYPE_H
88

9+
#include <attributes.h>
910
#include <keystore.h>
1011
#include <script/standard.h>
1112

@@ -26,7 +27,7 @@ enum class OutputType {
2627
CHANGE_AUTO,
2728
};
2829

29-
bool ParseOutputType(const std::string& str, OutputType& output_type);
30+
NODISCARD bool ParseOutputType(const std::string& str, OutputType& output_type);
3031
const std::string& FormatOutputType(OutputType type);
3132

3233
/**

src/rest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
#include <attributes.h>
67
#include <chain.h>
78
#include <chainparams.h>
89
#include <core_io.h>
10+
#include <httpserver.h>
911
#include <index/txindex.h>
1012
#include <primitives/block.h>
1113
#include <primitives/transaction.h>
12-
#include <validation.h>
13-
#include <httpserver.h>
1414
#include <rpc/blockchain.h>
1515
#include <rpc/server.h>
1616
#include <streams.h>
1717
#include <sync.h>
1818
#include <txmempool.h>
1919
#include <util/strencodings.h>
20+
#include <validation.h>
2021
#include <version.h>
2122

2223
#include <boost/algorithm/string.hpp>

src/script/descriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ std::vector<Span<const char>> Split(const Span<const char>& sp, char sep)
478478
}
479479

480480
/** Parse a key path, being passed a split list of elements (the first element is ignored). */
481-
bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath& out)
481+
NODISCARD bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath& out)
482482
{
483483
for (size_t i = 1; i < split.size(); ++i) {
484484
Span<const char> elem = split[i];

src/util/moneystr.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
#ifndef BITCOIN_UTIL_MONEYSTR_H
1010
#define BITCOIN_UTIL_MONEYSTR_H
1111

12-
#include <stdint.h>
13-
#include <string>
14-
1512
#include <amount.h>
13+
#include <attributes.h>
14+
15+
#include <cstdint>
16+
#include <string>
1617

1718
/* Do not use these functions to represent or parse monetary amounts to or from
1819
* JSON but use AmountFromValue and ValueFromAmount for that.
1920
*/
2021
std::string FormatMoney(const CAmount& n);
21-
bool ParseMoney(const std::string& str, CAmount& nRet);
22-
bool ParseMoney(const char* pszIn, CAmount& nRet);
22+
NODISCARD bool ParseMoney(const std::string& str, CAmount& nRet);
23+
NODISCARD bool ParseMoney(const char* pszIn, CAmount& nRet);
2324

2425
#endif // BITCOIN_UTIL_MONEYSTR_H

src/util/strencodings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ std::string DecodeBase32(const std::string& str)
263263
return std::string((const char*)vchRet.data(), vchRet.size());
264264
}
265265

266-
static bool ParsePrechecks(const std::string& str)
266+
NODISCARD static bool ParsePrechecks(const std::string& str)
267267
{
268268
if (str.empty()) // No empty string allowed
269269
return false;

src/util/strencodings.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#ifndef BITCOIN_UTIL_STRENCODINGS_H
1010
#define BITCOIN_UTIL_STRENCODINGS_H
1111

12-
#include <stdint.h>
12+
#include <attributes.h>
13+
14+
#include <cstdint>
1315
#include <string>
1416
#include <vector>
1517

@@ -92,35 +94,35 @@ constexpr inline bool IsSpace(char c) noexcept {
9294
* @returns true if the entire string could be parsed as valid integer,
9395
* false if not the entire string could be parsed or when overflow or underflow occurred.
9496
*/
95-
bool ParseInt32(const std::string& str, int32_t *out);
97+
NODISCARD bool ParseInt32(const std::string& str, int32_t *out);
9698

9799
/**
98100
* Convert string to signed 64-bit integer with strict parse error feedback.
99101
* @returns true if the entire string could be parsed as valid integer,
100102
* false if not the entire string could be parsed or when overflow or underflow occurred.
101103
*/
102-
bool ParseInt64(const std::string& str, int64_t *out);
104+
NODISCARD bool ParseInt64(const std::string& str, int64_t *out);
103105

104106
/**
105107
* Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
106108
* @returns true if the entire string could be parsed as valid integer,
107109
* false if not the entire string could be parsed or when overflow or underflow occurred.
108110
*/
109-
bool ParseUInt32(const std::string& str, uint32_t *out);
111+
NODISCARD bool ParseUInt32(const std::string& str, uint32_t *out);
110112

111113
/**
112114
* Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
113115
* @returns true if the entire string could be parsed as valid integer,
114116
* false if not the entire string could be parsed or when overflow or underflow occurred.
115117
*/
116-
bool ParseUInt64(const std::string& str, uint64_t *out);
118+
NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
117119

118120
/**
119121
* Convert string to double with strict parse error feedback.
120122
* @returns true if the entire string could be parsed as valid double,
121123
* false if not the entire string could be parsed or when overflow or underflow occurred.
122124
*/
123-
bool ParseDouble(const std::string& str, double *out);
125+
NODISCARD bool ParseDouble(const std::string& str, double *out);
124126

125127
template<typename T>
126128
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
@@ -173,7 +175,7 @@ bool TimingResistantEqual(const T& a, const T& b)
173175
* @returns true on success, false on error.
174176
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
175177
*/
176-
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
178+
NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
177179

178180
/** Convert from one power-of-2 number base to another. */
179181
template<int frombits, int tobits, bool pad, typename O, typename I>
@@ -200,7 +202,7 @@ bool ConvertBits(const O& outfn, I it, I end) {
200202
}
201203

202204
/** Parse an HD keypaths like "m/7/0'/2000". */
203-
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
205+
NODISCARD bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
204206

205207
/**
206208
* Converts the given character to its lowercase equivalent.

0 commit comments

Comments
 (0)