[clang-tools-extra] 546cc69 - [Clang-Tidy] Fixed `cppcoreguidelines-init-variables` to handle ObjC for-in loops. (#191306)

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 17 10:00:33 PDT 2026


Author: Dmitrii Kuragin
Date: 2026-04-17T10:00:29-07:00
New Revision: 546cc690020e4e4317cd05ea833d5e6ff00ec161

URL: https://github.com/llvm/llvm-project/commit/546cc690020e4e4317cd05ea833d5e6ff00ec161
DIFF: https://github.com/llvm/llvm-project/commit/546cc690020e4e4317cd05ea833d5e6ff00ec161.diff

LOG: [Clang-Tidy] Fixed `cppcoreguidelines-init-variables` to handle ObjC for-in loops. (#191306)

The check used to report false positive in case of for-in loop in
Objective-C[++]:
```
for (NSString *value in values) {
   ...
}
```
With the report message:
```
...: warning: variable 'value' is not initialized [cppcoreguidelines-init-variables]
for (NSString *value in values) {
               ^
                     = NULL
```

This PR exclude the for-in loop from the the matcher in order to avoid
the false-positive.

Fixes #62106

Added: 
    clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index 402ee9efcbc04..cf3e97ad500ab 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -10,6 +10,7 @@
 
 #include "../utils/LexerUtils.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/StmtObjC.h"
 #include "clang/AST/Type.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Lex/Preprocessor.h"
@@ -21,6 +22,9 @@ namespace clang::tidy::cppcoreguidelines {
 
 namespace {
 AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(Stmt, isObjCForCollectionStmt) {
+  return isa<ObjCForCollectionStmt>(&Node);
+}
 } // namespace
 
 InitVariablesCheck::InitVariablesCheck(StringRef Name,
@@ -42,6 +46,7 @@ void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
       varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
               isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
               unless(hasParent(cxxCatchStmt())),
+              unless(hasParent(declStmt(hasParent(isObjCForCollectionStmt())))),
               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 cc1c6bae65e2b..329d0a42aef7a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -328,6 +328,10 @@ Changes in existing checks
   <clang-tidy/checks/cppcoreguidelines/init-variables>` check by ensuring that
   member pointers are correctly flagged as uninitialized.
 
+- Fixed :doc:`cppcoreguidelines-init-variables
+  <clang-tidy/checks/cppcoreguidelines/init-variables>` check by excluding
+  Objective-C for-in loop variable declaration.
+
 - Improved :doc:`cppcoreguidelines-missing-std-forward
   <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check:
   

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm
new file mode 100644
index 0000000000000..86f2b4089aeb7
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm
@@ -0,0 +1,34 @@
+// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables %t -- -- -fno-objc-arc
+
+ at interface NSObject
+ at end
+
+ at interface NSString : NSObject
+ at end
+
+ at interface NSURL : NSObject
+- (NSString *)absoluteString;
+ at end
+
+ at interface NSArray : NSObject
+ at end
+
+void objc_for_in_no_false_positive(NSArray *urls) {
+  for (NSURL *url in urls) {
+    {
+      // 'url' should NOT be flagged - it is a for-in loop variable.
+      NSString *urlString = [url absoluteString];
+    }
+  }
+}
+
+void objc_for_in_body_uninit(NSArray *urls) {
+  for (NSURL *url in urls) {
+    (void)url;
+
+    // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: variable 'str' is not initialized [cppcoreguidelines-init-variables]
+    // CHECK-FIXES: NSString *str = nullptr;
+    NSString *str;
+    (void)str;
+  }
+}


        


More information about the cfe-commits mailing list