Commit bf2ab1e0 authored by Rosen Penev's avatar Rosen Penev Committed by Facebook Github Bot

Use empty instead of size (#1293)

Summary:
Found with readability-container-size-empty
Signed-off-by: default avatarRosen Penev <rosenp@gmail.com>
Pull Request resolved: https://github.com/facebook/folly/pull/1293

Reviewed By: markisaa

Differential Revision: D19241094

Pulled By: yfeldblum

fbshipit-source-id: 9362e8dc5dc1458fcd2b27c11ce81eafe674bcd2
parent 96652b6f
...@@ -778,7 +778,7 @@ ConversionError makeConversionError(ConversionCode code, StringPiece input) { ...@@ -778,7 +778,7 @@ ConversionError makeConversionError(ConversionCode code, StringPiece input) {
if (err.quote) { if (err.quote) {
tmp.append(1, '"'); tmp.append(1, '"');
} }
if (input.size() > 0) { if (!input.empty()) {
tmp.append(input.data(), input.size()); tmp.append(input.data(), input.size());
} }
if (err.quote) { if (err.quote) {
......
...@@ -98,7 +98,7 @@ std::string TypeDescriptor::name() const { ...@@ -98,7 +98,7 @@ std::string TypeDescriptor::name() const {
auto trace = detail::getSingletonStackTrace(); auto trace = detail::getSingletonStackTrace();
LOG(FATAL) << "Creating instance for unregistered singleton: " << type.name() LOG(FATAL) << "Creating instance for unregistered singleton: " << type.name()
<< "\n" << "\n"
<< "Stacktrace:\n" << (trace != "" ? trace : "(not available)"); << "Stacktrace:\n" << (!trace.empty() ? trace : "(not available)");
} }
[[noreturn]] void singletonWarnRegisterMockEarlyAndAbort( [[noreturn]] void singletonWarnRegisterMockEarlyAndAbort(
...@@ -130,7 +130,7 @@ void singletonWarnDestroyInstanceLeak( ...@@ -130,7 +130,7 @@ void singletonWarnDestroyInstanceLeak(
auto trace = detail::getSingletonStackTrace(); auto trace = detail::getSingletonStackTrace();
LOG(FATAL) << "Creating instance for unregistered singleton: " << type.name() LOG(FATAL) << "Creating instance for unregistered singleton: " << type.name()
<< "\n" << "\n"
<< "Stacktrace:\n" << (trace != "" ? trace : "(not available)"); << "Stacktrace:\n" << (!trace.empty() ? trace : "(not available)");
} }
[[noreturn]] void singletonWarnCreateBeforeRegistrationCompleteAndAbort( [[noreturn]] void singletonWarnCreateBeforeRegistrationCompleteAndAbort(
...@@ -141,13 +141,13 @@ void singletonWarnDestroyInstanceLeak( ...@@ -141,13 +141,13 @@ void singletonWarnDestroyInstanceLeak(
<< "This usually means that either main() never called " << "This usually means that either main() never called "
<< "folly::init, or singleton was requested before main() " << "folly::init, or singleton was requested before main() "
<< "(which is not allowed).\n" << "(which is not allowed).\n"
<< "Stacktrace:\n" << (trace != "" ? trace : "(not available)"); << "Stacktrace:\n" << (!trace.empty() ? trace : "(not available)");
} }
void singletonPrintDestructionStackTrace(const TypeDescriptor& type) { void singletonPrintDestructionStackTrace(const TypeDescriptor& type) {
auto trace = detail::getSingletonStackTrace(); auto trace = detail::getSingletonStackTrace();
LOG(ERROR) << "Singleton " << type.name() << " was released.\n" LOG(ERROR) << "Singleton " << type.name() << " was released.\n"
<< "Stacktrace:\n" << (trace != "" ? trace : "(not available)"); << "Stacktrace:\n" << (!trace.empty() ? trace : "(not available)");
} }
[[noreturn]] void singletonThrowNullCreator(const std::type_info& type) { [[noreturn]] void singletonThrowNullCreator(const std::type_info& type) {
......
...@@ -417,7 +417,7 @@ double prettyToDouble( ...@@ -417,7 +417,7 @@ double prettyToDouble(
folly::StringPiece* const prettyString, folly::StringPiece* const prettyString,
const PrettyType type) { const PrettyType type) {
auto value = folly::to<double>(prettyString); auto value = folly::to<double>(prettyString);
while (prettyString->size() > 0 && std::isspace(prettyString->front())) { while (!prettyString->empty() && std::isspace(prettyString->front())) {
prettyString->advance(1); // Skipping spaces between number and suffix prettyString->advance(1); // Skipping spaces between number and suffix
} }
const PrettySuffix* suffixes = kPrettySuffixes[type]; const PrettySuffix* suffixes = kPrettySuffixes[type];
......
...@@ -126,7 +126,7 @@ std::string Uri::authority() const { ...@@ -126,7 +126,7 @@ std::string Uri::authority() const {
} }
std::string Uri::hostname() const { std::string Uri::hostname() const {
if (host_.size() > 0 && host_[0] == '[') { if (!host_.empty() && host_[0] == '[') {
// If it starts with '[', then it should end with ']', this is ensured by // If it starts with '[', then it should end with ']', this is ensured by
// regex // regex
return host_.substr(1, host_.size() - 2); return host_.substr(1, host_.size() - 2);
......
...@@ -1754,7 +1754,7 @@ bool Bzip2StreamCodec::doCompressStream( ...@@ -1754,7 +1754,7 @@ bool Bzip2StreamCodec::doCompressStream(
case StreamCodec::FlushOp::FLUSH: case StreamCodec::FlushOp::FLUSH:
if (rc == BZ_RUN_OK) { if (rc == BZ_RUN_OK) {
DCHECK_EQ(cstream_->avail_in, 0); DCHECK_EQ(cstream_->avail_in, 0);
DCHECK(input.size() == 0 || cstream_->avail_out != output.size()); DCHECK(input.empty() || cstream_->avail_out != output.size());
return true; return true;
} }
return false; return false;
......
...@@ -112,7 +112,7 @@ CacheLocality CacheLocality::readFromSysfsTree( ...@@ -112,7 +112,7 @@ CacheLocality CacheLocality::readFromSysfsTree(
sformat("/sys/devices/system/cpu/cpu{}/cache/index{}/", cpu, index); sformat("/sys/devices/system/cpu/cpu{}/cache/index{}/", cpu, index);
auto cacheType = mapping(dir + "type"); auto cacheType = mapping(dir + "type");
auto equivStr = mapping(dir + "shared_cpu_list"); auto equivStr = mapping(dir + "shared_cpu_list");
if (cacheType.size() == 0 || equivStr.size() == 0) { if (cacheType.empty() || equivStr.empty()) {
// no more caches // no more caches
break; break;
} }
...@@ -134,7 +134,7 @@ CacheLocality CacheLocality::readFromSysfsTree( ...@@ -134,7 +134,7 @@ CacheLocality CacheLocality::readFromSysfsTree(
} }
} }
if (levels.size() == 0) { if (levels.empty()) {
// no levels at all for this cpu, we must be done // no levels at all for this cpu, we must be done
break; break;
} }
...@@ -142,7 +142,7 @@ CacheLocality CacheLocality::readFromSysfsTree( ...@@ -142,7 +142,7 @@ CacheLocality CacheLocality::readFromSysfsTree(
cpus.push_back(cpu); cpus.push_back(cpu);
} }
if (cpus.size() == 0) { if (cpus.empty()) {
throw std::runtime_error("unable to load cache sharing info"); throw std::runtime_error("unable to load cache sharing info");
} }
......
...@@ -169,9 +169,9 @@ void ThreadPoolExecutor::setNumThreads(size_t numThreads) { ...@@ -169,9 +169,9 @@ void ThreadPoolExecutor::setNumThreads(size_t numThreads) {
ThreadPoolExecutor::removeThreads(numThreadsToJoin, false); ThreadPoolExecutor::removeThreads(numThreadsToJoin, false);
activeThreads_.store( activeThreads_.store(
active - numThreadsToJoin, std::memory_order_relaxed); active - numThreadsToJoin, std::memory_order_relaxed);
} else if (pending > 0 || observers_.size() > 0 || active < minthreads) { } else if (pending > 0 || !observers_.empty() || active < minthreads) {
size_t numToAdd = std::min(pending, numThreads - active); size_t numToAdd = std::min(pending, numThreads - active);
if (observers_.size() > 0) { if (!observers_.empty()) {
numToAdd = numThreads - active; numToAdd = numThreads - active;
} }
if (active + numToAdd < minthreads) { if (active + numToAdd < minthreads) {
...@@ -323,7 +323,7 @@ ThreadPoolExecutor::ThreadPtr ThreadPoolExecutor::StoppedThreadQueue::take() { ...@@ -323,7 +323,7 @@ ThreadPoolExecutor::ThreadPtr ThreadPoolExecutor::StoppedThreadQueue::take() {
while (true) { while (true) {
{ {
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if (queue_.size() > 0) { if (!queue_.empty()) {
auto item = std::move(queue_.front()); auto item = std::move(queue_.front());
queue_.pop(); queue_.pop();
return item; return item;
...@@ -339,7 +339,7 @@ ThreadPoolExecutor::StoppedThreadQueue::try_take_for( ...@@ -339,7 +339,7 @@ ThreadPoolExecutor::StoppedThreadQueue::try_take_for(
while (true) { while (true) {
{ {
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if (queue_.size() > 0) { if (!queue_.empty()) {
auto item = std::move(queue_.front()); auto item = std::move(queue_.front());
queue_.pop(); queue_.pop();
return item; return item;
......
...@@ -157,8 +157,7 @@ folly::dynamic& DynamicParser::ParserStack::errors( ...@@ -157,8 +157,7 @@ folly::dynamic& DynamicParser::ParserStack::errors(
} }
folly::dynamic DynamicParser::ParserStack::releaseErrors() { folly::dynamic DynamicParser::ParserStack::releaseErrors() {
if (key_ || unmaterializedSubErrorKeys_.size() != 0 || if (key_ || !unmaterializedSubErrorKeys_.empty() || subErrors_.size() != 1) {
subErrors_.size() != 1) {
throw DynamicParserLogicError( throw DynamicParserLogicError(
"Do not releaseErrors() while parsing: ", "Do not releaseErrors() while parsing: ",
key_ != nullptr, key_ != nullptr,
......
...@@ -75,7 +75,7 @@ void initStateFromParams( ...@@ -75,7 +75,7 @@ void initStateFromParams(
reinterpret_cast<uint8_t*>(state) + sizeof(state->h), reinterpret_cast<uint8_t*>(state) + sizeof(state->h),
0, 0,
sizeof(*state) - sizeof(state->h)); sizeof(*state) - sizeof(state->h));
if (key.size() > 0) { if (!key.empty()) {
if (key.size() < crypto_generichash_blake2b_KEYBYTES_MIN || if (key.size() < crypto_generichash_blake2b_KEYBYTES_MIN ||
key.size() > crypto_generichash_blake2b_KEYBYTES_MAX) { key.size() > crypto_generichash_blake2b_KEYBYTES_MAX) {
throw std::runtime_error("invalid key size"); throw std::runtime_error("invalid key size");
...@@ -129,13 +129,13 @@ void Blake2xb::init( ...@@ -129,13 +129,13 @@ void Blake2xb::init(
param_.fanout = 1; param_.fanout = 1;
param_.depth = 1; param_.depth = 1;
param_.xofLength = Endian::little(static_cast<uint32_t>(outputLength)); param_.xofLength = Endian::little(static_cast<uint32_t>(outputLength));
if (salt.size() > 0) { if (!salt.empty()) {
if (salt.size() != crypto_generichash_blake2b_SALTBYTES) { if (salt.size() != crypto_generichash_blake2b_SALTBYTES) {
throw std::runtime_error("Invalid salt length, must be 16 bytes"); throw std::runtime_error("Invalid salt length, must be 16 bytes");
} }
std::memcpy(param_.salt, salt.data(), sizeof(param_.salt)); std::memcpy(param_.salt, salt.data(), sizeof(param_.salt));
} }
if (personalization.size() > 0) { if (!personalization.empty()) {
if (personalization.size() != crypto_generichash_blake2b_PERSONALBYTES) { if (personalization.size() != crypto_generichash_blake2b_PERSONALBYTES) {
throw std::runtime_error( throw std::runtime_error(
"Invalid personalization length, must be 16 bytes"); "Invalid personalization length, must be 16 bytes");
......
...@@ -824,7 +824,7 @@ void AsyncSSLSocket::sslConn( ...@@ -824,7 +824,7 @@ void AsyncSSLSocket::sslConn(
sslSession_ = nullptr; sslSession_ = nullptr;
} }
#if FOLLY_OPENSSL_HAS_SNI #if FOLLY_OPENSSL_HAS_SNI
if (tlsextHostname_.size()) { if (!tlsextHostname_.empty()) {
SSL_set_tlsext_host_name(ssl_.get(), tlsextHostname_.c_str()); SSL_set_tlsext_host_name(ssl_.get(), tlsextHostname_.c_str());
} }
#endif #endif
......
...@@ -263,7 +263,7 @@ void AsyncServerSocket::useExistingSockets( ...@@ -263,7 +263,7 @@ void AsyncServerSocket::useExistingSockets(
eventBase_->dcheckIsInEventBaseThread(); eventBase_->dcheckIsInEventBaseThread();
} }
if (sockets_.size() > 0) { if (!sockets_.empty()) {
throw std::invalid_argument( throw std::invalid_argument(
"cannot call useExistingSocket() on a " "cannot call useExistingSocket() on a "
"AsyncServerSocket that already has a socket"); "AsyncServerSocket that already has a socket");
...@@ -357,7 +357,7 @@ void AsyncServerSocket::bind(const SocketAddress& address) { ...@@ -357,7 +357,7 @@ void AsyncServerSocket::bind(const SocketAddress& address) {
// Don't set socket_ yet, so that socket_ will remain uninitialized if an // Don't set socket_ yet, so that socket_ will remain uninitialized if an
// error occurs. // error occurs.
NetworkSocket fd; NetworkSocket fd;
if (sockets_.size() == 0) { if (sockets_.empty()) {
fd = createSocket(address.getFamily()); fd = createSocket(address.getFamily());
} else if (sockets_.size() == 1) { } else if (sockets_.size() == 1) {
if (address.getFamily() != sockets_[0].addressFamily_) { if (address.getFamily() != sockets_[0].addressFamily_) {
...@@ -389,7 +389,7 @@ void AsyncServerSocket::bind( ...@@ -389,7 +389,7 @@ void AsyncServerSocket::bind(
bindSocket(fd, address, false); bindSocket(fd, address, false);
} }
if (sockets_.size() == 0) { if (sockets_.empty()) {
throw std::runtime_error( throw std::runtime_error(
"did not bind any async server socket for port and addresses"); "did not bind any async server socket for port and addresses");
} }
...@@ -523,7 +523,7 @@ void AsyncServerSocket::bind(uint16_t port) { ...@@ -523,7 +523,7 @@ void AsyncServerSocket::bind(uint16_t port) {
break; break;
} }
if (sockets_.size() == 0) { if (sockets_.empty()) {
throw std::runtime_error("did not bind any async server socket for port"); throw std::runtime_error("did not bind any async server socket for port");
} }
} }
...@@ -542,7 +542,7 @@ void AsyncServerSocket::listen(int backlog) { ...@@ -542,7 +542,7 @@ void AsyncServerSocket::listen(int backlog) {
} }
void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const { void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const {
CHECK(sockets_.size() >= 1); CHECK(!sockets_.empty());
VLOG_IF(2, sockets_.size() > 1) VLOG_IF(2, sockets_.size() > 1)
<< "Warning: getAddress() called and multiple addresses available (" << "Warning: getAddress() called and multiple addresses available ("
<< sockets_.size() << "). Returning only the first one."; << sockets_.size() << "). Returning only the first one.";
...@@ -551,7 +551,7 @@ void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const { ...@@ -551,7 +551,7 @@ void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const {
} }
std::vector<SocketAddress> AsyncServerSocket::getAddresses() const { std::vector<SocketAddress> AsyncServerSocket::getAddresses() const {
CHECK(sockets_.size() >= 1); CHECK(!sockets_.empty());
auto tsaVec = std::vector<SocketAddress>(sockets_.size()); auto tsaVec = std::vector<SocketAddress>(sockets_.size());
auto tsaIter = tsaVec.begin(); auto tsaIter = tsaVec.begin();
for (const auto& socket : sockets_) { for (const auto& socket : sockets_) {
......
...@@ -102,7 +102,7 @@ void SSLContext::ciphers(const std::string& ciphers) { ...@@ -102,7 +102,7 @@ void SSLContext::ciphers(const std::string& ciphers) {
void SSLContext::setClientECCurvesList( void SSLContext::setClientECCurvesList(
const std::vector<std::string>& ecCurves) { const std::vector<std::string>& ecCurves) {
if (ecCurves.size() == 0) { if (ecCurves.empty()) {
return; return;
} }
#if OPENSSL_VERSION_NUMBER >= 0x1000200fL #if OPENSSL_VERSION_NUMBER >= 0x1000200fL
...@@ -472,12 +472,12 @@ bool SSLContext::setAdvertisedNextProtocols( ...@@ -472,12 +472,12 @@ bool SSLContext::setAdvertisedNextProtocols(
bool SSLContext::setRandomizedAdvertisedNextProtocols( bool SSLContext::setRandomizedAdvertisedNextProtocols(
const std::list<NextProtocolsItem>& items) { const std::list<NextProtocolsItem>& items) {
unsetNextProtocols(); unsetNextProtocols();
if (items.size() == 0) { if (items.empty()) {
return false; return false;
} }
int total_weight = 0; int total_weight = 0;
for (const auto& item : items) { for (const auto& item : items) {
if (item.protocols.size() == 0) { if (item.protocols.empty()) {
continue; continue;
} }
AdvertisedNextProtocolsItem advertised_item; AdvertisedNextProtocolsItem advertised_item;
......
...@@ -32,7 +32,7 @@ static void run( ...@@ -32,7 +32,7 @@ static void run(
EventBase* eb, EventBase* eb,
folly::Baton<>* stop, folly::Baton<>* stop,
const StringPiece& name) { const StringPiece& name) {
if (name.size()) { if (!name.empty()) {
folly::setThreadName(name); folly::setThreadName(name);
} }
......
...@@ -291,7 +291,7 @@ LogConfig::CategoryConfigMap parseCategoryConfigs(StringPiece value) { ...@@ -291,7 +291,7 @@ LogConfig::CategoryConfigMap parseCategoryConfigs(StringPiece value) {
std::vector<StringPiece> handlerPieces; std::vector<StringPiece> handlerPieces;
folly::split(":", configString, handlerPieces); folly::split(":", configString, handlerPieces);
FOLLY_SAFE_DCHECK( FOLLY_SAFE_DCHECK(
handlerPieces.size() >= 1, !handlerPieces.empty(),
"folly::split() always returns a list of length 1"); "folly::split() always returns a list of length 1");
auto levelString = trimWhitespace(handlerPieces[0]); auto levelString = trimWhitespace(handlerPieces[0]);
...@@ -428,7 +428,7 @@ std::pair<std::string, LogHandlerConfig> parseHandlerConfig(StringPiece value) { ...@@ -428,7 +428,7 @@ std::pair<std::string, LogHandlerConfig> parseHandlerConfig(StringPiece value) {
std::vector<StringPiece> pieces; std::vector<StringPiece> pieces;
folly::split(",", optionsStr, pieces); folly::split(",", optionsStr, pieces);
FOLLY_SAFE_DCHECK( FOLLY_SAFE_DCHECK(
pieces.size() >= 1, "folly::split() always returns a list of length 1"); !pieces.empty(), "folly::split() always returns a list of length 1");
for (const auto& piece : pieces) { for (const auto& piece : pieces) {
StringPiece optionName; StringPiece optionName;
...@@ -469,7 +469,7 @@ LogConfig parseLogConfig(StringPiece value) { ...@@ -469,7 +469,7 @@ LogConfig parseLogConfig(StringPiece value) {
std::vector<StringPiece> pieces; std::vector<StringPiece> pieces;
folly::split(";", value, pieces); folly::split(";", value, pieces);
FOLLY_SAFE_DCHECK( FOLLY_SAFE_DCHECK(
pieces.size() >= 1, "folly::split() always returns a list of length 1"); !pieces.empty(), "folly::split() always returns a list of length 1");
auto categoryConfigs = parseCategoryConfigs(pieces[0]); auto categoryConfigs = parseCategoryConfigs(pieces[0]);
LogConfig::HandlerConfigMap handlerConfigs; LogConfig::HandlerConfigMap handlerConfigs;
......
...@@ -326,7 +326,7 @@ void DeterministicSchedule::beforeThreadExit() { ...@@ -326,7 +326,7 @@ void DeterministicSchedule::beforeThreadExit() {
} }
sems_.erase(std::find(sems_.begin(), sems_.end(), tls.sem)); sems_.erase(std::find(sems_.begin(), sems_.end(), tls.sem));
active_.erase(std::this_thread::get_id()); active_.erase(std::this_thread::get_id());
if (sems_.size() > 0) { if (!sems_.empty()) {
FOLLY_TEST_DSCHED_VLOG("exiting"); FOLLY_TEST_DSCHED_VLOG("exiting");
/* Wait here so that parent thread can control when the thread /* Wait here so that parent thread can control when the thread
* enters the thread local destructors. */ * enters the thread local destructors. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment