Skip to content

Commit f76e486

Browse files
committed
[CODE HEALTH] Fix clang-tidy narrowing-conversions warnings in tests
Apply static_cast at the narrowing site in three test files: - otlp_metrics_serialization_test.cc: cast loop index when calling protobuf data_points(int) accessors (3 sites). - aggregation_test.cc: cast size_t max_buckets_ on ternary branches where the lvalue is declared int (2 sites). - metric_test_stress.cc: cast hardware_concurrency() result and the uint64_t bucket counts/totals used for int64_t accumulation (3 sites). Decrement clang-tidy warning limits by 8 to match the removed unique warnings. Fixes #3978 Signed-off-by: Mateen Anjum <mateenali66@gmail.com>
1 parent 37a57da commit f76e486

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

.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: 57
20+
warning_limit: 49
2121
- cmake_options: all-options-abiv2-preview
22-
warning_limit: 59
22+
warning_limit: 51
2323
env:
2424
CC: /usr/bin/clang-18
2525
CXX: /usr/bin/clang++-18

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Increment the:
3737
* [CODE HEALTH] Cleanup nostd variant access in API and SDK
3838
[#3965](https://github.com/open-telemetry/opentelemetry-cpp/pull/3965)
3939

40+
* [CODE HEALTH] Fix clang-tidy narrowing-conversions warnings in tests
41+
[#3978](https://github.com/open-telemetry/opentelemetry-cpp/issues/3978)
42+
4043
* Enable WITH_OTLP_RETRY_PREVIEW by default
4144
[#3953](https://github.com/open-telemetry/opentelemetry-cpp/pull/3953)
4245

exporters/otlp/test/otlp_metrics_serialization_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ TEST(OtlpMetricSerializationTest, Counter)
278278
EXPECT_EQ(sum.is_monotonic(), true);
279279
for (size_t i = 0; i < 1; i++)
280280
{
281-
const auto &proto_number_point = sum.data_points(i);
281+
const auto &proto_number_point = sum.data_points(static_cast<int>(i));
282282
EXPECT_EQ(proto_number_point.as_double(), i == 0 ? 10.2 : 20.2);
283283
}
284284

@@ -308,7 +308,7 @@ TEST(OtlpMetricSerializationTest, Histogram)
308308
proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE);
309309
for (size_t i = 0; i < 1; i++)
310310
{
311-
const auto &proto_number_point = histogram.data_points(i);
311+
const auto &proto_number_point = histogram.data_points(static_cast<int>(i));
312312
EXPECT_EQ(proto_number_point.sum(), i == 0 ? 100.2 : 200.2);
313313
}
314314

@@ -381,7 +381,7 @@ TEST(OtlpMetricSerializationTest, ObservableGauge)
381381
otlp_exporter::OtlpMetricUtils::ConvertGaugeMetric(data, &gauge);
382382
for (size_t i = 0; i < 1; i++)
383383
{
384-
const auto &proto_number_point = gauge.data_points(i);
384+
const auto &proto_number_point = gauge.data_points(static_cast<int>(i));
385385
EXPECT_EQ(proto_number_point.as_double(), i == 0 ? 30.2 : 50.2);
386386
}
387387

sdk/test/metrics/aggregation_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ TEST(Aggregation, Base2ExponentialHistogramAggregationMerge)
416416
const int expected_scale =
417417
aggr_point.scale_ < default_point.scale_ ? aggr_point.scale_ : default_point.scale_;
418418
const int expected_max_buckets = aggr_point.max_buckets_ < default_point.max_buckets_
419-
? aggr_point.max_buckets_
420-
: default_point.max_buckets_;
419+
? static_cast<int>(aggr_point.max_buckets_)
420+
: static_cast<int>(default_point.max_buckets_);
421421
const int expected_zero_count = 0;
422422

423423
auto merged_from_default = aggr.Merge(*default_aggr);
@@ -438,8 +438,8 @@ TEST(Aggregation, Base2ExponentialHistogramAggregationMerge)
438438
const int expected_scale =
439439
aggr_point.scale_ < zero_point.scale_ ? aggr_point.scale_ : zero_point.scale_;
440440
const int expected_max_buckets = aggr_point.max_buckets_ < zero_point.max_buckets_
441-
? aggr_point.max_buckets_
442-
: zero_point.max_buckets_;
441+
? static_cast<int>(aggr_point.max_buckets_)
442+
: static_cast<int>(zero_point.max_buckets_);
443443
const int expected_zero_count = 1;
444444

445445
auto merged_from_zero = aggr.Merge(zero_aggr);

sdk/test/metrics/metric_test_stress.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ TEST(HistogramStress, UnsignedInt64)
9595
//
9696
// Start logging threads
9797
//
98-
int record_thread_count = std::thread::hardware_concurrency() - 1;
98+
int record_thread_count = static_cast<int>(std::thread::hardware_concurrency()) - 1;
9999
if (record_thread_count <= 0)
100100
{
101101
record_thread_count = 1;
@@ -160,12 +160,12 @@ TEST(HistogramStress, UnsignedInt64)
160160
int64_t collected_bucket_sum = 0;
161161
for (const auto &count : actual.counts_)
162162
{
163-
collected_bucket_sum += count;
163+
collected_bucket_sum += static_cast<int64_t>(count);
164164
}
165165
ASSERT_EQ(collected_bucket_sum, actual.count_);
166166

167167
collected_sum += opentelemetry::nostd::get<int64_t>(actual.sum_);
168-
collected_count += actual.count_;
168+
collected_count += static_cast<int64_t>(actual.count_);
169169
}
170170

171171
ASSERT_EQ(expected_count, collected_count);

0 commit comments

Comments
 (0)