Skip to content

Conversation

@vitalybuka
Copy link
Collaborator

@vitalybuka vitalybuka commented Nov 5, 2025

The loop iterates once and returns the first element.
Replace it with "if !empty()" to make it more explicit.

Follow up to #158193.

Created using spr 1.3.7
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Nov 5, 2025
@vitalybuka vitalybuka requested a review from shafik November 5, 2025 07:20
@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2025

@llvm/pr-subscribers-clang

Author: Vitaly Buka (vitalybuka)

Changes

The loop iterates once and returns the first element.
Replace it with "if !empty()" to make it more explicit.


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

1 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+2-3)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 0fea57b2e1799..98d59b79ab881 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2368,9 +2368,8 @@ static QualType GeneralizeTransparentUnion(QualType Ty) {
   const RecordDecl *UD = UT->getDecl()->getDefinitionOrSelf();
   if (!UD->hasAttr<TransparentUnionAttr>())
     return Ty;
-  for (const auto *it : UD->fields()) {
-    return it->getType();
-  }
+  if (!UD->fields().empty())
+    return UD->fields().begin()->getType();
   return Ty;
 }
 

@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2025

@llvm/pr-subscribers-clang-codegen

Author: Vitaly Buka (vitalybuka)

Changes

The loop iterates once and returns the first element.
Replace it with "if !empty()" to make it more explicit.


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

1 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+2-3)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 0fea57b2e1799..98d59b79ab881 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2368,9 +2368,8 @@ static QualType GeneralizeTransparentUnion(QualType Ty) {
   const RecordDecl *UD = UT->getDecl()->getDefinitionOrSelf();
   if (!UD->hasAttr<TransparentUnionAttr>())
     return Ty;
-  for (const auto *it : UD->fields()) {
-    return it->getType();
-  }
+  if (!UD->fields().empty())
+    return UD->fields().begin()->getType();
   return Ty;
 }
 

@vitalybuka vitalybuka changed the title [CodeGen] Replace loop with "if !empty()" [NFC][CodeGen] Replace loop with "if !empty()" Nov 5, 2025
@vitalybuka vitalybuka merged commit 77a194c into main Nov 8, 2025
13 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/codegen-replace-loop-with-if-empty branch November 8, 2025 01:17
vinay-deshmukh pushed a commit to vinay-deshmukh/llvm-project that referenced this pull request Nov 8, 2025
The loop iterates once and returns the first element.
Replace it with "if !empty()" to make it more explicit.

Follow up to llvm#158193.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants