https://github.com/dillona updated 
https://github.com/llvm/llvm-project/pull/173448

>From 97d556228875f2f995a699da139c184944d6ab6c Mon Sep 17 00:00:00 2001
From: Dillon Amburgey <[email protected]>
Date: Wed, 24 Dec 2025 03:28:32 +0000
Subject: [PATCH] [clang-tidy] Skip ObjC fast-enum loop vars in init-variables

Avoid cppcoreguidelines-init-variables warnings on Objective-C fast enumeration 
loop variables by excluding ObjC for-in declarations. Add a regression test 
covering the GitHub report.

Fixes: https://github.com/llvm/llvm-project/issues/173435
---
 .../cppcoreguidelines/InitVariablesCheck.cpp  |  3 ++-
 clang-tools-extra/docs/ReleaseNotes.rst       |  4 ++++
 .../cppcoreguidelines/init-variables-objc.m   | 21 +++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objc.m

diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index 93b5b96926865..ed1fa10653b85 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -21,6 +21,7 @@ namespace clang::tidy::cppcoreguidelines {
 
 namespace {
 AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isObjCForDecl) { return Node.isObjCForDecl(); }
 } // namespace
 
 InitVariablesCheck::InitVariablesCheck(StringRef Name,
@@ -41,7 +42,7 @@ void InitVariablesCheck::registerMatchers(MatchFinder 
*Finder) {
   Finder->addMatcher(
       varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
               isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
-              unless(hasParent(cxxCatchStmt())),
+              unless(isObjCForDecl()), unless(hasParent(cxxCatchStmt())),
               optionally(hasParent(declStmt(hasParent(
                   
cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
               unless(equalsBoundNode(BadDecl)))
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index c4ba060a45a0f..07e1d4a5139cd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -463,6 +463,10 @@ Changes in existing checks
   <clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
   insertion location for function pointers with multiple parameters.
 
+- Improved :doc:`cppcoreguidelines-init-variables
+  <clang-tidy/checks/cppcoreguidelines/init-variables>` check to avoid false
+  positives on Objective-C fast enumeration loop variables.
+
 - Improved :doc:`cppcoreguidelines-macro-usage
   <clang-tidy/checks/cppcoreguidelines/macro-usage>` check by excluding macro
   bodies that starts with ``__attribute__((..))`` keyword.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objc.m
 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objc.m
new file mode 100644
index 0000000000000..440580651bec5
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objc.m
@@ -0,0 +1,21 @@
+// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables %t -- -- 
-fobjc-arc
+
+@interface NSObject
+@end
+
+@interface NSNumber : NSObject
+@end
+
+@protocol NSFastEnumeration
+@end
+
+@interface NSArray<ObjectType> : NSObject <NSFastEnumeration>
+@end
+
+void testFastEnumeration(NSArray<NSNumber *> *array) {
+  for (NSNumber *n in array) {
+  }
+}
+
+// Regression test for https://github.com/llvm/llvm-project/issues/173435.
+// CHECK-MESSAGES-NOT: warning: variable 'n' is not initialized

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to