1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-31 03:12:44 +00:00

refactor: add [[nodiscard]] (#3793)

This commit is contained in:
Charles Kerr 2022-09-08 21:49:51 -05:00 committed by GitHub
parent afa043c788
commit c0f29a89ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 58 additions and 53 deletions

View file

@ -89,7 +89,7 @@ void tr_x509_cert_free(tr_x509_cert_t handle);
/**
* @brief Returns a random number in the range of [0...upper_bound).
*/
int tr_rand_int(int upper_bound);
[[nodiscard]] int tr_rand_int(int upper_bound);
/**
* @brief Returns a pseudorandom number in the range of [0...upper_bound).
@ -97,7 +97,7 @@ int tr_rand_int(int upper_bound);
* This is faster, BUT WEAKER, than tr_rand_int() and never be used in sensitive cases.
* @see tr_rand_int()
*/
int tr_rand_int_weak(int upper_bound);
[[nodiscard]] int tr_rand_int_weak(int upper_bound);
/**
* @brief Fill a buffer with random bytes.
@ -107,52 +107,53 @@ bool tr_rand_buffer(void* buffer, size_t length);
/**
* @brief Generate a SSHA password from its plaintext source.
*/
std::string tr_ssha1(std::string_view plaintext);
[[nodiscard]] std::string tr_ssha1(std::string_view plaintext);
/**
* @brief Return true if this is salted text, false otherwise
*/
bool tr_ssha1_test(std::string_view text);
[[nodiscard]] bool tr_ssha1_test(std::string_view text);
/**
* @brief Validate a test password against the a ssha1 password.
*/
bool tr_ssha1_matches(std::string_view ssha1, std::string_view plaintext);
[[nodiscard]] bool tr_ssha1_matches(std::string_view ssha1, std::string_view plaintext);
/**
* @brief Translate null-terminated string into base64.
* @return a new std::string with the encoded contents
*/
std::string tr_base64_encode(std::string_view input);
[[nodiscard]] std::string tr_base64_encode(std::string_view input);
/**
* @brief Translate a character range from base64 into raw form.
* @return a new std::string with the decoded contents.
*/
std::string tr_base64_decode(std::string_view input);
[[nodiscard]] std::string tr_base64_decode(std::string_view input);
/**
* @brief Generate an ascii hex string for a sha1 digest.
*/
std::string tr_sha1_to_string(tr_sha1_digest_t const&);
[[nodiscard]] std::string tr_sha1_to_string(tr_sha1_digest_t const&);
/**
* @brief Generate a sha1 digest from a hex string.
*/
std::optional<tr_sha1_digest_t> tr_sha1_from_string(std::string_view hex);
[[nodiscard]] std::optional<tr_sha1_digest_t> tr_sha1_from_string(std::string_view hex);
/**
* @brief Generate an ascii hex string for a sha256 digest.
*/
std::string tr_sha256_to_string(tr_sha256_digest_t const&);
[[nodiscard]] std::string tr_sha256_to_string(tr_sha256_digest_t const&);
/**
* @brief Generate a sha256 digest from a hex string.
*/
std::optional<tr_sha256_digest_t> tr_sha256_from_string(std::string_view hex);
[[nodiscard]] std::optional<tr_sha256_digest_t> tr_sha256_from_string(std::string_view hex);
// Convenience utility to efficiently get many random small values.
// Use this instead of making a lot of calls to tr_rand_int().
template<typename T = uint8_t, size_t N = 1024U>
class tr_salt_shaker
{
public:
@ -165,7 +166,7 @@ public:
if (pos == 0U)
{
tr_rand_buffer(std::data(buf), std::size(buf));
tr_rand_buffer(std::data(buf), std::size(buf) * sizeof(T));
}
return buf[pos++];
@ -173,7 +174,7 @@ public:
private:
size_t pos = 0;
std::array<uint8_t, 1024U> buf;
std::array<T, N> buf;
};
/** @} */

View file

@ -63,7 +63,7 @@ public:
}
// generate the metainfo
std::string benc(tr_error** error = nullptr) const;
[[nodiscard]] std::string benc(tr_error** error = nullptr) const;
// generate the metainfo and save it to a torrent file
bool save(std::string_view filename, tr_error** error = nullptr) const

