https://github.com/dillona created https://github.com/llvm/llvm-project/pull/173448
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 >From fb64cb3fa16e5667dfd3ef00c20a606534e8e326 Mon Sep 17 00:00:00 2001 From: Dillon Amburgey <[email protected]> Date: Wed, 24 Dec 2025 03:28:32 +0000 Subject: [PATCH] [clang-tidy] Skip ObjC fast-enum loop vars in init-variables 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 --- .../cppcoreguidelines/InitVariablesCheck.cpp | 3 ++- .../cppcoreguidelines/init-variables-objc.m | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objc.m 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 + +@interface NSObject +@end + +@interface NSNumber : NSObject +@end + +@protocol NSFastEnumeration +@end + +@interface NSArray<ObjectType> : NSObject <NSFastEnumeration> +@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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
