Fix clang-tidy performance-unnecessary-copy-initialization warnings

This commit is contained in:
Tim Wojtulewicz 2025-04-25 16:34:11 -07:00
parent 909413838c
commit cb8c35748a
15 changed files with 26 additions and 28 deletions

View file

@ -6,6 +6,7 @@ Checks: [-*,
performance-for-range-copy, performance-for-range-copy,
performance-inefficient-vector-operation, performance-inefficient-vector-operation,
performance-move-const-argument, performance-move-const-argument,
performance-unnecessary-copy-initialization,
# Skipping these temporarily because they are very noisy # Skipping these temporarily because they are very noisy
-bugprone-narrowing-conversions, -bugprone-narrowing-conversions,

View file

@ -1026,8 +1026,8 @@ ValPtr BinaryExpr::TableFold(Val* v1, Val* v2) const {
} }
ValPtr BinaryExpr::AddrFold(Val* v1, Val* v2) const { ValPtr BinaryExpr::AddrFold(Val* v1, Val* v2) const {
IPAddr a1 = v1->AsAddr(); const IPAddr& a1 = v1->AsAddr();
IPAddr a2 = v2->AsAddr(); const IPAddr& a2 = v2->AsAddr();
bool result = false; bool result = false;
switch ( tag ) { switch ( tag ) {

View file

@ -694,8 +694,7 @@ struct type_checker {
else if ( type->Tag() == TYPE_OPAQUE ) { else if ( type->Tag() == TYPE_OPAQUE ) {
// TODO: Could avoid doing the full unserialization here // TODO: Could avoid doing the full unserialization here
// and just check if the type is a correct match. // and just check if the type is a correct match.
auto cpy = a; auto ov = OpaqueVal::UnserializeData(BrokerListView{&a});
auto ov = OpaqueVal::UnserializeData(BrokerListView{&cpy});
return ov != nullptr; return ov != nullptr;
} }
@ -729,14 +728,14 @@ std::optional<broker::data> val_to_data(const Val* v) {
return {broker::port(p->Port(), to_broker_port_proto(p->PortType()))}; return {broker::port(p->Port(), to_broker_port_proto(p->PortType()))};
} }
case TYPE_ADDR: { case TYPE_ADDR: {
auto a = v->AsAddr(); const auto& a = v->AsAddr();
in6_addr tmp; in6_addr tmp;
a.CopyIPv6(&tmp); a.CopyIPv6(&tmp);
return {broker::address(reinterpret_cast<const uint32_t*>(&tmp), broker::address::family::ipv6, return {broker::address(reinterpret_cast<const uint32_t*>(&tmp), broker::address::family::ipv6,
broker::address::byte_order::network)}; broker::address::byte_order::network)};
} break; } break;
case TYPE_SUBNET: { case TYPE_SUBNET: {
auto s = v->AsSubNet(); const auto& s = v->AsSubNet();
in6_addr tmp; in6_addr tmp;
s.Prefix().CopyIPv6(&tmp); s.Prefix().CopyIPv6(&tmp);
auto a = broker::address(reinterpret_cast<const uint32_t*>(&tmp), broker::address::family::ipv6, auto a = broker::address(reinterpret_cast<const uint32_t*>(&tmp), broker::address::family::ipv6,

View file

@ -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) { 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_t = GenTypeName(t);
auto gen = string("vec_op_") + vec_op + "__CPP(" + op + ", " + gen_t + ")"; auto gen = string("vec_op_") + vec_op + "__CPP(" + op + ", " + gen_t + ")";

View file

@ -190,7 +190,7 @@ void CPP_IndexedInits<T>::Generate(InitsManager* im, std::vector<AttrPtr>& ivec,
} }
case AE_RECORD: { case AE_RECORD: {
auto t = im->Types(e_arg); const auto& t = im->Types(e_arg);
auto rt = cast_intrusive<RecordType>(t); auto rt = cast_intrusive<RecordType>(t);
auto empty_vals = make_intrusive<ListExpr>(); auto empty_vals = make_intrusive<ListExpr>();
auto construct = make_intrusive<RecordConstructorExpr>(empty_vals); auto construct = make_intrusive<RecordConstructorExpr>(empty_vals);
@ -410,7 +410,7 @@ TypePtr CPP_TypeInits::BuildRecordType(InitsManager* im, ValElemVec& init_vals,
while ( i < n ) { while ( i < n ) {
auto s = im->Strings(init_vals[i++]); auto s = im->Strings(init_vals[i++]);
auto id = util::copy_string(s); 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++]; auto attrs_i = init_vals[i++];
AttributesPtr attrs; AttributesPtr attrs;
@ -439,7 +439,7 @@ int CPP_FieldMapping::ComputeOffset(InitsManager* im) const {
fm_offset = r->NumFields(); fm_offset = r->NumFields();
auto id = util::copy_string(field_name.c_str(), field_name.size()); 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; AttributesPtr attrs;
if ( field_attrs >= 0 ) if ( field_attrs >= 0 )

View file

@ -1526,7 +1526,7 @@ ASTBlockAnalyzer::ASTBlockAnalyzer(std::vector<FuncInfo>& funcs) {
auto func = f.Func(); auto func = f.Func();
auto fn = func->GetName(); auto fn = func->GetName();
auto body = f.Body(); const auto& body = f.Body();
// First get the line numbers all sorted out. // First get the line numbers all sorted out.
SetBlockLineNumbers sbln; SetBlockLineNumbers sbln;

View file

@ -471,7 +471,7 @@ bool Reducer::ExprValid(const ID* id, const Expr* e1, const Expr* e2) const {
std::optional<ExprSideEffects>& e1_se = e1->GetOptInfo()->SideEffects(); std::optional<ExprSideEffects>& e1_se = e1->GetOptInfo()->SideEffects();
if ( ! e1_se ) { if ( ! e1_se ) {
bool has_side_effects = false; 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 ) if ( e1_t->Tag() == TYPE_OPAQUE || e1_t->Tag() == TYPE_ANY )
// These have difficult-to-analyze semantics. // These have difficult-to-analyze semantics.

View file

@ -1104,7 +1104,7 @@ StmtPtr AssertStmt::DoReduce(Reducer* c) {
bool WhenInfo::HasUnreducedIDs(Reducer* c) const { bool WhenInfo::HasUnreducedIDs(Reducer* c) const {
for ( auto& cp : *cl ) { 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()) ) if ( when_new_locals.count(cid.get()) == 0 && ! c->ID_IsReduced(cp.Id()) )
return true; return true;

View file

@ -357,7 +357,7 @@ bool MultiZBI::Build(ZAMCompiler* zam, const NameExpr* n, const ExprPList& args)
break; break;
case 2: { case 2: {
auto c2 = consts[1]; const auto& c2 = consts[1];
auto c2_t = c2->GetType()->Tag(); auto c2_t = c2->GetType()->Tag();
ASSERT(c2_t == TYPE_BOOL || c2_t == TYPE_INT || c2_t == TYPE_COUNT); ASSERT(c2_t == TYPE_BOOL || c2_t == TYPE_INT || c2_t == TYPE_COUNT);

View file

@ -540,7 +540,7 @@ const ZAMStmt ZAMCompiler::CompileSchedule(const NameExpr* n, const ConstExpr* c
} }
const ZAMStmt ZAMCompiler::CompileEvent(EventHandler* h, const ListExpr* l) { const ZAMStmt ZAMCompiler::CompileEvent(EventHandler* h, const ListExpr* l) {
auto exprs = l->Exprs(); const auto& exprs = l->Exprs();
unsigned int n = exprs.length(); unsigned int n = exprs.length();
bool all_vars = true; 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; 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) { 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 nt_is_vec = nt->Tag() == TYPE_VECTOR;
auto op = e->GetOp1(); auto op = e->GetOp1();

View file

@ -44,7 +44,7 @@ std::unique_ptr<OpaqueVals> ZAMCompiler::BuildVals(const ListExprPtr& l) {
} }
ZInstAux* ZAMCompiler::InternalBuildVals(const ListExpr* l, int stride) { ZInstAux* ZAMCompiler::InternalBuildVals(const ListExpr* l, int stride) {
auto exprs = l->Exprs(); const auto& exprs = l->Exprs();
int n = exprs.length(); int n = exprs.length();
auto aux = new ZInstAux(n * stride); auto aux = new ZInstAux(n * stride);

View file

@ -616,7 +616,7 @@ const ZAMStmt ZAMCompiler::TypeSwitch(const SwitchStmt* sw, const NameExpr* v, c
} }
const ZAMStmt ZAMCompiler::CompileWhile(const WhileStmt* ws) { 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->Tag() == EXPR_CONST ) {
if ( loop_condition->IsZero() ) if ( loop_condition->IsZero() )
@ -1019,7 +1019,7 @@ const ZAMStmt ZAMCompiler::CompileWhen(const WhenStmt* ws) {
aux->wi = wi; aux->wi = wi;
for ( auto i = 0; i < n; ++i ) { 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()); aux->Add(i, FrameSlot(la), la->GetType());
} }
@ -1072,7 +1072,7 @@ const ZAMStmt ZAMCompiler::CompileAssert(const AssertStmt* as) {
auto cond_desc = make_intrusive<StringVal>(new String(as->CondDesc())); auto cond_desc = make_intrusive<StringVal>(new String(as->CondDesc()));
auto cond_desc_e = make_intrusive<ConstExpr>(cond_desc); auto cond_desc_e = make_intrusive<ConstExpr>(cond_desc);
if ( auto msg = as->Msg() ) { if ( const auto& msg = as->Msg() ) {
auto& msg_setup_stmt = as->MsgSetupStmt(); auto& msg_setup_stmt = as->MsgSetupStmt();
if ( msg_setup_stmt ) if ( msg_setup_stmt )
(void)CompileStmt(msg_setup_stmt); (void)CompileStmt(msg_setup_stmt);

View file

@ -15,7 +15,7 @@ Key::Key(const void* session, size_t size, size_t type, bool copy) : size(size),
copied = copy; copied = copy;
} }
Key::Key(Key&& rhs) { Key::Key(Key&& rhs) noexcept {
data = rhs.data; data = rhs.data;
size = rhs.size; size = rhs.size;
copied = rhs.copied; copied = rhs.copied;
@ -25,7 +25,7 @@ Key::Key(Key&& rhs) {
rhs.copied = false; rhs.copied = false;
} }
Key& Key::operator=(Key&& rhs) { Key& Key::operator=(Key&& rhs) noexcept {
if ( this != &rhs ) { if ( this != &rhs ) {
data = rhs.data; data = rhs.data;
size = rhs.size; size = rhs.size;

View file

@ -44,8 +44,8 @@ public:
// Implement move semantics for Key, since they're used as keys // Implement move semantics for Key, since they're used as keys
// in a map. // in a map.
Key(Key&& rhs); Key(Key&& rhs) noexcept;
Key& operator=(Key&& rhs); Key& operator=(Key&& rhs) noexcept;
// Explicitly delete the copy constructor and operator since copying // Explicitly delete the copy constructor and operator since copying
// may cause issues with double-freeing pointers. // may cause issues with double-freeing pointers.

View file

@ -491,9 +491,7 @@ void ScriptTarget::DoGenerate() const {
pkg_deps[i]->Generate(); pkg_deps[i]->Generate();
} }
for ( size_t i = 0; i < dir_contents.size(); ++i ) { for ( const auto& f : dir_contents ) {
string f = dir_contents[i];
if ( targets.find(f) != targets.end() ) if ( targets.find(f) != targets.end() )
continue; continue;