[clang-tools-extra] [clang-tidy] Skip ObjC fast-enum loop vars in init-variables (PR #173448)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 23 20:34:22 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Dillon Amburgey (dillona)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/173448.diff


2 Files Affected:

- (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp (+2-1) 
- (added) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objc.m (+21) 


``````````diff
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/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
+
+ at interface NSObject
+ at end
+
+ at interface NSNumber : NSObject
+ at end
+
+ at protocol NSFastEnumeration
+ at end
+
+ at interface NSArray<ObjectType> : NSObject <NSFastEnumeration>
+ at 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

``````````

</details>


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


More information about the cfe-commits mailing list