https://github.com/sstepashka updated https://github.com/llvm/llvm-project/pull/191306
>From 2316a925c9d09b78d07b8e652805f34e26181675 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 +++ .../init-variables-objcxx.mm | 38 +++++++++++++++++++ 2 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/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..bb83e90a82f8d --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm @@ -0,0 +1,38 @@ +// 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 + +// ObjC for-in loop variables should not be flagged, as they are initialized +// by the loop itself. Previously, the check would incorrectly try to insert +// '= nullptr' at the wrong source location, corrupting the following statement. +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]; + // 'urlString' should NOT be flagged - it has an initializer. + (void)urlString; + } +} + +// Uninitialized pointer variables inside a for-in loop body should still be +// flagged. +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
