[clang] [clang-tools-extra] [clang-tidy] `bugprone-unchecked-optional-access`: Improve handling of value constructors for `bsl::optional` and `bdlb::NullableValue` (PR #224969)

Jan Voung via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 2 06:45:00 PDT 2026


================
@@ -272,15 +278,29 @@ auto isOptionalNulloptConstructor() {
 }
 
 auto isOptionalInPlaceConstructor() {
-  return cxxConstructExpr(hasArgument(0, hasType(inPlaceClass())),
+  return cxxConstructExpr(hasAnyArgument(hasType(inPlaceClass())),
                           hasOptionalOrDerivedType());
 }
 
+// `optional(value, ...)`. Arguments after the value are ignored. Tag types are
+// excluded because they denote other constructions.
 auto isOptionalValueOrConversionConstructor() {
   return cxxConstructExpr(
       unless(hasDeclaration(
           cxxConstructorDecl(anyOf(isCopyConstructor(), isMoveConstructor())))),
-      argumentCountIs(1), hasArgument(0, unless(hasNulloptType())),
+      argumentCountAtLeast(1),
+      hasArgument(0, unless(anyOf(hasNulloptType(), hasType(inPlaceClass()),
+                                  hasAllocatorArgType()))),
+      hasOptionalOrDerivedType());
+}
+
+// `optional(allocator_arg_t, allocator, value, ...)`.
----------------
jvoung wrote:

Agreed, probably overkill right now to restrict the matcher.

https://github.com/llvm/llvm-project/pull/224969


More information about the cfe-commits mailing list