View file

@ -231,10 +231,10 @@ bool tr_net_hasIPv6(tr_port);
/// TOS / DSCP
// get a string of one of <netinet/ip.h>'s IPTOS_ values, e.g. "cs0"
std::string tr_netTosToName(int tos);
[[nodiscard]] std::string tr_netTosToName(int tos);
// get the number that corresponds to the specified IPTOS_ name, e.g. "cs0" returns 0x00
std::optional<int> tr_netTosFromName(std::string_view name);
[[nodiscard]] std::optional<int> tr_netTosFromName(std::string_view name);
// set the IPTOS_ value for the specified socket
void tr_netSetTOS(tr_socket_t sock, int tos, tr_address_type type);
@ -243,6 +243,6 @@ void tr_netSetTOS(tr_socket_t sock, int tos, tr_address_type type);
* @brief get a human-representable string representing the network error.
* @param err an errno on Unix/Linux and an WSAError on win32)
*/
std::string tr_net_strerror(int err);
[[nodiscard]] std::string tr_net_strerror(int err);
unsigned char const* tr_globalIPv6(tr_session const* session);

View file

@ -105,7 +105,7 @@ constexpr bool tr_isPex(tr_pex const* pex)
return pex && tr_address_is_valid(&pex->addr);
}
tr_peerMgr* tr_peerMgrNew(tr_session* session);
[[nodiscard]] tr_peerMgr* tr_peerMgrNew(tr_session* session);
void tr_peerMgrFree(tr_peerMgr* manager);
@ -113,19 +113,23 @@ void tr_peerMgrSetUtpSupported(tr_torrent* tor, tr_address const& addr);
void tr_peerMgrSetUtpFailed(tr_torrent* tor, tr_address const& addr, bool failed);
std::vector<tr_block_span_t> tr_peerMgrGetNextRequests(tr_torrent* torrent, tr_peer const* peer, size_t numwant);
[[nodiscard]] std::vector<tr_block_span_t> tr_peerMgrGetNextRequests(tr_torrent* torrent, tr_peer const* peer, size_t numwant);
bool tr_peerMgrDidPeerRequest(tr_torrent const* torrent, tr_peer const* peer, tr_block_index_t block);
[[nodiscard]] bool tr_peerMgrDidPeerRequest(tr_torrent const* torrent, tr_peer const* peer, tr_block_index_t block);
void tr_peerMgrClientSentRequests(tr_torrent* torrent, tr_peer* peer, tr_block_span_t span);
size_t tr_peerMgrCountActiveRequestsToPeer(tr_torrent const* torrent, tr_peer const* peer);
[[nodiscard]] size_t tr_peerMgrCountActiveRequestsToPeer(tr_torrent const* torrent, tr_peer const* peer);
void tr_peerMgrAddIncoming(tr_peerMgr* manager, tr_address const* addr, tr_port port, struct tr_peer_socket const socket);
std::vector<tr_pex> tr_peerMgrCompactToPex(void const* compact, size_t compact_len, uint8_t const* added_f, size_t added_f_len);
[[nodiscard]] std::vector<tr_pex> tr_peerMgrCompactToPex(
void const* compact,
size_t compact_len,
uint8_t const* added_f,
size_t added_f_len);
std::vector<tr_pex> tr_peerMgrCompact6ToPex(
[[nodiscard]] std::vector<tr_pex> tr_peerMgrCompact6ToPex(
void const* compact,
size_t compact_len,
uint8_t const* added_f,
@ -141,7 +145,7 @@ enum
TR_PEERS_INTERESTING
};
std::vector<tr_pex> tr_peerMgrGetPeers(
[[nodiscard]] std::vector<tr_pex> tr_peerMgrGetPeers(
tr_torrent const* tor,
uint8_t address_type,
uint8_t peer_list_mode,
@ -156,21 +160,21 @@ void tr_peerMgrAddTorrent(tr_peerMgr* manager, struct tr_torrent* tor);
void tr_peerMgrRemoveTorrent(tr_torrent* tor);
// return the number of connected peers that have `piece`, or -1 if we already have it
int8_t tr_peerMgrPieceAvailability(tr_torrent const* tor, tr_piece_index_t piece);
[[nodiscard]] int8_t tr_peerMgrPieceAvailability(tr_torrent const* tor, tr_piece_index_t piece);
void tr_peerMgrTorrentAvailability(tr_torrent const* tor, int8_t* tab, unsigned int n_tabs);
uint64_t tr_peerMgrGetDesiredAvailable(tr_torrent const* tor);
[[nodiscard]] uint64_t tr_peerMgrGetDesiredAvailable(tr_torrent const* tor);
void tr_peerMgrOnTorrentGotMetainfo(tr_torrent* tor);
void tr_peerMgrOnBlocklistChanged(tr_peerMgr* mgr);
struct tr_peer_stat* tr_peerMgrPeerStats(tr_torrent const* tor, int* setme_count);
[[nodiscard]] struct tr_peer_stat* tr_peerMgrPeerStats(tr_torrent const* tor, int* setme_count);
tr_webseed_view tr_peerMgrWebseed(tr_torrent const* tor, size_t i);
[[nodiscard]] tr_webseed_view tr_peerMgrWebseed(tr_torrent const* tor, size_t i);
unsigned int tr_peerGetPieceSpeedBytesPerSecond(tr_peer const* peer, uint64_t now, tr_direction direction);
[[nodiscard]] unsigned int tr_peerGetPieceSpeedBytesPerSecond(tr_peer const* peer, uint64_t now, tr_direction direction);
void tr_peerMgrClearInterest(tr_torrent* tor);

View file

@ -39,10 +39,10 @@ void tr_variantToBufBenc(tr_variant const* top, struct evbuffer* buf);
void tr_variantInit(tr_variant* v, char type);
/** @brief Private function that's exposed here only for unit tests */
std::optional<int64_t> tr_bencParseInt(std::string_view* benc_inout);
[[nodiscard]] std::optional<int64_t> tr_bencParseInt(std::string_view* benc_inout);
/** @brief Private function that's exposed here only for unit tests */
std::optional<std::string_view> tr_bencParseStr(std::string_view* benc_inout);
[[nodiscard]] std::optional<std::string_view> tr_bencParseStr(std::string_view* benc_inout);
bool tr_variantParseBenc(tr_variant& top, int parse_opts, std::string_view benc, char const** setme_end, tr_error** error);

View file

@ -154,12 +154,12 @@ bool tr_variantFromBuf(
error);
}
constexpr bool tr_variantIsType(tr_variant const* b, int type)
[[nodiscard]] constexpr bool tr_variantIsType(tr_variant const* b, int type)
{
return b != nullptr && b->type == type;
}
constexpr bool tr_variantIsEmpty(tr_variant const* b)
[[nodiscard]] constexpr bool tr_variantIsEmpty(tr_variant const* b)
{
return b == nullptr || b->type == '\0';
}
@ -168,7 +168,7 @@ constexpr bool tr_variantIsEmpty(tr_variant const* b)
**** Strings
***/
constexpr bool tr_variantIsString(tr_variant const* b)
[[nodiscard]] constexpr bool tr_variantIsString(tr_variant const* b)
{
return b != nullptr && b->type == TR_VARIANT_TYPE_STR;
}
@ -186,7 +186,7 @@ bool tr_variantGetRaw(tr_variant const* variant, uint8_t const** setme_raw, size
**** Real Numbers
***/
constexpr bool tr_variantIsReal(tr_variant const* v)
[[nodiscard]] constexpr bool tr_variantIsReal(tr_variant const* v)
{
return v != nullptr && v->type == TR_VARIANT_TYPE_REAL;
}
@ -198,7 +198,7 @@ bool tr_variantGetReal(tr_variant const* variant, double* value_setme);
**** Booleans
***/
constexpr bool tr_variantIsBool(tr_variant const* v)
[[nodiscard]] constexpr bool tr_variantIsBool(tr_variant const* v)
{
return v != nullptr && v->type == TR_VARIANT_TYPE_BOOL;
}
@ -210,7 +210,7 @@ bool tr_variantGetBool(tr_variant const* variant, bool* setme);
**** Ints
***/
constexpr bool tr_variantIsInt(tr_variant const* v)
[[nodiscard]] constexpr bool tr_variantIsInt(tr_variant const* v)
{
return v != nullptr && v->type == TR_VARIANT_TYPE_INT;
}
@ -222,7 +222,7 @@ bool tr_variantGetInt(tr_variant const* val, int64_t* setme);
**** Lists
***/
constexpr bool tr_variantIsList(tr_variant const* v)
[[nodiscard]] constexpr bool tr_variantIsList(tr_variant const* v)
{
return v != nullptr && v->type == TR_VARIANT_TYPE_LIST;
}
@ -243,13 +243,13 @@ tr_variant* tr_variantListAddDict(tr_variant* list, size_t reserve_count);
tr_variant* tr_variantListChild(tr_variant* list, size_t pos);
bool tr_variantListRemove(tr_variant* list, size_t pos);
size_t tr_variantListSize(tr_variant const* list);
[[nodiscard]] size_t tr_variantListSize(tr_variant const* list);
/***
**** Dictionaries
***/
constexpr bool tr_variantIsDict(tr_variant const* v)
[[nodiscard]] constexpr bool tr_variantIsDict(tr_variant const* v)
{
return v != nullptr && v->type == TR_VARIANT_TYPE_DICT;
}

View file

@ -49,13 +49,13 @@ public:
generic_rescan_interval_ = interval;
}
static std::unique_ptr<Watchdir> create(
[[nodiscard]] static std::unique_ptr<Watchdir> create(
std::string_view dirname,
Callback callback,
libtransmission::TimerMaker& timer_maker,
struct event_base* evbase);
static std::unique_ptr<Watchdir> createGeneric(
[[nodiscard]] static std::unique_ptr<Watchdir> createGeneric(
std::string_view dirname,
Callback callback,
libtransmission::TimerMaker& timer_maker,

View file

@ -39,11 +39,11 @@ struct tr_url_parsed_t
uint16_t port = 0;
};
std::optional<tr_url_parsed_t> tr_urlParse(std::string_view url);
[[nodiscard]] std::optional<tr_url_parsed_t> tr_urlParse(std::string_view url);
// like tr_urlParse(), but with the added constraint that 'scheme'
// must be one we that Transmission supports for announce and scrape
std::optional<tr_url_parsed_t> tr_urlParseTracker(std::string_view url);
[[nodiscard]] std::optional<tr_url_parsed_t> tr_urlParseTracker(std::string_view url);
// example use: `for (auto const [key, val] : tr_url_query_view{ querystr })`
struct tr_url_query_view
@ -62,30 +62,30 @@ struct tr_url_query_view
iterator& operator++();
constexpr auto const& operator*() const
[[nodiscard]] constexpr auto const& operator*() const
{
return keyval;
}
constexpr auto const* operator->() const
[[nodiscard]] constexpr auto const* operator->() const
{
return &keyval;
}
constexpr bool operator==(iterator const& that) const
[[nodiscard]] constexpr bool operator==(iterator const& that) const
{
return this->remain == that.remain && this->keyval == that.keyval;
}
constexpr bool operator!=(iterator const& that) const
[[nodiscard]] constexpr bool operator!=(iterator const& that) const
{
return !(*this == that);
}
};
iterator begin() const;
[[nodiscard]] iterator begin() const;
constexpr iterator end() const
[[nodiscard]] constexpr iterator end() const
{
return iterator{};
}
@ -126,6 +126,6 @@ constexpr void tr_urlPercentEncode(BackInsertIter out, tr_sha1_digest_t const& d
tr_urlPercentEncode(out, std::string_view{ reinterpret_cast<char const*>(digest.data()), std::size(digest) });
}
char const* tr_webGetResponseStr(long response_code);
[[nodiscard]] char const* tr_webGetResponseStr(long response_code);
std::string tr_urlPercentDecode(std::string_view);
[[nodiscard]] std::string tr_urlPercentDecode(std::string_view);

View file

@ -130,7 +130,7 @@ TEST(WebUtilsTest, urlParseFuzz)
{
buf.resize(tr_rand_int(1024));
tr_rand_buffer(std::data(buf), std::size(buf));
tr_urlParse({ std::data(buf), std::size(buf) });
(void)tr_urlParse({ std::data(buf), std::size(buf) });
}
}