llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

regression in clang-tidy `bugprone-unchecked-optional-access` with newer libc++

Open jvoung opened this issue 8 months ago • 8 comments

There are some regressions in the clang tidy bugprone-unchecked-optional-access with newer libc++.

I believe after:

  • https://github.com/llvm/llvm-project/pull/183230 , the checker will start having false positives (doesn't properly match the has_value() calls for checks)
  • https://github.com/llvm/llvm-project/pull/185252, the checker will start having false negatives (doesn't properly match the value, operator*, and operator->)

Initial thought is we need to adjust something around here (getPublicReceiverType).

jvoung avatar Mar 20 '26 20:03 jvoung

@llvm/issue-subscribers-clang-tidy

Author: Jan Voung (jvoung)

There are some regressions in the clang tidy `bugprone-unchecked-optional-access` with newer libc++.

I believe after:

  • https://github.com/llvm/llvm-project/pull/183230 , the checker will start having false positives (doesn't properly match the has_value() calls for checks)
  • https://github.com/llvm/llvm-project/pull/185252, the checker will start having false negatives (doesn't properly match the value, operator*, and operator->)

Initial thought is we need to adjust something around here (getPublicReceiverType).

llvmbot avatar Mar 20 '26 20:03 llvmbot

@smallp-o-p are the changes urgent? If not, would you mind reverting while we work on the fix?

jvoung avatar Mar 20 '26 20:03 jvoung

They're NFC, so it should be okay to revert.

Gonna ping @philnik77 @ldionne to help me revert, since I don't have commit access.

smallp-o-p avatar Mar 20 '26 20:03 smallp-o-p

Thank you @smallp-o-p !

One question is, is it intentional that the inheritance of __optional_iterator_base is public for optional? It seems like it exposes some of the internals? Making the inheritance private also helps fix part of this.

jvoung avatar Mar 22 '26 16:03 jvoung

Thank you @smallp-o-p !

One question is, is it intentional that the inheritance of __optional_iterator_base is public for optional? It seems like it exposes some of the internals? Making the inheritance private also helps fix part of this.

It's intentional, we can't just use

using __base::begin using __base::end

in optional itself because it begin() and end() may not exist for certain types such as references to arrays and references to function types. This would cause a compile error when they're allowed types for optional

It's possible I should have put the using __base ... statement in the iterator base into a private block, but I'm unsure about that just yet.

smallp-o-p avatar Mar 22 '26 16:03 smallp-o-p

Do we really need to revert changes on libc++ side if the issue is on clang-tidy for using internal details? If libstdc++ refactored optional, we wouldn't ask them to revert changes, right?


By commit date it seems that these changes didn't land in 22 release so we have plenty of time to fix it till 23 release branches on Jul 14th, 2026

vbvictor avatar Mar 22 '26 16:03 vbvictor

Fair enough about reverts -- ok, I have a possible fix to make things less dependent on the inheritance structure. Testing now!

jvoung avatar Mar 23 '26 13:03 jvoung

Forgot to update the registerMatchers -- one more piece to fix https://github.com/llvm/llvm-project/pull/191681

jvoung avatar Apr 12 '26 03:04 jvoung