From 6a3e3c0c914997eee1d1633c2411d56bbeec72c1 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Sat, 11 Apr 2026 16:39:42 +0800 Subject: [PATCH] [CODE HEALTH] Fix clang-tidy narrowing conversions in baggage Wrap three int->char conversions inside Baggage::UrlEncode and Baggage::UrlDecode with explicit static_cast(...) so that cppcoreguidelines-narrowing-conversions stops firing on api/include/opentelemetry/baggage/baggage.h:238,239,270 under clang-tidy 20. The change is purely a source-level cast: c >> 4 and c & 15 are already bounded to [0, 15] by the surrounding mask/shift, and the bitwise OR of two nibbles in UrlDecode fits in a single byte. The existing baggage_test cases cover both UrlEncode and UrlDecode round-trips and remain green. Decrement the clang-tidy warning_limit by 3 for both all-options-abiv1-preview and all-options-abiv2-preview matrix entries to ratchet the limit down to the new measured count. Resolves #3980. Part of #2053. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- .github/workflows/clang-tidy.yaml | 4 ++-- CHANGELOG.md | 3 +++ api/include/opentelemetry/baggage/baggage.h | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/clang-tidy.yaml b/.github/workflows/clang-tidy.yaml index ec03ee71c2..3018fe8a12 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: 57 + warning_limit: 54 - cmake_options: all-options-abiv2-preview - warning_limit: 59 + warning_limit: 56 env: CC: /usr/bin/clang-18 CXX: /usr/bin/clang++-18 diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b494ff9da..7bc8d2fc60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,9 @@ Increment the: * [CODE HEALTH] Cleanup nostd variant access in API and SDK [#3965](https://github.com/open-telemetry/opentelemetry-cpp/pull/3965) +* [CODE HEALTH] Fix clang-tidy narrowing conversions in baggage + [#3989](https://github.com/open-telemetry/opentelemetry-cpp/pull/3989) + * Enable WITH_OTLP_RETRY_PREVIEW by default [#3953](https://github.com/open-telemetry/opentelemetry-cpp/pull/3953) diff --git a/api/include/opentelemetry/baggage/baggage.h b/api/include/opentelemetry/baggage/baggage.h index 6e2354366c..0dbf21281a 100644 --- a/api/include/opentelemetry/baggage/baggage.h +++ b/api/include/opentelemetry/baggage/baggage.h @@ -235,8 +235,8 @@ class OPENTELEMETRY_EXPORT Baggage else { ret.push_back('%'); - ret.push_back(to_hex(c >> 4)); - ret.push_back(to_hex(c & 15)); + ret.push_back(to_hex(static_cast(c >> 4))); + ret.push_back(to_hex(static_cast(c & 15))); } } @@ -267,7 +267,7 @@ class OPENTELEMETRY_EXPORT Baggage err = 1; return ""; } - ret.push_back(from_hex(str[i + 1]) << 4 | from_hex(str[i + 2])); + ret.push_back(static_cast(from_hex(str[i + 1]) << 4 | from_hex(str[i + 2]))); i += 2; } else if (str[i] == '+')