https://github.com/dillona updated https://github.com/llvm/llvm-project/pull/173448
>From 00b62cca9e70d1b515e7e95799ca90c7f48c4bf8 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 ++- clang-tools-extra/docs/ReleaseNotes.rst | 3 +++ .../cppcoreguidelines/init-variables-objc.m | 21 +++++++++++++++++++ 3 files changed, 26 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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index c4ba060a45a0f..752735bad9ba4 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -462,6 +462,9 @@ Changes in existing checks - Improved :doc:`cppcoreguidelines-init-variables <clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the insertion location for function pointers with multiple parameters. +- Improved :doc:`cppcoreguidelines-init-variables + <clang-tidy/checks/cppcoreguidelines/init-variables>` check to avoid false + positives on Objective-C fast enumeration loop variables. - Improved :doc:`cppcoreguidelines-macro-usage <clang-tidy/checks/cppcoreguidelines/macro-usage>` check by excluding macro 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
