Skip to content
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

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

Open
woutervdstoel opened this issue May 23, 2024 · 1 comment
Open
Labels
clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang-tidy 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).

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 clang-tidy false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

3 participants