Skip to content

[CODE HEALTH] fix clang-tidy cppcoreguidelines-pro-type-const-cast warnings - #4482

Open
Anish701 wants to merge 7 commits into
open-telemetry:mainfrom
Anish701:fix-const-cast-4477
Open

[CODE HEALTH] fix clang-tidy cppcoreguidelines-pro-type-const-cast warnings#4482
Anish701 wants to merge 7 commits into
open-telemetry:mainfrom
Anish701:fix-const-cast-4477

Conversation

@Anish701

@Anish701 Anish701 commented Aug 26, 2026

Copy link
Copy Markdown

Fixes #4477

Changes

Resolves all cppcoreguidelines-pro-type-const-cast warnings by eliminating const_cast usage through proper typing .

  • examples/http: Accept const T& in HttpTextMapCarrier constructor, removing the need to const_cast request headers.
  • ext/http/client/curl: Change SetCurlPtrOption to accept const void*, and silence const_cast warning for string data.
  • ext/test/w3c_tracecontext_http_test_server: Copy headers by value instead of const_casting a reference.
  • sdk/common/circular_buffer.h: Make PeekImpl() a const method, eliminating the const_cast<CircularBuffer*>(this) in Peek().
  • CHANGELOG.md updated for non-trivial changes - N/A this is a trivial code health change
  • Unit tests have been added - N/A
  • Changes in public API reviewed - yes mentioned in description

Signed-off-by: Anish701 <anish2sinha@gmail.com>
@Anish701
Anish701 requested a review from a team as a code owner August 26, 2026 04:51
@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 26, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Anish701 / name: Anish Sinha (4297c36)
  • ✅ login: Anish701 / name: Anish701 (d2890d8)

@Anish701 Anish701 changed the title [CODE HEALTH] clang-tidy cppcoreguidelines-pro-type-const-cast warnings [CODE HEALTH] fix clang-tidy cppcoreguidelines-pro-type-const-cast warnings Aug 26, 2026
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 36.84211% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.11%. Comparing base (45f2629) to head (e36cf5b).

Files with missing lines Patch % Lines
ext/src/http/client/curl/http_operation_curl.cc 7.70% 12 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4482      +/-   ##
==========================================
- Coverage   83.12%   83.11%   -0.00%     
==========================================
  Files         519      519              
  Lines       20256    20254       -2     
==========================================
- Hits        16835    16832       -3     
- Misses       3421     3422       +1     
Files with missing lines Coverage Δ
...lude/opentelemetry/metrics/multi_observer_result.h 100.00% <ø> (ø)
...lemetry/ext/http/client/curl/http_operation_curl.h 90.91% <100.00%> (-0.39%) ⬇️
...include/opentelemetry/sdk/common/circular_buffer.h 100.00% <100.00%> (ø)
sdk/src/metrics/multi_observer_result.cc 92.31% <100.00%> (-0.41%) ⬇️
ext/src/http/client/curl/http_operation_curl.cc 60.30% <7.70%> (-0.29%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@marcalff

Copy link
Copy Markdown
Member

@Anish701

Thanks for the cleanup patch.

Note: The methods MultiObserverResult::ForInstrument and MultiObserverResult::HasInstrument have their parameters changed from const ObservableInstrument* to ObservableInstrument*. The previous const qualifier was misleading because there was a const_cast on the pointer. Also, all existing callers already pass non-const pointers. This is not a breaking change to the API. All unit tests pass successfully.

I don't think this is the proper fix, and it is a breaking change to the API: what if an existing application invokes opentelemetry with a const pointer today ?

Please avoid changing this area, in favor of #4499 which is a different fix.

#if LIBCURL_VERSION_NUM >= CURL_VERSION_BITS(7, 77, 0)
const char *data = ssl_options_.ssl_ca_cert_string.c_str();
size_t data_len = ssl_options_.ssl_ca_cert_string.length();
std::vector<char> blob_data(ssl_options_.ssl_ca_cert_string.begin(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

CURL is not changing the blob data given to it.

Making a mutable copy is not necessary, and causes overhead.

@Anish701 Anish701 Aug 29, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok I silenced the lint warnings as you suggested using
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)

@marcalff marcalff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the patch.

Please revert changes related to metrics instruments, to be fixed separately.

Please revert changes related to CURL blob parameters, and silence the warning instead.

Everything else looks ok.

@Anish701

Anish701 commented Aug 29, 2026

Copy link
Copy Markdown
Author

Thanks for the patch.

Please revert changes related to metrics instruments, to be fixed separately.

Please revert changes related to CURL blob parameters, and silence the warning instead.

Everything else looks ok.

@marcalff Thanks for reviewing! I reverted the metrics related changes. I also reverted the changes to the CURL blob params and silenced the warnings with // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast).

Please let me know if any other changes are needed. Thanks!

@Anish701
Anish701 requested a review from marcalff August 29, 2026 20:20
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.

[CODE HEALTH] clang-tidy cppcoreguidelines-pro-type-const-cast warnings

2 participants