llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Dillon Amburgey (dillona)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/173448.diff


2 Files Affected:

- (modified) 
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp (+2-1) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objc.m
 (+21) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/173448
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to