[clang] [clang-tools-extra] [clang-tidy] Add `IgnoreValueCalls` option to bugprone-unchecked-optional-access (PR #167209)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 10 00:38:26 PST 2025


================
@@ -1153,26 +1153,30 @@ auto buildDiagnoseMatchSwitch(
   // FIXME: Evaluate the efficiency of matchers. If using matchers results in a
   // lot of duplicated work (e.g. string comparisons), consider providing APIs
   // that avoid it through memoization.
-  auto IgnorableOptional = ignorableOptional(Options);
-  return CFGMatchSwitchBuilder<
-             const Environment,
-             llvm::SmallVector<UncheckedOptionalAccessDiagnostic>>()
-      // optional::value
-      .CaseOfCFGStmt<CXXMemberCallExpr>(
-          valueCall(IgnorableOptional),
-          [](const CXXMemberCallExpr *E, const MatchFinder::MatchResult &,
-             const Environment &Env) {
-            return diagnoseUnwrapCall(E->getImplicitObjectArgument(), Env);
-          })
+  const auto IgnorableOptional = ignorableOptional(Options);
+
+  auto Builder = CFGMatchSwitchBuilder<
+                     const Environment,
+                     llvm::SmallVector<UncheckedOptionalAccessDiagnostic>>()
+                     .CaseOfCFGStmt<CallExpr>(
+                         valueOperatorCall(IgnorableOptional),
+                         [](const CallExpr *E, const MatchFinder::MatchResult &,
+                            const Environment &Env) {
+                           return diagnoseUnwrapCall(E->getArg(0), Env);
+                         });
+
+  if (!Options.IgnoreValueCalls) {
+    return std::move(Builder)
----------------
vbvictor wrote:

Could we not return here but only add one more `CaseOfCFGStmt` and return only once at the end of the function?
The rationale is if we are to add completely new `CaseOfCFGStmt` to builder, then we would need to modify both returns or make more transformations.
Be better leave code in flexible way to change in the future

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


More information about the cfe-commits mailing list