[clang-tools-extra] [Clang-Tidy] Fixed `cppcoreguidelines-init-variables` to handle ObjC for-in loops. (PR #191306)
Dmitrii Kuragin via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 16 13:55:51 PDT 2026
https://github.com/sstepashka updated https://github.com/llvm/llvm-project/pull/191306
>From 7fee96a6cdd49fcfe338bac398b277445ef1ba48 Mon Sep 17 00:00:00 2001
From: Dmitrii Kuragin <dkuragin at adobe.com>
Date: Thu, 9 Apr 2026 14:28:10 -0700
Subject: [PATCH] [Clang-Tidy] Fixed `cppcoreguidelines-init-variables` to
handle ObjC for-in loops.
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.
---
.../cppcoreguidelines/InitVariablesCheck.cpp | 5 +++
clang-tools-extra/docs/ReleaseNotes.rst | 4 +++
.../init-variables-objcxx.mm | 34 +++++++++++++++++++
3 files changed, 43 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm
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 6979c2cbcfff2..799f7a3edebdc 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -313,6 +313,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