[clang-tools-extra] [clang-tidy] Filter out googletest TUs in bugprone-unchecked-optional-access (PR #115051)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 6 17:45:25 PST 2024


https://github.com/HerrCai0907 requested changes to this pull request.

It would be better to use `GtestCmp` if you really want clang-tidy support gtest in this check.
Here is an unfinished example which can match
```int v = 10;
TEST(a, b) {
  std::optional<int> a;
  if (v) {
    a = 1;
  }
  ASSERT_NE(a, std::nullopt);
  a.value();
}
```
```c++
  using namespace ast_matchers;
  auto HasOptionalCallDescendant = hasDescendant(callExpr(callee(cxxMethodDecl(
      ofClass(UncheckedOptionalAccessModel::optionalClassDecl())))));
  Finder->addMatcher(
      decl(anyOf(functionDecl(
                     unless(isExpansionInSystemHeader()),
                     // FIXME: Remove the filter below when lambdas are
                     // well supported by the check.
                     unless(hasDeclContext(cxxRecordDecl(isLambda()))),
                     hasBody(allOf(
                         HasOptionalCallDescendant,
                         unless(hasDescendant(gtestAssert(
                             GtestCmp::Ne,
                             expr(hasType(qualType(hasUnqualifiedDesugaredType(
                                 recordType(hasDeclaration(
                                     UncheckedOptionalAccessModel::
                                         optionalClassDecl())))))),
                             anything())))))),
                 cxxConstructorDecl(hasAnyConstructorInitializer(
                     withInitializer(HasOptionalCallDescendant)))))
          .bind(FuncID),
      this);
```

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


More information about the cfe-commits mailing list