From cb8c35748adf818502ed998b82018a976721d44b Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Fri, 25 Apr 2025 16:34:11 -0700 Subject: [PATCH] Fix clang-tidy performance-unnecessary-copy-initialization warnings --- .clang-tidy | 1 + src/Expr.cc | 4 ++-- src/broker/Data.cc | 7 +++---- src/script_opt/CPP/Exprs.cc | 2 +- src/script_opt/CPP/RuntimeInits.cc | 6 +++--- src/script_opt/ProfileFunc.cc | 2 +- src/script_opt/Reduce.cc | 2 +- src/script_opt/Stmt.cc | 2 +- src/script_opt/ZAM/BuiltIn.cc | 2 +- src/script_opt/ZAM/Expr.cc | 6 +++--- src/script_opt/ZAM/Low-Level.cc | 2 +- src/script_opt/ZAM/Stmt.cc | 6 +++--- src/session/Key.cc | 4 ++-- src/session/Key.h | 4 ++-- src/zeekygen/Target.cc | 4 +--- 15 files changed, 26 insertions(+), 28 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 6689d1d3e4..4e0f232cbf 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -6,6 +6,7 @@ Checks: [-*, performance-for-range-copy, performance-inefficient-vector-operation, performance-move-const-argument, + performance-unnecessary-copy-initialization, # Skipping these temporarily because they are very noisy -bugprone-narrowing-conversions, diff --git a/src/Expr.cc b/src/Expr.cc index a0acb01b5e..35391eea34 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -1026,8 +1026,8 @@ ValPtr BinaryExpr::TableFold(Val* v1, Val* v2) const { } ValPtr BinaryExpr::AddrFold(Val* v1, Val* v2) const { - IPAddr a1 = v1->AsAddr(); - IPAddr a2 = v2->AsAddr(); + const IPAddr& a1 = v1->AsAddr(); + const IPAddr& a2 = v2->AsAddr(); bool result = false; switch ( tag ) { diff --git a/src/broker/Data.cc b/src/broker/Data.cc index 2284ac5fd6..1d44e38874 100644 --- a/src/broker/Data.cc +++ b/src/broker/Data.cc @@ -694,8 +694,7 @@ struct type_checker { else if ( type->Tag() == TYPE_OPAQUE ) { // TODO: Could avoid doing the full unserialization here // and just check if the type is a correct match. - auto cpy = a; - auto ov = OpaqueVal::UnserializeData(BrokerListView{&cpy}); + auto ov = OpaqueVal::UnserializeData(BrokerListView{&a}); return ov != nullptr; } @@ -729,14 +728,14 @@ std::optional val_to_data(const Val* v) { return {broker::port(p->Port(), to_broker_port_proto(p->PortType()))}; } case TYPE_ADDR: { - auto a = v->AsAddr(); + const auto& a = v->AsAddr(); in6_addr tmp; a.CopyIPv6(&tmp); return {broker::address(reinterpret_cast(&tmp), broker::address::family::ipv6, broker::address::byte_order::network)}; } break; case TYPE_SUBNET: { - auto s = v->AsSubNet(); + const auto& s = v->AsSubNet(); in6_addr tmp; s.Prefix().CopyIPv6(&tmp); auto a = broker::address(reinterpret_cast(&tmp), broker::address::family::ipv6, diff --git a/src/script_opt/CPP/Exprs.cc b/src/script_opt/CPP/Exprs.cc index 6c8fc1579f..b09eef94bf 100644 --- a/src/script_opt/CPP/Exprs.cc +++ b/src/script_opt/CPP/Exprs.cc @@ -1144,7 +1144,7 @@ string CPPCompile::GenListAssign(const ExprPtr& lhs, const ExprPtr& rhs) { } string CPPCompile::GenVectorOp(const Expr* e, string op, const char* vec_op) { - auto t = e->GetType(); + const auto& t = e->GetType(); auto gen_t = GenTypeName(t); auto gen = string("vec_op_") + vec_op + "__CPP(" + op + ", " + gen_t + ")"; diff --git a/src/script_opt/CPP/RuntimeInits.cc b/src/script_opt/CPP/RuntimeInits.cc index 946d332d0f..6efdad27f4 100644 --- a/src/script_opt/CPP/RuntimeInits.cc +++ b/src/script_opt/CPP/RuntimeInits.cc @@ -190,7 +190,7 @@ void CPP_IndexedInits::Generate(InitsManager* im, std::vector& ivec, } case AE_RECORD: { - auto t = im->Types(e_arg); + const auto& t = im->Types(e_arg); auto rt = cast_intrusive(t); auto empty_vals = make_intrusive(); auto construct = make_intrusive(empty_vals); @@ -410,7 +410,7 @@ TypePtr CPP_TypeInits::BuildRecordType(InitsManager* im, ValElemVec& init_vals, while ( i < n ) { auto s = im->Strings(init_vals[i++]); auto id = util::copy_string(s); - auto type = im->Types(init_vals[i++]); + const auto& type = im->Types(init_vals[i++]); auto attrs_i = init_vals[i++]; AttributesPtr attrs; @@ -439,7 +439,7 @@ int CPP_FieldMapping::ComputeOffset(InitsManager* im) const { fm_offset = r->NumFields(); auto id = util::copy_string(field_name.c_str(), field_name.size()); - auto type = im->Types(field_type); + const auto& type = im->Types(field_type); AttributesPtr attrs; if ( field_attrs >= 0 ) diff --git a/src/script_opt/ProfileFunc.cc b/src/script_opt/ProfileFunc.cc index 7a384a23e2..463b29ab53 100644 --- a/src/script_opt/ProfileFunc.cc +++ b/src/script_opt/ProfileFunc.cc @@ -1526,7 +1526,7 @@ ASTBlockAnalyzer::ASTBlockAnalyzer(std::vector& funcs) { auto func = f.Func(); auto fn = func->GetName(); - auto body = f.Body(); + const auto& body = f.Body(); // First get the line numbers all sorted out. SetBlockLineNumbers sbln; diff --git a/src/script_opt/Reduce.cc b/src/script_opt/Reduce.cc index bcb2044e3a..5808c5f2de 100644 --- a/src/script_opt/Reduce.cc +++ b/src/script_opt/Reduce.cc @@ -471,7 +471,7 @@ bool Reducer::ExprValid(const ID* id, const Expr* e1, const Expr* e2) const { std::optional& e1_se = e1->GetOptInfo()->SideEffects(); if ( ! e1_se ) { bool has_side_effects = false; - auto e1_t = e1->GetType(); + const auto& e1_t = e1->GetType(); if ( e1_t->Tag() == TYPE_OPAQUE || e1_t->Tag() == TYPE_ANY ) // These have difficult-to-analyze semantics. diff --git a/src/script_opt/Stmt.cc b/src/script_opt/Stmt.cc index 92f0b981d2..62acce3441 100644 --- a/src/script_opt/Stmt.cc +++ b/src/script_opt/Stmt.cc @@ -1104,7 +1104,7 @@ StmtPtr AssertStmt::DoReduce(Reducer* c) { bool WhenInfo::HasUnreducedIDs(Reducer* c) const { for ( auto& cp : *cl ) { - auto cid = cp.Id(); + const auto& cid = cp.Id(); if ( when_new_locals.count(cid.get()) == 0 && ! c->ID_IsReduced(cp.Id()) ) return true; diff --git a/src/script_opt/ZAM/BuiltIn.cc b/src/script_opt/ZAM/BuiltIn.cc index f1fbcd210f..8238e122e3 100644 --- a/src/script_opt/ZAM/BuiltIn.cc +++ b/src/script_opt/ZAM/BuiltIn.cc @@ -357,7 +357,7 @@ bool MultiZBI::Build(ZAMCompiler* zam, const NameExpr* n, const ExprPList& args) break; case 2: { - auto c2 = consts[1]; + const auto& c2 = consts[1]; auto c2_t = c2->GetType()->Tag(); ASSERT(c2_t == TYPE_BOOL || c2_t == TYPE_INT || c2_t == TYPE_COUNT); diff --git a/src/script_opt/ZAM/Expr.cc b/src/script_opt/ZAM/Expr.cc index adfc903e20..3e5a2faac9 100644 --- a/src/script_opt/ZAM/Expr.cc +++ b/src/script_opt/ZAM/Expr.cc @@ -540,7 +540,7 @@ const ZAMStmt ZAMCompiler::CompileSchedule(const NameExpr* n, const ConstExpr* c } const ZAMStmt ZAMCompiler::CompileEvent(EventHandler* h, const ListExpr* l) { - auto exprs = l->Exprs(); + const auto& exprs = l->Exprs(); unsigned int n = exprs.length(); bool all_vars = true; @@ -895,7 +895,7 @@ const ZAMStmt ZAMCompiler::CompileIndex(const NameExpr* n1, int n2_slot, const T } } - auto indexes = l->Exprs(); + const auto& indexes = l->Exprs(); ZOp op; @@ -1515,7 +1515,7 @@ const ZAMStmt ZAMCompiler::ConstructVector(const NameExpr* n, const Expr* e) { } const ZAMStmt ZAMCompiler::ArithCoerce(const NameExpr* n, const Expr* e) { - auto nt = n->GetType(); + const auto& nt = n->GetType(); auto nt_is_vec = nt->Tag() == TYPE_VECTOR; auto op = e->GetOp1(); diff --git a/src/script_opt/ZAM/Low-Level.cc b/src/script_opt/ZAM/Low-Level.cc index 87d3de2057..83cc4715f0 100644 --- a/src/script_opt/ZAM/Low-Level.cc +++ b/src/script_opt/ZAM/Low-Level.cc @@ -44,7 +44,7 @@ std::unique_ptr ZAMCompiler::BuildVals(const ListExprPtr& l) { } ZInstAux* ZAMCompiler::InternalBuildVals(const ListExpr* l, int stride) { - auto exprs = l->Exprs(); + const auto& exprs = l->Exprs(); int n = exprs.length(); auto aux = new ZInstAux(n * stride); diff --git a/src/script_opt/ZAM/Stmt.cc b/src/script_opt/ZAM/Stmt.cc index 53cf43479b..3ae1d2a329 100644 --- a/src/script_opt/ZAM/Stmt.cc +++ b/src/script_opt/ZAM/Stmt.cc @@ -616,7 +616,7 @@ const ZAMStmt ZAMCompiler::TypeSwitch(const SwitchStmt* sw, const NameExpr* v, c } const ZAMStmt ZAMCompiler::CompileWhile(const WhileStmt* ws) { - auto loop_condition = ws->Condition(); + const auto& loop_condition = ws->Condition(); if ( loop_condition->Tag() == EXPR_CONST ) { if ( loop_condition->IsZero() ) @@ -1019,7 +1019,7 @@ const ZAMStmt ZAMCompiler::CompileWhen(const WhenStmt* ws) { aux->wi = wi; for ( auto i = 0; i < n; ++i ) { - auto la = local_aggr_slots[i]; + const auto& la = local_aggr_slots[i]; aux->Add(i, FrameSlot(la), la->GetType()); } @@ -1072,7 +1072,7 @@ const ZAMStmt ZAMCompiler::CompileAssert(const AssertStmt* as) { auto cond_desc = make_intrusive(new String(as->CondDesc())); auto cond_desc_e = make_intrusive(cond_desc); - if ( auto msg = as->Msg() ) { + if ( const auto& msg = as->Msg() ) { auto& msg_setup_stmt = as->MsgSetupStmt(); if ( msg_setup_stmt ) (void)CompileStmt(msg_setup_stmt); diff --git a/src/session/Key.cc b/src/session/Key.cc index 91eef2bdb3..1d133b545b 100644 --- a/src/session/Key.cc +++ b/src/session/Key.cc @@ -15,7 +15,7 @@ Key::Key(const void* session, size_t size, size_t type, bool copy) : size(size), copied = copy; } -Key::Key(Key&& rhs) { +Key::Key(Key&& rhs) noexcept { data = rhs.data; size = rhs.size; copied = rhs.copied; @@ -25,7 +25,7 @@ Key::Key(Key&& rhs) { rhs.copied = false; } -Key& Key::operator=(Key&& rhs) { +Key& Key::operator=(Key&& rhs) noexcept { if ( this != &rhs ) { data = rhs.data; size = rhs.size; diff --git a/src/session/Key.h b/src/session/Key.h index 7def5e6cf8..31c48aba0b 100644 --- a/src/session/Key.h +++ b/src/session/Key.h @@ -44,8 +44,8 @@ public: // Implement move semantics for Key, since they're used as keys // in a map. - Key(Key&& rhs); - Key& operator=(Key&& rhs); + Key(Key&& rhs) noexcept; + Key& operator=(Key&& rhs) noexcept; // Explicitly delete the copy constructor and operator since copying // may cause issues with double-freeing pointers. diff --git a/src/zeekygen/Target.cc b/src/zeekygen/Target.cc index b7ba5cc6fd..d9fcbd4b68 100644 --- a/src/zeekygen/Target.cc +++ b/src/zeekygen/Target.cc @@ -491,9 +491,7 @@ void ScriptTarget::DoGenerate() const { pkg_deps[i]->Generate(); } - for ( size_t i = 0; i < dir_contents.size(); ++i ) { - string f = dir_contents[i]; - + for ( const auto& f : dir_contents ) { if ( targets.find(f) != targets.end() ) continue;