llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Dmitrii Kuragin (sstepashka)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) 
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp (+5) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-objcxx.mm
 (+38) 


``````````diff
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;
+  }
+}

``````````

</details>


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

Reply via email to