[clang-tidy] Check bugprone-unchecked-optional-access triggers a false-positive related to std::reference_wrapper
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
This code triggers a false-positive result from clang-tidy check bugprone-unchecked-optional-access (see https://godbolt.org/z/PEY3Ebrzj):
```c++
#include
#include
#include
struct Group
{
int m_id;
};
struct KeyBase
{
std::optional m_group;
};
struct KeyWrapper
{
const KeyBase& m_keyBase;
};
using KeyWrappers = std::list>;
void check(const KeyWrappers& items)
{
for (const KeyWrapper& item : items)
{
if (item.m_keyBase.m_group.has_value())
{
const Group& g = item.m_keyBase.m_group.value(); // <-- false positive
}
}
}
```
The issue occurs on the main branch (upcoming clang 23) but is present at least since clang 21.
Could be related to #203686 but I am not sure.
Contributor guide
Research direction
Reproduce the false positive with the provided std::reference_wrapper example using the bugprone-unchecked-optional-access check, starting from the Godbolt link and the check's implementation. Trace how the range item is modeled, then add a regression test showing that the guarded optional access produces no diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100