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

Jan Voung via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 8 13:20:39 PST 2025


================
@@ -40,11 +40,30 @@ void UncheckedOptionalAccessCheck::registerMatchers(MatchFinder *Finder) {
       this);
 }
 
+void UncheckedOptionalAccessCheck::onStartOfTranslationUnit() {
+  // Reset the flag for each TU.
+  is_test_tu_ = false;
+}
+
 void UncheckedOptionalAccessCheck::check(
     const MatchFinder::MatchResult &Result) {
-  if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
+  // The googletest assertion macros are not currently recognized, so we have
+  // many false positives in tests. So, do not check functions in a test TU
+  // if the option ignore_test_tus_ is set.
+  if ((ignore_test_tus_ && is_test_tu_) ||
+      Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
----------------
jvoung wrote:

I don't think we'd have access to `Result.Context->Idents` yet, at onStartOfTranslationUnit, to compute IsTestTu ?

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


More information about the cfe-commits mailing list