llvm / llvm/llvm-project

[clang-tidy] Check bugprone-unchecked-optional-access triggers a false-positive related to nested loops

Open
#203,686 2 comments 0 reactions 0 assignees View on GitHub
clang-tidy false-positive
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/joe53TMs4):

```c++
#include
#include

struct Dependency
{
std::optional m_valuePart;
};

void process(const std::list& dependencies, const std::list& values)
{
for (const Dependency& dependency : dependencies) // outer loop
{
for (int val : values) // inner loop — unrelated to dependency
{
if (dependency.m_valuePart.has_value()) // std::optional check
{
int vp = dependency.m_valuePart.value(); // <-- false positive
}
}
}
}
```

The issue occurs on the main branch (upcoming clang 23) but is present at least since clang 21.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the provided nested-loop example on the main branch and inspect the implementation and tests for bugprone-unchecked-optional-access. Trace why the inner loop invalidates the has_value() reasoning, then add a regression test and confirm the false-positive diagnostic is gone without weakening valid warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.