Skip to content

clang-tidy: resolve bugprone-unchecked-optional-access#495

Merged
knoepfel merged 6 commits intomainfrom
maintenance/clang-tidy/bugprone-unchecked-optional-access
Apr 13, 2026
Merged

clang-tidy: resolve bugprone-unchecked-optional-access#495
knoepfel merged 6 commits intomainfrom
maintenance/clang-tidy/bugprone-unchecked-optional-access

Conversation

@greenc-FNAL
Copy link
Copy Markdown
Contributor

  • test/form/form_basics_test.cpp
  • test/identifier.cpp

clang-tidy does not recognize Catch2's REQUIRE macro as protecting
the target std::optional against non-existent-value access; use // NOLINT to silence the warning.

  • plugins/python/src/modulewrap.cpp

clang-tidy correctly noted that there was a (theoretical) path to
unprotected access to an std::optional. We now throw an
std::logic_error and protect it from coverage validation as a should never happen untestable case.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses clang-tidy’s bugprone-unchecked-optional-access findings by suppressing false positives in Catch2 tests and by adding a defensive fallback path in the Python module wrapper when an expected Python error message cannot be retrieved.

Changes:

  • Add NOLINTNEXTLINE(bugprone-unchecked-optional-access) annotations in Catch2 tests where REQUIRE(...) guards std::optional dereference.
  • Adjust a test to use REQUIRE(item) before item->... and suppress the corresponding clang-tidy warning.
  • Add a fallback std::logic_error throw in plugins/python/src/modulewrap.cpp for the “no message available” error-reporting edge case, with a coverage exclusion marker.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/identifier.cpp Suppresses bugprone-unchecked-optional-access for an optional dereference guarded by REQUIRE.
test/form/form_basics_test.cpp Uses REQUIRE(item) and suppresses clang-tidy for guarded optional member access.
plugins/python/src/modulewrap.cpp Adds a defensive fallback exception when Python error text cannot be extracted.

Comment thread plugins/python/src/modulewrap.cpp Outdated
Comment thread test/form/form_basics_test.cpp
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
plugins/python/src/modulewrap.cpp 0.00% 3 Missing ⚠️

❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

@@            Coverage Diff             @@
##             main     #495      +/-   ##
==========================================
+ Coverage   85.54%   85.59%   +0.05%     
==========================================
  Files         142      142              
  Lines        3604     3604              
  Branches      616      615       -1     
==========================================
+ Hits         3083     3085       +2     
  Misses        310      310              
+ Partials      211      209       -2     
Flag Coverage Δ
unittests 85.59% <0.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
plugins/python/src/modulewrap.cpp 78.66% <0.00%> (ø)

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c26bde...ea4c230. Read the comment docs.

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

@greenc-FNAL greenc-FNAL force-pushed the maintenance/clang-tidy/bugprone-unchecked-optional-access branch from 6a806b1 to c5623fb Compare April 8, 2026 21:54
Comment thread plugins/python/src/modulewrap.cpp Outdated
- `test/form/form_basics_test.cpp`
- `test/identifier.cpp`

`clang-tidy` does not recognize Catch2's `REQUIRE` macro as protecting
the target `std::optional` against non-existent-value access; use `//
NOLINT` to silence the warning.

- `plugins/python/src/modulewrap.cpp`

`clang-tidy` correctly noted that there was a (theoretical) path to
unprotected access to an `std::optional`. We now throw an
`std::logic_error` and protect it from coverage validation as a `should
never happen` untestable case.
Claude Sonnet 4.6 claims that `msg_from_py_error()` _will_ return true
when called if `!opq.has_value()`, so an else clause would be
structurally unreachable code.

Additionally, the claim is that the `std::runtime_error()` throw will
never be caught, and we should probably be using `Python_SetErr()` here
and returning `nullptr` instead of throwing exceptions.
@greenc-FNAL greenc-FNAL force-pushed the maintenance/clang-tidy/bugprone-unchecked-optional-access branch from 8ccdc78 to e2f8495 Compare April 10, 2026 20:42
Comment thread plugins/python/src/modulewrap.cpp Outdated
@knoepfel knoepfel merged commit 190ac7f into main Apr 13, 2026
35 of 36 checks passed
@knoepfel knoepfel deleted the maintenance/clang-tidy/bugprone-unchecked-optional-access branch April 13, 2026 16:00
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.

3 participants