<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/93194>93194</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            New false positive in clang-tidy 18 bugprone-unchecked-optional access
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-tidy
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          woutervdstoel
      </td>
    </tr>
</table>

<pre>
    I'm trying to upgrade to clang-tidy-18 and found a regression with the changed bugprone-unchecked-optional-access check.

I managed to distill the following minimal reproducer:

```
#include <iostream>
#include <optional>

std::optional<int> get_optional()
{
  return 5;
}

int main()
{
  for (int j = 0; j < 1; j++)
  {
 }
  auto i = get_optional();
  // perform the check
  if (!i.has_value())
  {
    return 0;
  }
  for (int j = 0; j < 1; ++j)
  {
    // test.cpp:24:19: error: unchecked access to optional value [bugprone-unchecked-optional-access,-warnings-as-errors]
    // 24 | std::cout << *i << std::endl;
    std::cout << *i << std::endl;
 }
  return 0;
}
}```

The reuse of the loop indexing variable name seems to be the root cause.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVEuP4ygQ_jX4UnJkgx0nBx_ymEhz2dPeRxjKNj0YLB7J9r9fmTy13b0tjWSRCsVXVV_xUdx7NRjEltR7Uh8zHsNoXXuxMaA7Sx8s6qyz8r39SWgzQXDvygwQLMR5cFziYgrNzZAHJd_zcgPcSOhtNBI4OBwceq-sgYsKI4QRQYzcDCihi8PsrME8GjGi-I0yt3NQ1nCdcyHQe0j7K1IcSbG7rj9h4oYv8GBBKh-U1ilqb7W2l6W2SRk1cQ0OZ2dlFOgI273GIOvi9l3_UqaM0FEiEHZQ1geHfCLsx2fue4VPd1p9kEsStnv6D8oEwn7AgOHXY5duCN3egM3-agA4DNEZqAnb333H1-jKBJi4Ml_Ae-uA0M1y6g0IO0JB2D6ZByiTSeg-fds75Il-pALgMVhQKcInRbNHPkJPhJ5gRtdbN93uFMXvu1_1kDClWo3c_zpzHfEW5LMK4NGA4jXJs65v-V3pvX0V_VZvQB9WYp4J29GKsF25JWwH6JxdBAIPFcJNfMHCvQWQOACp99-LltBDfuHOKDP4nPs8JfCkPn4oiFZAmgM8tCNsDAuphRehO3W3HwfQSP3SIvhj6EtzP7T-qb3m-N-Xkta_RwSH0SPYPl2-tnYGZST-szy_M3eKdxrB8AnBI06plR2ms87aAIJHj6tMtkxu2ZZn2JZNua4aVlXrbGxpwda8Ek3R9bhZV8W6rutOdqIsqqZf11WmWlrQqqgpK6uyqMpVXfJGiO0G2bpiXSNJVeDElV5pfZ5W1g2Z8j5iu2Xltso071D7NPEofc4uQukyAV27gPIuDp5UhVY--GeYoILG9i-8QM-1R5itV0GdEZR5mYJQbv5vut0ElkWn2zGE2S9XlCQxqDDGbiXsROhpSXr7yWdn31AEQk-JiCf0dOVybum_AQAA__82jrRF">