From 224085cccd7d7f52ceac3bd1f0ec8e4cd262948d Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 5 Dec 2023 12:23:56 -0800 Subject: [PATCH] clang-tidy: pass function by ref Found with performance-unnecessary-value-param Found with performance-unnecessary-copy-initialization Signed-off-by: Rosen Penev --- modules/lmdbbackend/lmdbbackend.cc | 6 ++--- modules/lmdbbackend/lmdbbackend.hh | 6 ++--- pdns/dnswasher.cc | 2 +- pdns/iputils.hh | 3 ++- pdns/json.cc | 39 ++++++++++++++++-------------- pdns/lua-record.cc | 5 ++-- pdns/statnode.cc | 8 +++--- pdns/statnode.hh | 6 ++--- pdns/tcpiohandler.hh | 7 +++--- pdns/webserver.cc | 3 ++- 10 files changed, 45 insertions(+), 40 deletions(-) diff --git a/modules/lmdbbackend/lmdbbackend.cc b/modules/lmdbbackend/lmdbbackend.cc index 6b795654f7f9..e528d63295c2 100644 --- a/modules/lmdbbackend/lmdbbackend.cc +++ b/modules/lmdbbackend/lmdbbackend.cc @@ -1222,7 +1222,7 @@ std::shared_ptr LMDBBackend::getRecordsRWTran return ret; } -std::shared_ptr LMDBBackend::getRecordsROTransaction(uint32_t id, std::shared_ptr rwtxn) +std::shared_ptr LMDBBackend::getRecordsROTransaction(uint32_t id, const std::shared_ptr& rwtxn) { auto& shard = d_trecords[id % s_shards]; if (!shard.env) { @@ -1627,7 +1627,7 @@ bool LMDBBackend::getDomainInfo(const DNSName& domain, DomainInfo& di, bool gets return true; } -int LMDBBackend::genChangeDomain(const DNSName& domain, std::function func) +int LMDBBackend::genChangeDomain(const DNSName& domain, const std::function& func) { auto txn = d_tdomains->getRWTransaction(); @@ -1641,7 +1641,7 @@ int LMDBBackend::genChangeDomain(const DNSName& domain, std::function func) +int LMDBBackend::genChangeDomain(uint32_t id, const std::function& func) { DomainInfo di; diff --git a/modules/lmdbbackend/lmdbbackend.hh b/modules/lmdbbackend/lmdbbackend.hh index 6bef1078347b..40f18f8fb3bd 100644 --- a/modules/lmdbbackend/lmdbbackend.hh +++ b/modules/lmdbbackend/lmdbbackend.hh @@ -305,9 +305,9 @@ private: shared_ptr d_rotxn; // for lookup and list shared_ptr d_rwtxn; // for feedrecord within begin/aborttransaction std::shared_ptr getRecordsRWTransaction(uint32_t id); - std::shared_ptr getRecordsROTransaction(uint32_t id, std::shared_ptr rwtxn = nullptr); - int genChangeDomain(const DNSName& domain, std::function func); - int genChangeDomain(uint32_t id, std::function func); + std::shared_ptr getRecordsROTransaction(uint32_t id, const std::shared_ptr& rwtxn = nullptr); + int genChangeDomain(const DNSName& domain, const std::function& func); + int genChangeDomain(uint32_t id, const std::function& func); void deleteDomainRecords(RecordsRWTransaction& txn, uint32_t domain_id, uint16_t qtype = QType::ANY); void getAllDomainsFiltered(vector* domains, const std::function& allow); diff --git a/pdns/dnswasher.cc b/pdns/dnswasher.cc index a155305324e7..fcb844d1d582 100644 --- a/pdns/dnswasher.cc +++ b/pdns/dnswasher.cc @@ -127,7 +127,7 @@ class IPCipherObfuscator : public IPObfuscator } } - static std::unique_ptr make(std::string key, bool decrypt) + static std::unique_ptr make(const std::string& key, bool decrypt) { return std::make_unique(key, decrypt); } diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 7e6a392105fd..4ef0b8f764a3 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -1517,7 +1517,8 @@ public: d_addr.sin4.sin_port = 0; // this guarantees d_network compares identical } - AddressAndPortRange(ComboAddress ca, uint8_t addrMask, uint8_t portMask = 0): d_addr(std::move(ca)), d_addrMask(addrMask), d_portMask(portMask) + AddressAndPortRange(ComboAddress ca, uint8_t addrMask, uint8_t portMask = 0) : + d_addr(ca), d_addrMask(addrMask), d_portMask(portMask) { if (!d_addr.isIPv4()) { d_portMask = 0; diff --git a/pdns/json.cc b/pdns/json.cc index 285a953aefcb..12831a750bac 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -30,21 +30,23 @@ using json11::Json; static inline int intFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const int default_value) { - auto val = container[key]; + const auto& val = container[key]; if (val.is_number()) { return val.int_value(); - } else if (val.is_string()) { + } + + if (val.is_string()) { try { return std::stoi(val.string_value()); } catch (std::out_of_range&) { throw JsonException("Key '" + string(key) + "' is out of range"); } - } else { - if (have_default) { - return default_value; - } - throw JsonException("Key '" + string(key) + "' not an Integer or not present"); } + + if (have_default) { + return default_value; + } + throw JsonException("Key '" + string(key) + "' not an Integer or not present"); } int intFromJson(const Json& container, const std::string& key) @@ -78,21 +80,23 @@ unsigned int uintFromJson(const Json& container, const std::string& key, const u static inline double doubleFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const double default_value) { - auto val = container[key]; + const auto& val = container[key]; if (val.is_number()) { return val.number_value(); - } else if (val.is_string()) { + } + + if (val.is_string()) { try { return std::stod(val.string_value()); } catch (std::out_of_range&) { throw JsonException("Value for key '" + string(key) + "' is out of range"); } - } else { - if (have_default) { - return default_value; - } - throw JsonException("Key '" + string(key) + "' not an Integer or not present"); } + + if (have_default) { + return default_value; + } + throw JsonException("Key '" + string(key) + "' not an Integer or not present"); } double doubleFromJson(const Json& container, const std::string& key) @@ -107,17 +111,16 @@ double doubleFromJson(const Json& container, const std::string& key, const doubl string stringFromJson(const Json& container, const std::string &key) { - const Json val = container[key]; + const auto& val = container[key]; if (val.is_string()) { return val.string_value(); - } else { - throw JsonException("Key '" + string(key) + "' not present or not a String"); } + throw JsonException("Key '" + string(key) + "' not present or not a String"); } static inline bool boolFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const bool default_value) { - auto val = container[key]; + const auto& val = container[key]; if (val.is_bool()) { return val.bool_value(); } diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 4a9b8861ae0a..6ce7da6d2a30 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -616,7 +616,8 @@ typedef struct AuthLuaRecordContext static thread_local unique_ptr s_lua_record_ctx; -static vector genericIfUp(const boost::variant& ips, boost::optional options, std::function upcheckf, uint16_t port = 0) { +static vector genericIfUp(const boost::variant& ips, boost::optional options, const std::function& upcheckf, uint16_t port = 0) +{ vector > candidates; opts_t opts; if(options) @@ -894,7 +895,7 @@ static void setupLuaRecords(LuaContext& lua) auto checker = [](const ComboAddress& addr, const opts_t& opts) { return g_up.isUp(addr, opts); }; - return genericIfUp(ips, std::move(options), checker, port); + return genericIfUp(ips, options, checker, port); }); lua.writeFunction("ifurlextup", [](const vector >& ipurls, boost::optional options) { diff --git a/pdns/statnode.cc b/pdns/statnode.cc index a2b493496edb..898c221094da 100644 --- a/pdns/statnode.cc +++ b/pdns/statnode.cc @@ -35,8 +35,7 @@ StatNode::Stat StatNode::print(unsigned int depth, Stat newstat, bool silent) co return newstat; } - -void StatNode::visit(visitor_t visitor, Stat &newstat, unsigned int depth) const +void StatNode::visit(const visitor_t& visitor, Stat& newstat, unsigned int depth) const { Stat childstat(s); @@ -49,8 +48,7 @@ void StatNode::visit(visitor_t visitor, Stat &newstat, unsigned int depth) const newstat += childstat; } - -void StatNode::submit(const DNSName& domain, int rcode, unsigned int bytes, bool hit, boost::optional remote) +void StatNode::submit(const DNSName& domain, int rcode, unsigned int bytes, bool hit, const boost::optional& remote) { // cerr<<"FIRST submit called on '"< tmp = domain.getRawLabels(); @@ -69,7 +67,7 @@ void StatNode::submit(const DNSName& domain, int rcode, unsigned int bytes, bool www.powerdns.com. */ -void StatNode::submit(std::vector::const_iterator end, std::vector::const_iterator begin, const std::string& domain, int rcode, unsigned int bytes, boost::optional remote, unsigned int count, bool hit) +void StatNode::submit(std::vector::const_iterator end, std::vector::const_iterator begin, const std::string& domain, int rcode, unsigned int bytes, const boost::optional& remote, unsigned int count, bool hit) { // cerr<<"Submit called for domain='"< remote); + void submit(const DNSName& domain, int rcode, unsigned int bytes, bool hit, const boost::optional& remote); Stat print(unsigned int depth=0, Stat newstat=Stat(), bool silent=false) const; - void visit(visitor_t visitor, Stat& newstat, unsigned int depth=0) const; + void visit(const visitor_t& visitor, Stat& newstat, unsigned int depth = 0) const; bool empty() const { return children.empty() && s.remotes.empty(); @@ -75,5 +75,5 @@ public: children_t children; private: - void submit(std::vector::const_iterator end, std::vector::const_iterator begin, const std::string& domain, int rcode, unsigned int bytes, boost::optional remote, unsigned int count, bool hit); + void submit(std::vector::const_iterator end, std::vector::const_iterator begin, const std::string& domain, int rcode, unsigned int bytes, const boost::optional& remote, unsigned int count, bool hit); }; diff --git a/pdns/tcpiohandler.hh b/pdns/tcpiohandler.hh index 9e0dfdf5aa8d..058d10443b71 100644 --- a/pdns/tcpiohandler.hh +++ b/pdns/tcpiohandler.hh @@ -232,15 +232,16 @@ protected: class TCPIOHandler { public: - - TCPIOHandler(const std::string& host, bool hostIsAddr, int socket, const struct timeval& timeout, std::shared_ptr ctx): d_socket(socket) + TCPIOHandler(const std::string& host, bool hostIsAddr, int socket, const struct timeval& timeout, const std::shared_ptr& ctx) : + d_socket(socket) { if (ctx) { d_conn = ctx->getClientConnection(host, hostIsAddr, d_socket, timeout); } } - TCPIOHandler(int socket, const struct timeval& timeout, std::shared_ptr ctx, time_t now): d_socket(socket) + TCPIOHandler(int socket, const struct timeval& timeout, const std::shared_ptr& ctx, time_t now) : + d_socket(socket) { if (ctx) { d_conn = ctx->getConnection(d_socket, timeout, now); diff --git a/pdns/webserver.cc b/pdns/webserver.cc index 95fee8aebda5..ec4b09f5f94a 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -222,7 +222,8 @@ void WebServer::registerWebHandler(const string& url, const HandlerFunction& han registerBareHandler(url, f, method); } -static void *WebServerConnectionThreadStart(const WebServer* webServer, std::shared_ptr client) { +static void* WebServerConnectionThreadStart(const WebServer* webServer, const std::shared_ptr& client) +{ setThreadName("rec/webhndlr"); const std::string msg = "Exception while serving a connection in main webserver thread"; try {