From fd5804fbb794e179c7929ea3fe17ad994c3567fb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 14 Oct 2021 14:26:38 -0500 Subject: [PATCH] refactor: add readability-else-after-return to clang-tidy (#1956) * refactor: add readability-else-after-return to clang-tidy --- libtransmission/.clang-tidy | 4 +- libtransmission/clients.cc | 2 +- libtransmission/completion.cc | 77 +++++++++++++---------------- libtransmission/crypto-utils.cc | 12 ++--- libtransmission/handshake.cc | 23 ++++----- libtransmission/net.cc | 43 ++++++---------- libtransmission/peer-io.cc | 6 +-- libtransmission/peer-mgr.cc | 27 +++++----- libtransmission/peer-msgs.cc | 88 +++++++++++++++------------------ libtransmission/tr-dht.cc | 12 ++--- libtransmission/tr-lpd.cc | 6 +-- libtransmission/variant-benc.cc | 11 ++--- 12 files changed, 133 insertions(+), 178 deletions(-) diff --git a/libtransmission/.clang-tidy b/libtransmission/.clang-tidy index 7e06d5726..b641dd596 100644 --- a/libtransmission/.clang-tidy +++ b/libtransmission/.clang-tidy @@ -7,5 +7,7 @@ Checks: > modernize-use-default-member-init, modernize-use-nullptr, modernize-use-override, - modernize-use-using + modernize-use-using, + readability-avoid-const-params-in-decls, + readability-else-after-return diff --git a/libtransmission/clients.cc b/libtransmission/clients.cc index 45988aee3..04b013aac 100644 --- a/libtransmission/clients.cc +++ b/libtransmission/clients.cc @@ -66,7 +66,7 @@ constexpr std::pair buf_append(char* buf, size_t buflen, int n) } template -constexpr std::pair buf_append(char* buf, size_t buflen, T const t, ArgTypes... args) +constexpr std::pair buf_append(char* buf, size_t buflen, T t, ArgTypes... args) { std::tie(buf, buflen) = buf_append(buf, buflen, t); return buf_append(buf, buflen, args...); diff --git a/libtransmission/completion.cc b/libtransmission/completion.cc index 08ab7fec8..17c9bc3de 100644 --- a/libtransmission/completion.cc +++ b/libtransmission/completion.cc @@ -243,13 +243,11 @@ size_t tr_cpMissingBlocksInPiece(tr_completion const* cp, tr_piece_index_t piece { return 0; } - else - { - tr_block_index_t f; - tr_block_index_t l; - tr_torGetPieceBlockRange(cp->tor, piece, &f, &l); - return (l + 1 - f) - cp->blockBitfield->countRange(f, l + 1); - } + + auto f = tr_block_index_t{}; + auto l = tr_block_index_t{}; + tr_torGetPieceBlockRange(cp->tor, piece, &f, &l); + return (l + 1 - f) - cp->blockBitfield->countRange(f, l + 1); } size_t tr_cpMissingBytesInPiece(tr_completion const* cp, tr_piece_index_t piece) @@ -258,31 +256,29 @@ size_t tr_cpMissingBytesInPiece(tr_completion const* cp, tr_piece_index_t piece) { return 0; } - else + + auto f = tr_block_index_t{}; + auto l = tr_block_index_t{}; + size_t const pieceByteSize = tr_torPieceCountBytes(cp->tor, piece); + tr_torGetPieceBlockRange(cp->tor, piece, &f, &l); + + auto haveBytes = size_t{}; + if (f != l) { - size_t haveBytes = 0; - tr_block_index_t f; - tr_block_index_t l; - size_t const pieceByteSize = tr_torPieceCountBytes(cp->tor, piece); - tr_torGetPieceBlockRange(cp->tor, piece, &f, &l); - - if (f != l) - { - /* nb: we don't pass the usual l+1 here to Bitfield::countRange(). - It's faster to handle the last block separately because its size - needs to be checked separately. */ - haveBytes = cp->blockBitfield->countRange(f, l); - haveBytes *= cp->tor->blockSize; - } - - if (cp->blockBitfield->readBit(l)) /* handle the last block */ - { - haveBytes += tr_torBlockCountBytes(cp->tor, l); - } - - TR_ASSERT(haveBytes <= pieceByteSize); - return pieceByteSize - haveBytes; + /* nb: we don't pass the usual l+1 here to Bitfield::countRange(). + It's faster to handle the last block separately because its size + needs to be checked separately. */ + haveBytes = cp->blockBitfield->countRange(f, l); + haveBytes *= cp->tor->blockSize; } + + if (cp->blockBitfield->readBit(l)) /* handle the last block */ + { + haveBytes += tr_torBlockCountBytes(cp->tor, l); + } + + TR_ASSERT(haveBytes <= pieceByteSize); + return pieceByteSize - haveBytes; } bool tr_cpFileIsComplete(tr_completion const* cp, tr_file_index_t i) @@ -291,13 +287,11 @@ bool tr_cpFileIsComplete(tr_completion const* cp, tr_file_index_t i) { return true; } - else - { - tr_block_index_t f; - tr_block_index_t l; - tr_torGetFileBlockRange(cp->tor, i, &f, &l); - return cp->blockBitfield->countRange(f, l + 1) == (l + 1 - f); - } + + auto f = tr_block_index_t{}; + auto l = tr_block_index_t{}; + tr_torGetFileBlockRange(cp->tor, i, &f, &l); + return cp->blockBitfield->countRange(f, l + 1) == (l + 1 - f); } void* tr_cpCreatePieceBitfield(tr_completion const* cp, size_t* byte_count) @@ -340,14 +334,13 @@ double tr_cpPercentComplete(tr_completion const* cp) { return 0.0; } - else if ((int)ratio == TR_RATIO_INF) + + if ((int)ratio == TR_RATIO_INF) { return 1.0; } - else - { - return ratio; - } + + return ratio; } double tr_cpPercentDone(tr_completion const* cp) diff --git a/libtransmission/crypto-utils.cc b/libtransmission/crypto-utils.cc index fb40c705d..1a8353308 100644 --- a/libtransmission/crypto-utils.cc +++ b/libtransmission/crypto-utils.cc @@ -223,10 +223,8 @@ void* tr_base64_encode(void const* input, size_t input_length, size_t* output_le return ret; } - else - { - ret = tr_strdup(""); - } + + ret = tr_strdup(""); } else { @@ -271,10 +269,8 @@ void* tr_base64_decode(void const* input, size_t input_length, size_t* output_le return ret; } - else - { - ret = tr_strdup(""); - } + + ret = tr_strdup(""); } else { diff --git a/libtransmission/handshake.cc b/libtransmission/handshake.cc index 91ae6870c..74fcf5a1d 100644 --- a/libtransmission/handshake.cc +++ b/libtransmission/handshake.cc @@ -659,11 +659,9 @@ static ReadState readHandshake(tr_handshake* handshake, struct evbuffer* inbuf) dbgmsg(handshake, "peer is trying to connect to us for a torrent we don't have."); return tr_handshakeDone(handshake, false); } - else - { - TR_ASSERT(!tr_peerIoHasTorrentHash(handshake->io)); - tr_peerIoSetTorrentHash(handshake->io, hash); - } + + TR_ASSERT(!tr_peerIoHasTorrentHash(handshake->io)); + tr_peerIoSetTorrentHash(handshake->io, hash); } else /* outgoing */ { @@ -775,17 +773,14 @@ static ReadState readPadA(tr_handshake* handshake, struct evbuffer* inbuf) setState(handshake, AWAITING_CRYPTO_PROVIDE); return READ_NOW; } - else + + size_t const len = evbuffer_get_length(inbuf); + if (len > SHA_DIGEST_LENGTH) { - size_t const len = evbuffer_get_length(inbuf); - - if (len > SHA_DIGEST_LENGTH) - { - evbuffer_drain(inbuf, len - SHA_DIGEST_LENGTH); - } - - return READ_LATER; + evbuffer_drain(inbuf, len - SHA_DIGEST_LENGTH); } + + return READ_LATER; } static ReadState readCryptoProvide(tr_handshake* handshake, struct evbuffer* inbuf) diff --git a/libtransmission/net.cc b/libtransmission/net.cc index a94fab709..0dcf19d55 100644 --- a/libtransmission/net.cc +++ b/libtransmission/net.cc @@ -90,14 +90,8 @@ char const* tr_address_to_string_with_buf(tr_address const* addr, char* buf, siz { TR_ASSERT(tr_address_is_valid(addr)); - if (addr->type == TR_AF_INET) - { - return evutil_inet_ntop(AF_INET, &addr->addr, buf, buflen); - } - else - { - return evutil_inet_ntop(AF_INET6, &addr->addr, buf, buflen); - } + return addr->type == TR_AF_INET ? evutil_inet_ntop(AF_INET, &addr->addr, buf, buflen) : + evutil_inet_ntop(AF_INET6, &addr->addr, buf, buflen); } /* @@ -231,25 +225,21 @@ static socklen_t setup_sockaddr(tr_address const* addr, tr_port port, struct soc if (addr->type == TR_AF_INET) { - struct sockaddr_in sock4; - memset(&sock4, 0, sizeof(sock4)); + sockaddr_in sock4 = {}; sock4.sin_family = AF_INET; sock4.sin_addr.s_addr = addr->addr.addr4.s_addr; sock4.sin_port = port; memcpy(sockaddr, &sock4, sizeof(sock4)); return sizeof(struct sockaddr_in); } - else - { - struct sockaddr_in6 sock6; - memset(&sock6, 0, sizeof(sock6)); - sock6.sin6_family = AF_INET6; - sock6.sin6_port = port; - sock6.sin6_flowinfo = 0; - sock6.sin6_addr = addr->addr.addr6; - memcpy(sockaddr, &sock6, sizeof(sock6)); - return sizeof(struct sockaddr_in6); - } + + sockaddr_in6 sock6 = {}; + sock6.sin6_family = AF_INET6; + sock6.sin6_port = port; + sock6.sin6_flowinfo = 0; + sock6.sin6_addr = addr->addr.addr6; + memcpy(sockaddr, &sock6, sizeof(sock6)); + return sizeof(struct sockaddr_in6); } struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const* addr, tr_port port, bool clientIsSeed) @@ -606,17 +596,16 @@ static int global_unicast_address(struct sockaddr_storage* ss) return 1; } - else if (ss->ss_family == AF_INET6) + + if (ss->ss_family == AF_INET6) { unsigned char const* a = (unsigned char*)&((struct sockaddr_in6*)ss)->sin6_addr; /* 2000::/3 */ return (a[0] & 0xE0) == 0x20 ? 1 : 0; } - else - { - errno = EAFNOSUPPORT; - return -1; - } + + errno = EAFNOSUPPORT; + return -1; } static int tr_globalAddress(int af, void* addr, int* addr_len) diff --git a/libtransmission/peer-io.cc b/libtransmission/peer-io.cc index 241713712..a529a116b 100644 --- a/libtransmission/peer-io.cc +++ b/libtransmission/peer-io.cc @@ -1443,10 +1443,8 @@ int tr_peerIoFlushOutgoingProtocolMsgs(tr_peerIo* io) { break; } - else - { - byteCount += it->length; - } + + byteCount += it->length; } return tr_peerIoFlush(io, TR_UP, byteCount); diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index cc3db34fb..69e258e18 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -2643,16 +2643,13 @@ uint64_t tr_peerMgrGetDesiredAvailable(tr_torrent const* tor) { return 0; } - else - { - tr_peer const** peers = (tr_peer const**)tr_ptrArrayBase(&s->peers); - for (size_t i = 0; i < peer_count; ++i) + tr_peer const** const peers = (tr_peer const**)tr_ptrArrayBase(&s->peers); + for (size_t i = 0; i < peer_count; ++i) + { + if (peers[i]->atom != nullptr && atomIsSeed(peers[i]->atom)) { - if (peers[i]->atom != nullptr && atomIsSeed(peers[i]->atom)) - { - return tr_torrentGetLeftUntilDone(tor); - } + return tr_torrentGetLeftUntilDone(tor); } } @@ -3147,12 +3144,10 @@ static inline bool isBandwidthMaxedOut(Bandwidth const* b, uint64_t const now_ms { return false; } - else - { - unsigned int const got = b->getPieceSpeedBytesPerSecond(now_msec, dir); - unsigned int const want = b->getDesiredSpeedBytesPerSecond(dir); - return got >= want; - } + + unsigned int const got = b->getPieceSpeedBytesPerSecond(now_msec, dir); + unsigned int const want = b->getDesiredSpeedBytesPerSecond(dir); + return got >= want; } static void rechokeUploads(tr_swarm* s, uint64_t const now) @@ -3662,7 +3657,7 @@ static void enforceSessionPeerLimit(tr_session* session, uint64_t now) } } -static void makeNewPeerConnections(tr_peerMgr* mgr, int const max); +static void makeNewPeerConnections(tr_peerMgr* mgr, int max); static void reconnectPulse([[maybe_unused]] evutil_socket_t fd, [[maybe_unused]] short what, void* vmgr) { @@ -4250,7 +4245,7 @@ static void initiateCandidateConnection(tr_peerMgr* mgr, struct peer_candidate* initiateConnection(mgr, c->tor->swarm, c->atom); } -static void makeNewPeerConnections(struct tr_peerMgr* mgr, int const max) +static void makeNewPeerConnections(struct tr_peerMgr* mgr, int max) { auto n = int{}; struct peer_candidate* candidates = getPeerCandidates(mgr->session, &n, max); diff --git a/libtransmission/peer-msgs.cc b/libtransmission/peer-msgs.cc index 0dc4ced2c..3e61b27f4 100644 --- a/libtransmission/peer-msgs.cc +++ b/libtransmission/peer-msgs.cc @@ -1403,15 +1403,14 @@ static ReadState readBtId(tr_peerMsgsImpl* msgs, struct evbuffer* inbuf, size_t msgs->state = AWAITING_BT_PIECE; return READ_NOW; } - else if (msgs->incoming.length != 1) + + if (msgs->incoming.length != 1) { msgs->state = AWAITING_BT_MESSAGE; return READ_NOW; } - else - { - return readBtMessage(msgs, inbuf, inlen - 1); - } + + return readBtMessage(msgs, inbuf, inlen - 1); } static void updatePeerProgress(tr_peerMsgsImpl* msgs) @@ -1555,51 +1554,44 @@ static ReadState readBtPiece(tr_peerMsgsImpl* msgs, struct evbuffer* inbuf, size dbgmsg(msgs, "got incoming block header %u:%u->%u", req->index, req->offset, req->length); return READ_NOW; } - else + + if (msgs->incoming.block == nullptr) { - int err; - size_t n; - size_t nLeft; - struct evbuffer* block_buffer; - - if (msgs->incoming.block == nullptr) - { - msgs->incoming.block = evbuffer_new(); - } - - block_buffer = msgs->incoming.block; - - /* read in another chunk of data */ - nLeft = req->length - evbuffer_get_length(block_buffer); - n = std::min(nLeft, inlen); - - tr_peerIoReadBytesToBuf(msgs->io, inbuf, block_buffer, n); - - msgs->publishClientGotPieceData(n); - *setme_piece_bytes_read += n; - dbgmsg( - msgs, - "got %zu bytes for block %u:%u->%u ... %d remain", - n, - req->index, - req->offset, - req->length, - (int)(req->length - evbuffer_get_length(block_buffer))); - - if (evbuffer_get_length(block_buffer) < req->length) - { - return READ_LATER; - } - - /* pass the block along... */ - err = clientGotBlock(msgs, block_buffer, req); - evbuffer_drain(block_buffer, evbuffer_get_length(block_buffer)); - - /* cleanup */ - req->length = 0; - msgs->state = AWAITING_BT_LENGTH; - return err != 0 ? READ_ERR : READ_NOW; + msgs->incoming.block = evbuffer_new(); } + + struct evbuffer* const block_buffer = msgs->incoming.block; + + /* read in another chunk of data */ + size_t const nLeft = req->length - evbuffer_get_length(block_buffer); + size_t const n = std::min(nLeft, inlen); + + tr_peerIoReadBytesToBuf(msgs->io, inbuf, block_buffer, n); + + msgs->publishClientGotPieceData(n); + *setme_piece_bytes_read += n; + dbgmsg( + msgs, + "got %zu bytes for block %u:%u->%u ... %d remain", + n, + req->index, + req->offset, + req->length, + (int)(req->length - evbuffer_get_length(block_buffer))); + + if (evbuffer_get_length(block_buffer) < req->length) + { + return READ_LATER; + } + + /* pass the block along... */ + int const err = clientGotBlock(msgs, block_buffer, req); + evbuffer_drain(block_buffer, evbuffer_get_length(block_buffer)); + + /* cleanup */ + req->length = 0; + msgs->state = AWAITING_BT_LENGTH; + return err != 0 ? READ_ERR : READ_NOW; } static ReadState readBtMessage(tr_peerMsgsImpl* msgs, struct evbuffer* inbuf, size_t inlen) diff --git a/libtransmission/tr-dht.cc b/libtransmission/tr-dht.cc index 49c4e081c..c731c1f7d 100644 --- a/libtransmission/tr-dht.cc +++ b/libtransmission/tr-dht.cc @@ -103,14 +103,13 @@ static int bootstrap_af(tr_session* session) { return AF_INET; } - else if (bootstrap_done(session, AF_INET)) + + if (bootstrap_done(session, AF_INET)) { return AF_INET6; } - else - { - return 0; - } + + return 0; } static void bootstrap_from_name(char const* name, tr_port port, int af) @@ -608,7 +607,8 @@ bool tr_dhtAddNode(tr_session* ss, tr_address const* address, tr_port port, bool dht_ping_node((struct sockaddr*)&sin, sizeof(sin)); return true; } - else if (address->type == TR_AF_INET6) + + if (address->type == TR_AF_INET6) { struct sockaddr_in6 sin6; memset(&sin6, 0, sizeof(sin6)); diff --git a/libtransmission/tr-lpd.cc b/libtransmission/tr-lpd.cc index 37df3b6c0..b5bae623b 100644 --- a/libtransmission/tr-lpd.cc +++ b/libtransmission/tr-lpd.cc @@ -592,10 +592,8 @@ static int tr_lpdConsiderAnnounce(tr_pex* peer, char const* const msg) return 1; } - else - { - tr_logAddNamedDbg("LPD", "Cannot serve torrent #%s", hashString); - } + + tr_logAddNamedDbg("LPD", "Cannot serve torrent #%s", hashString); } return res; diff --git a/libtransmission/variant-benc.cc b/libtransmission/variant-benc.cc index 2911f3fbc..788c2ed59 100644 --- a/libtransmission/variant-benc.cc +++ b/libtransmission/variant-benc.cc @@ -240,14 +240,11 @@ int tr_variantParseBenc(void const* buf_in, void const* bufend_in, tr_variant* t err = EILSEQ; break; } - else - { - stack.pop_back(); - if (std::empty(stack)) - { - break; - } + stack.pop_back(); + if (std::empty(stack)) + { + break; } } else if (isdigit(*buf)) /* string? */