From 785ec9a5caa653ec91ca13e1301a1fbb88968334 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Mon, 18 May 2026 21:31:21 +0800 Subject: [PATCH] [CODE HEALTH] Fix clang-tidy narrowing-conversions warnings in otlp_populate_attribute_utils After the clang-tidy v22 upgrade in #4066, four narrowing-conversion warnings surfaced in otlp_populate_attribute_utils.cc at the four uint64_t to int64_t call sites passing to proto set_int_value. The original code suppressed via NOLINT(cppcoreguidelines-narrowing-conversions). In clang-tidy v22 both bugprone-narrowing-conversions and cppcoreguidelines-narrowing-conversions are registered as separate aliases, and NOLINT for one alias does not suppress the report under the other. So an alias-name-rename approach cannot silence the warnings with a single NOLINT. Replace the four NOLINT-suppressed implicit narrowings with explicit static_cast conversions, matching the pattern established in and makes the narrowing explicit at the call site. The fifth NOLINT at line ~297 (uint32_t to int64_t) is signed widening, not narrowing; clang-tidy v22 emits no diagnostic there. Left untouched. Ratchet: * abiv1-preview warning_limit lowered from 418 to 414 * abiv2-preview warning_limit lowered from 424 to 420 Part of #2053 Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- .github/workflows/clang-tidy.yaml | 4 ++-- CHANGELOG.md | 3 +++ .../otlp/src/otlp_populate_attribute_utils.cc | 16 ++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.github/workflows/clang-tidy.yaml b/.github/workflows/clang-tidy.yaml index 19ec7f1573..2cc9b91eaa 100644 --- a/.github/workflows/clang-tidy.yaml +++ b/.github/workflows/clang-tidy.yaml @@ -17,9 +17,9 @@ jobs: matrix: include: - cmake_options: all-options-abiv1-preview - warning_limit: 389 + warning_limit: 385 - cmake_options: all-options-abiv2-preview - warning_limit: 395 + warning_limit: 391 env: CC: /usr/bin/clang-22 CXX: /usr/bin/clang++-22 diff --git a/CHANGELOG.md b/CHANGELOG.md index e57d7fa70b..d953ee278b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ Increment the: * [CODE HEALTH] Fix IWYU Clang22 warnings [#4083](https://github.com/open-telemetry/opentelemetry-cpp/pull/4083) +* [CODE HEALTH] Fix clang-tidy narrowing-conversions warnings in otlp_populate_attribute_utils + [#4090](https://github.com/open-telemetry/opentelemetry-cpp/pull/4090) + * [CODE HEALTH] Remove unused alias declarations [#4091](https://github.com/open-telemetry/opentelemetry-cpp/pull/4091) diff --git a/exporters/otlp/src/otlp_populate_attribute_utils.cc b/exporters/otlp/src/otlp_populate_attribute_utils.cc index 75e47998ab..1fa384553a 100644 --- a/exporters/otlp/src/otlp_populate_attribute_utils.cc +++ b/exporters/otlp/src/otlp_populate_attribute_utils.cc @@ -75,8 +75,8 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue( } else if (nostd::holds_alternative(value)) { - proto_value->set_int_value( - nostd::get(value)); // NOLINT(cppcoreguidelines-narrowing-conversions) + // uint64_t narrowed to int64_t; OTLP int_value is signed, behavior intentional. + proto_value->set_int_value(static_cast(nostd::get(value))); } else if (nostd::holds_alternative(value)) { @@ -166,10 +166,10 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue( else if (nostd::holds_alternative>(value)) { auto array_value = proto_value->mutable_array_value(); + // uint64_t narrowed to int64_t; OTLP int_value is signed, behavior intentional. for (const auto &val : nostd::get>(value)) { - array_value->add_values()->set_int_value( - val); // NOLINT(cppcoreguidelines-narrowing-conversions) + array_value->add_values()->set_int_value(static_cast(val)); } } else if (nostd::holds_alternative>(value)) @@ -235,8 +235,8 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue( } else if (nostd::holds_alternative(value)) { - proto_value->set_int_value( - nostd::get(value)); // NOLINT(cppcoreguidelines-narrowing-conversions) + // uint64_t narrowed to int64_t; OTLP int_value is signed, behavior intentional. + proto_value->set_int_value(static_cast(nostd::get(value))); } else if (nostd::holds_alternative(value)) { @@ -311,10 +311,10 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue( else if (nostd::holds_alternative>(value)) { auto array_value = proto_value->mutable_array_value(); + // uint64_t narrowed to int64_t; OTLP int_value is signed, behavior intentional. for (const auto &val : nostd::get>(value)) { - array_value->add_values()->set_int_value( - val); // NOLINT(cppcoreguidelines-narrowing-conversions) + array_value->add_values()->set_int_value(static_cast(val)); } } else if (nostd::holds_alternative>(value))