Skip to content

Commit 45f2629

Browse files
[CODE HEALTH] Replace push_back with emplace_back where applicable (#4476) (#4485)
1 parent 75e36ac commit 45f2629

16 files changed

Lines changed: 30 additions & 26 deletions

File tree

‎.github/workflows/clang-tidy.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
matrix:
1818
include:
1919
- cmake_options: all-options-abiv1-preview
20-
warning_limit: 133
20+
warning_limit: 113
2121
- cmake_options: all-options-abiv2-preview
22-
warning_limit: 144
22+
warning_limit: 124
2323
env:
2424
CC: /usr/bin/clang-22
2525
CXX: /usr/bin/clang++-22

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ Increment the:
6868
in the value as-is rather than dropping the attribute.
6969
[#1536](https://github.com/open-telemetry/opentelemetry-cpp/issues/1536)
7070

71+
* [CLEANUP] Replace `push_back` with `emplace_back` where applicable to
72+
construct objects in-place, avoiding unnecessary temporaries.
73+
[#4476](https://github.com/open-telemetry/opentelemetry-cpp/pull/4476)
74+
7175
* [BUG] Fix `MultiSpanProcessor` and `MultiLogRecordProcessor` not reporting
7276
failures from `ForceFlush` and `Shutdown` when a child processor failed.
7377
[#4472](https://github.com/open-telemetry/opentelemetry-cpp/pull/4472)

‎api/test/baggage/propagation/baggage_propagator_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ TEST(BaggagePropagatorTest, ExtractAndInjectBaggage)
8787

8888
std::vector<std::string> fields;
8989
format.Fields([&fields](nostd::string_view field) {
90-
fields.push_back(field.data());
90+
fields.emplace_back(field.data());
9191
return true;
9292
});
9393
EXPECT_EQ(fields.size(), 1);

‎api/test/context/propagation/composite_propagator_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ TEST_F(CompositePropagatorTest, Inject)
142142

143143
std::vector<std::string> fields;
144144
composite_propagator_->Fields([&fields](nostd::string_view field) {
145-
fields.push_back(field.data());
145+
fields.emplace_back(field.data());
146146
return true;
147147
});
148148
EXPECT_EQ(fields.size(), 3);

‎api/test/context/runtime_context_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ TEST(RuntimeContextTest, DetachOutOfOrder)
124124
contexts.reserve(indices.size());
125125
for (auto i : indices)
126126
{
127-
contexts.push_back(context::Context("index", static_cast<int64_t>(i)));
127+
contexts.emplace_back("index", static_cast<int64_t>(i));
128128
}
129129

130130
do

‎api/test/nostd/shared_ptr_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static void SharedPtrTest_Sort(size_t size = 10)
221221

222222
for (int i = static_cast<int>(size); i > 0; i--)
223223
{
224-
nums.push_back(shared_ptr<int>(new int(i)));
224+
nums.emplace_back(new int(i));
225225
}
226226

227227
auto nums2 = nums;

‎api/test/trace/propagation/http_text_format_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ TEST(GlobalPropagator, SetAndGet)
262262

263263
std::vector<std::string> fields;
264264
propagator->Fields([&fields](nostd::string_view field) {
265-
fields.push_back(field.data());
265+
fields.emplace_back(field.data());
266266
return true;
267267
});
268268
EXPECT_EQ(fields.size(), 2);

‎api/test/trace/propagation/jaeger_propagation_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ TEST(JaegerPropagatorTest, InjectsContext)
191191

192192
std::vector<std::string> fields;
193193
format.Fields([&fields](nostd::string_view field) {
194-
fields.push_back(field.data());
194+
fields.emplace_back(field.data());
195195
return true;
196196
});
197197
EXPECT_EQ(fields.size(), 1);

‎examples/multithreaded/main.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ void run_threads()
6060
{
6161
// This shows how one can effectively use Scope objects to correctly
6262
// parent spans across threads.
63-
threads.push_back(std::thread([=] {
63+
threads.emplace_back([=] {
6464
trace_api::Scope scope(thread_span);
6565
auto thread_span_2 =
6666
get_tracer()->StartSpan(std::string("thread ") + std::to_string(thread_num));
67-
}));
67+
});
6868
}
6969

7070
std::for_each(threads.begin(), threads.end(), [](std::thread &th) { th.join(); });

‎exporters/otlp/src/otlp_file_client.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,17 +565,17 @@ class OPENTELEMETRY_LOCAL_SYMBOL FileSystemUtil
565565
}
566566
else
567567
{
568-
out.push_back(token);
568+
out.emplace_back(token);
569569
}
570570
}
571571
else if (0 != std::strcmp(".", token))
572572
{
573-
out.push_back(token);
573+
out.emplace_back(token);
574574
}
575575
}
576576
else
577577
{
578-
out.push_back(token);
578+
out.emplace_back(token);
579579
}
580580
}
581581
token = SAFE_STRTOK_S(nullptr, "\\/", &saveptr);

0 commit comments

Comments
 (0)