Skip to content

[CODE HEALTH] Fix clang-tidy narrowing-conversions warnings in sync_instruments#4013

Merged
marcalff merged 3 commits into
open-telemetry:mainfrom
thc1006:codehealth/sync-instruments-narrowing
Apr 21, 2026
Merged

[CODE HEALTH] Fix clang-tidy narrowing-conversions warnings in sync_instruments#4013
marcalff merged 3 commits into
open-telemetry:mainfrom
thc1006:codehealth/sync-instruments-narrowing

Conversation

@thc1006
Copy link
Copy Markdown
Contributor

@thc1006 thc1006 commented Apr 20, 2026

Summary

Eliminates the last two cppcoreguidelines-narrowing-conversions warnings remaining in the project. Both occur in the abiv2-only LongHistogram::Record overloads in sdk/src/metrics/sync_instruments.cc (lines 463 and 475), where a uint64_t value parameter is passed directly to SyncWritableMetricStorage::RecordLong, whose signature expects int64_t. Wrapping the argument in static_cast<int64_t> makes the narrowing explicit and silences the diagnostic.

The cast is observably equivalent to the prior code. Implicit narrowing under two's-complement was already producing the same bit pattern; this PR only makes the conversion explicit at the call site. There is no behavior change.

After this change, the project's narrowing-conversions warning count is 0 under both all-options-abiv1-preview and all-options-abiv2-preview, completing the narrowing-conversions cleanup tracked under #2053.

Ratchet

  • all-options-abiv1-preview warning_limit unchanged at 31 (no abiv1 narrowing warnings in this file)
  • all-options-abiv2-preview warning_limit lowered from 33 to 31 (the two warnings being fixed are abiv2-only)

Verification

Counts measured against artifacts of the most recent successful CI run on main (run 24674777807, head 699664b7):

Preset Before (count / limit) After (predicted)
abiv1-preview 31 / 31 31 / 31
abiv2-preview 33 / 33 31 / 31

Test coverage

The existing TEST(SyncInstruments, LongHistogram) in sdk/test/metrics/sync_instruments_test.cc exercised only the non-abiv2 Record(int64_t, ...) overloads. The abiv2-only Record(uint64_t) and Record(uint64_t, KeyValueIterable&) overloads being modified in this PR were not previously covered. This PR adds calls to both overloads, guarded by #if OPENTELEMETRY_ABI_VERSION_NO >= 2, so the abiv2 build now exercises the modified code paths.

Test plan

  • CI clang-tidy job passes both abiv1-preview and abiv2-preview at the new limit
  • CI metrics tests cover the abiv2-only Record(uint64_t) overloads via the new test additions
  • No behavioral change to LongHistogram::Record

Part of #2053

@thc1006 thc1006 requested a review from a team as a code owner April 20, 2026 17:10
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Apr 20, 2026
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
@thc1006 thc1006 force-pushed the codehealth/sync-instruments-narrowing branch 2 times, most recently from 8575386 to 3d09f19 Compare April 20, 2026 17:27
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.34%. Comparing base (785f3cd) to head (a159fcb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #4013   +/-   ##
=======================================
  Coverage   90.34%   90.34%           
=======================================
  Files         230      230           
  Lines        7303     7303           
=======================================
  Hits         6597     6597           
  Misses        706      706           
Files with missing lines Coverage Δ
sdk/src/metrics/sync_instruments.cc 65.84% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 open-telemetry#2053

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
@thc1006 thc1006 force-pushed the codehealth/sync-instruments-narrowing branch from 3d09f19 to 68f896a Compare April 20, 2026 18:13
Copy link
Copy Markdown
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the cleanup.

@thc1006 thc1006 requested a review from marcalff April 20, 2026 22:48
Copy link
Copy Markdown
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marcalff marcalff merged commit b6d9bc2 into open-telemetry:main Apr 21, 2026
68 checks passed
@thc1006 thc1006 deleted the codehealth/sync-instruments-narrowing branch April 21, 2026 08:02
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request May 18, 2026
…opulate_attribute_utils

After the clang-tidy v22 upgrade in open-telemetry#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<int64_t> conversions, matching the pattern established in
open-telemetry#3989, open-telemetry#3997, and open-telemetry#4013. This removes the alias dependency entirely
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 open-telemetry#2053

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request May 18, 2026
…opulate_attribute_utils

After the clang-tidy v22 upgrade in open-telemetry#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<int64_t> conversions, matching the pattern established in
open-telemetry#3989, open-telemetry#3997, and open-telemetry#4013. This removes the alias dependency entirely
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 open-telemetry#2053

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants