https://github.com/sstepashka updated https://github.com/llvm/llvm-project/pull/191306
>From b8bbc31e34f77513c1fdd209e7db37e415372f95 Mon Sep 17 00:00:00 2001 From: Dmitrii Kuragin <[email protected]> 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 | 31 +++++++++++++++++++ 3 files changed, 40 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 caf0275035064..f8253dda59d6d 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -303,6 +303,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..ea61f7933c061 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm @@ -0,0 +1,31 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables %t -- -- -fno-objc-arc + +@interface NSObject +@end + +@interface NSString : NSObject +@end + +@interface NSURL : NSObject +- (NSString *)absoluteString; +@end + +@interface NSArray : NSObject +@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-FIXES: NSString *str = nullptr; + NSString *str; + (void)str; + } +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
