From d0ce8c8ad35f54d47c7343e80f494ab51cc805e1 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 8 Dec 2024 16:46:50 -0800 Subject: [PATCH] clang-tidy: simplify some algorithms Signed-off-by: Rosen Penev --- pdns/arguments.cc | 2 +- pdns/auth-secondarycommunicator.cc | 10 ++++------ pdns/dnsbulktest.cc | 3 +-- pdns/dnsdistdist/dnsdist-lua.cc | 4 +++- pdns/dnsdistdist/dnsdist-protocols.cc | 2 +- pdns/recursordist/syncres.cc | 14 +------------- pdns/rfc2136handler.cc | 6 ++++-- 7 files changed, 15 insertions(+), 26 deletions(-) diff --git a/pdns/arguments.cc b/pdns/arguments.cc index f87fa39b1d234..7eef549ded369 100644 --- a/pdns/arguments.cc +++ b/pdns/arguments.cc @@ -111,7 +111,7 @@ bool ArgvMap::contains(const string& var, const string& val) vector parts; stringtok(parts, param->second, ", \t"); - return std::any_of(parts.begin(), parts.end(), [&](const std::string& str) { return str == val; }); + return std::find(parts.begin(), parts.end(), val) != parts.end(); } string ArgvMap::helpstring(string prefix) diff --git a/pdns/auth-secondarycommunicator.cc b/pdns/auth-secondarycommunicator.cc index 739838afe9762..b62537dce9bdd 100644 --- a/pdns/auth-secondarycommunicator.cc +++ b/pdns/auth-secondarycommunicator.cc @@ -27,6 +27,7 @@ #include "dnssecinfra.hh" #include "dnsseckeeper.hh" #include "base32.hh" +#include #include #include "communicator.hh" #include @@ -490,12 +491,9 @@ void CommunicatorClass::ixfrSuck(const DNSName& domain, const TSIGTriplet& tt, c } } // O(N^2)! - rrset.erase(remove_if(rrset.begin(), rrset.end(), - [&g](const DNSRecord& dr) { - return count(g.second.first.cbegin(), - g.second.first.cend(), dr); - }), - rrset.end()); + boost::range::remove_erase_if(rrset, [&g](const auto& dnsr) { + return boost::algorithm::any_of_equal(g.second.first, dnsr); + }); // the DNSRecord== operator compares on name, type, class and lowercase content representation for (const auto& x : g.second.second) { diff --git a/pdns/dnsbulktest.cc b/pdns/dnsbulktest.cc index 49ad38ab49e46..9c9abd063d3a9 100644 --- a/pdns/dnsbulktest.cc +++ b/pdns/dnsbulktest.cc @@ -317,8 +317,7 @@ try pos=split.second.find('/'); if(pos != string::npos) // alexa has whole urls in the list now. split.second.resize(pos); - if(find_if(split.second.begin(), split.second.end(), isalpha) == split.second.end()) - { + if (std::none_of(split.second.begin(), split.second.end(), isalpha)) { continue; // this was an IP address } domains.push_back(TypedQuery(split.second, qtype)); diff --git a/pdns/dnsdistdist/dnsdist-lua.cc b/pdns/dnsdistdist/dnsdist-lua.cc index 09818d982107a..3805bcc2c6749 100644 --- a/pdns/dnsdistdist/dnsdist-lua.cc +++ b/pdns/dnsdistdist/dnsdist-lua.cc @@ -36,6 +36,8 @@ #include #include +#include + #include "dnsdist.hh" #include "dnsdist-backend.hh" #include "dnsdist-cache.hh" @@ -760,7 +762,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } dnsdist::configuration::updateRuntimeConfiguration([&server](dnsdist::configuration::RuntimeConfiguration& config) { - config.d_backends.erase(std::remove(config.d_backends.begin(), config.d_backends.end(), server), config.d_backends.end()); + boost::range::remove_erase(config.d_backends, server); }); server->stop(); diff --git a/pdns/dnsdistdist/dnsdist-protocols.cc b/pdns/dnsdistdist/dnsdist-protocols.cc index 35da4711f4152..ee362e9cc8f7a 100644 --- a/pdns/dnsdistdist/dnsdist-protocols.cc +++ b/pdns/dnsdistdist/dnsdist-protocols.cc @@ -49,7 +49,7 @@ const std::array Protocol::s_prettyN Protocol::Protocol(const std::string& protocol) { - const auto& namesIt = std::find(s_names.begin(), s_names.end(), protocol); + auto namesIt = std::find(s_names.begin(), s_name.end(), protocol); if (namesIt == s_names.end()) { throw std::runtime_error("Unknown protocol name: '" + protocol + "'"); } diff --git a/pdns/recursordist/syncres.cc b/pdns/recursordist/syncres.cc index 9a318dfc46ba8..18e7f238a9832 100644 --- a/pdns/recursordist/syncres.cc +++ b/pdns/recursordist/syncres.cc @@ -6266,19 +6266,7 @@ bool SyncRes::answerIsNOData(uint16_t requestedType, int rcode, const std::vecto return false; } - // NOLINTNEXTLINE(readability-use-anyofallof) - for (const auto& rec : records) { - if (rec.d_place == DNSResourceRecord::ANSWER && rec.d_type == requestedType) { - /* we have a record, of the right type, in the right section */ - return false; - } - } - return true; -#if 0 - // This code should be equivalent to the code above, clang-tidy prefers any_of() - // I have doubts if that is easier to read - return !std::any_of(records.begin(), records.end(), [=](const DNSRecord& rec) { + return std::none_of(records.begin(), records.end(), [=](const auto& rec) { return rec.d_place == DNSResourceRecord::ANSWER && rec.d_type == requestedType; }); -#endif } diff --git a/pdns/rfc2136handler.cc b/pdns/rfc2136handler.cc index be4d78f5c1273..d6ce409fa5e44 100644 --- a/pdns/rfc2136handler.cc +++ b/pdns/rfc2136handler.cc @@ -476,10 +476,12 @@ uint PacketHandler::performUpdate(const string &msgPrefix, const DNSRecord *rr, bool foundDeeper = false, foundOtherWithSameName = false; di->backend->listSubZone(rr->d_name, di->id); while (di->backend->get(rec)) { - if (rec.qname == rr->d_name && !count(recordsToDelete.begin(), recordsToDelete.end(), rec)) + if (rec.qname == rr->d_name && count(recordsToDelete.begin(), recordsToDelete.end(), rec) == 0) { foundOtherWithSameName = true; - if (rec.qname != rr->d_name && rec.qtype.getCode() != QType::NS) //Skip NS records, as this would be a delegate that we can ignore as this does not require us to create a ENT + } + if (rec.qname != rr->d_name && rec.qtype.getCode() != QType::NS) { // Skip NS records, as this would be a delegate that we can ignore as this does not require us to create a ENT foundDeeper = true; + } } if (foundDeeper && !foundOtherWithSameName) {