Description
This patch adds separate implementations of several assertion macros under CATCH_CONFIG_EXPERIMENTAL_STATIC_ANALYSIS_SUPPORT, so that single-TU static analysis, such as clang-tidy's bugprone-unchecked-optional-access, can reason about Catch2's assertions.
The goal is to remove a class of false positives (in particular for flow-sensitive analysers) that users currently get in every test that guards with REQUIRE.
The main idea is to model early termination in case of REQUIRE macro by using Catch::Detail::Unreachable().
Testing
I tried to do automated testing based on guidance from https://github.com/catchorg/Catch2/pull/3194#issuecomment-5400964184
Manual testing results are posted in https://github.com/catchorg/Catch2/pull/3194#issuecomment-5349279602
GitHub Issues
Partially address https://github.com/catchorg/Catch2/issues/3170
Some discussed follow-ups have not been implemented yet:
- Support of
REQUIRE_THAT - Support of
REQUIRE_THROWS_MATCHES - Support of
REQUIRE_THROWS_WITH
Activity
14Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 91.18%. Comparing base (0809213) to head (21ba6fd).
Additional details and impacted files
@@ Coverage Diff @@
## devel #3194 +/- ##
=======================================
Coverage 91.18% 91.18%
=======================================
Files 205 205
Lines 9101 9101
=======================================
Hits 8298 8298
Misses 803 803 :rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
At a glance the changes look correct. The tests however, need to be done differently. As a rough draft:
- You should add the examples you manually tested into different .cpp files.
- You should then run clang-tidy over these files.
- The output from clang-tidy should be checked for the presence of the warnings -> the REQUIRE example should not have warning, the CHECK examples should have warning.
We already have some scripts that do their own builds of Catch2 in tests/TestScripts, e.g. in the DiscoverTests subfolder. I would probably add a tests/TestProjects/StaticAnalysis/ subfolder with new CMakeLists.txt, that adds the relevant source files as their own binaries excluded from full build. Then add a new script that invokes their build and captures stdout. By calling this script in the CI job with clang-tidy, the full build should be properly configured to give you clang-tidy's output instead of compiler's.
@horenmar I pushed my trial to implement your draft but I am not sure that did everything correctly with cmake setup and python script scope so bear with me please :)
Few comments:
- I am not sure how to trigger Github Actions, I assume it requires your approval for workflow, right?
- Existing
clang-tidyversion in CI is too old to test my implementation (I put some comments why). For now, I decided to have a separate CI step with newer version of clang-tidy but maybe you want to bump it for everything. But IMO it would be better to make it in a separate PR and then rebase/refactor this one.
Thank you for your review and recommendation!
@horenmar friendly ping to not lose the PR
Thank you for your time and review :)
@horenmar friendly ping
Questions from https://github.com/catchorg/Catch2/pull/3194#discussion_r3966311881 and https://github.com/catchorg/Catch2/pull/3194#issuecomment-5584083072 are still relevant
Manual tests
REQUIRE/REQUIRE_FALSECode example:
clang-tidy output:
CHECKED_IF/CHECKED_ELSECode example:
clang-tidy output:
REQUIRE_NOTHROWCode example:
clang-tidy output:
REQUIRE_THROWSCode example:
clang-tidy output:
REQUIRE_THROWS_ASCode example:
clang-tidy output: