Skip to content

New false positive in clang-tidy 18 bugprone-unchecked-optional access #93194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
woutervdstoel opened this issue May 23, 2024 · 2 comments
Closed
Labels
clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html false-positive Warning fires when it should not

Comments

@woutervdstoel
Copy link

woutervdstoel commented May 23, 2024

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.

@PiotrZSL PiotrZSL added the clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html label May 23, 2024
@EugeneZelenko EugeneZelenko added the false-positive Warning fires when it should not label May 23, 2024
@woutervdstoel
Copy link
Author

woutervdstoel commented May 24, 2024

This issue seems to be fixed in current main (used clang-tidy from godbolt).

@jvoung
Copy link
Contributor

jvoung commented Mar 6, 2025

Not sure the cause @woutervdstoel , but seems fixed according to your May comment.

I also checked current trunk clang-tidy in godbolt doesn't repro anymore.

Closing then =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

4 participants