From 68f896aeeddf4dac2346e642861735e3aac1e260 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Tue, 21 Apr 2026 01:22:37 +0800 Subject: [PATCH] [CODE HEALTH] Fix clang-tidy narrowing-conversions in sync_instruments Eliminate the last two cppcoreguidelines-narrowing-conversions warnings in the SDK. Both occur in the abiv2-only LongHistogram::Record overloads, where uint64_t values are passed to RecordLong (which expects int64_t). The cast is observably equivalent to the prior code: implicit narrowing under two's-complement already produced the same bit pattern. Add a test exercising the abiv2-only LongHistogram::Record(uint64_t) and Record(uint64_t, KeyValueIterable&) overloads, which were not covered by the existing LongHistogram test. Ratchet: * abiv1-preview warning_limit unchanged (31) * abiv2-preview warning_limit lowered from 33 to 31 Part of #2053 Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- .github/workflows/clang-tidy.yaml | 2 +- CHANGELOG.md | 3 +++ sdk/src/metrics/sync_instruments.cc | 4 ++-- sdk/test/metrics/sync_instruments_test.cc | 5 +++++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.github/workflows/clang-tidy.yaml b/.github/workflows/clang-tidy.yaml index 4abf4c27c4..e20dca847f 100644 --- a/.github/workflows/clang-tidy.yaml +++ b/.github/workflows/clang-tidy.yaml @@ -19,7 +19,7 @@ jobs: - cmake_options: all-options-abiv1-preview warning_limit: 31 - cmake_options: all-options-abiv2-preview - warning_limit: 33 + warning_limit: 31 env: CC: /usr/bin/clang-18 CXX: /usr/bin/clang++-18 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f8f70a67d..c24a76b40f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,9 @@ Increment the: * [CODE HEALTH] Fix clang-tidy misc-no-recursion warnings [#4009](https://github.com/open-telemetry/opentelemetry-cpp/pull/4009) +* [CODE HEALTH] Fix clang-tidy narrowing-conversions warnings in sync_instruments + [#4013](https://github.com/open-telemetry/opentelemetry-cpp/pull/4013) + Important changes: * Enable WITH_OTLP_RETRY_PREVIEW by default diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 4eac12bb83..ab40ad7336 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -460,7 +460,7 @@ void LongHistogram::Record(uint64_t value, return; } auto context = opentelemetry::context::Context{}; - return storage_->RecordLong(value, attributes, context); + return storage_->RecordLong(static_cast(value), attributes, context); } void LongHistogram::Record(uint64_t value) noexcept @@ -472,7 +472,7 @@ void LongHistogram::Record(uint64_t value) noexcept return; } auto context = opentelemetry::context::Context{}; - return storage_->RecordLong(value, context); + return storage_->RecordLong(static_cast(value), context); } #endif diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index 263441d38c..b07a44f041 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -163,6 +163,11 @@ TEST(SyncInstruments, LongHistogram) opentelemetry::context::Context{}); histogram.Record(10, opentelemetry::common::KeyValueIterableView({}), opentelemetry::context::Context{}); + +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + histogram.Record(10ULL); + histogram.Record(10ULL, opentelemetry::common::KeyValueIterableView({})); +#endif } TEST(SyncInstruments, DoubleHistogram)