[clang] [Clang][NFC] Change uses of getAs() to castAs() where the target type is assured. (PR #130188)

Tom Honermann via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 6 14:22:13 PST 2025


https://github.com/tahonermann created https://github.com/llvm/llvm-project/pull/130188

Static analysis identified two uses of `getAs()` for which the result pointer was unconditionally dereferenced. Source code inspection confirmed that the target type is assured by prior checks. This change replaces these uses of `getAs()` with `castAs()`.

The first case, in `clang/lib/CodeGen/Targets/AArch64.cpp`, performs a cast to `BuiltinType` following a check for `isBuiltinType()`.

The second case, in `clang/lib/Sema/SemaTemplateVariadic.cpp`, performs a cast to `PackExpansionType` on the result of a call to `getAsType()` on an object of type `TemplateArgument` following confirmation that `isPackExpansion()` returned true and that `getKind()` returned `TemplateArgument::Type`. Inspection of `isPackExpansion()` revealed that it only returns true when the template argument kind is `TemplateArgument::Type` if `isa<PackExpansionType>(getAsType())` is true.

>From ffb1b55a12a499c294b5e550065718d25eece575 Mon Sep 17 00:00:00 2001
From: Tom Honermann <tom.honermann at intel.com>
Date: Thu, 6 Mar 2025 14:05:08 -0800
Subject: [PATCH] [Clang][NFC] Change uses of getAs() to castAs() where the
 target type is assured.

Static analysis identified two uses of getAs() for which the result pointer
was unconditionally dereferenced. Source code inspection confirmed that the
target type is assured by prior checks. This change replaces these uses of
getAs() with castAs().

The first case, in clang/lib/CodeGen/Targets/AArch64.cpp, performs a cast
to BuiltinType following a check for isBuiltinType().

The second case, in clang/lib/Sema/SemaTemplateVariadic.cpp, performs a cast
to PackExpansionType on the result of a call to getAsType() on an object of
type TemplateArgument following confirmation that isPackExpansion() returned
true and that getKind() returned TemplateArgument::Type. Inspection of
isPackExpansion() revealed that it only returns true when the template argument
kind is TemplateArgument::Type if isa<PackExpansionType>(getAsType()) is true.
---
 clang/lib/CodeGen/Targets/AArch64.cpp   | 2 +-
 clang/lib/Sema/SemaTemplateVariadic.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index d6e0e720a0941..073ca3cc82690 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -763,7 +763,7 @@ bool AArch64ABIInfo::passAsPureScalableType(
     return false;
 
   bool isPredicate;
-  switch (Ty->getAs<BuiltinType>()->getKind()) {
+  switch (Ty->castAs<BuiltinType>()->getKind()) {
 #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId)                    \
   case BuiltinType::Id:                                                        \
     isPredicate = false;                                                       \
diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index fad00f7648848..d9256dbd07d7a 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -834,7 +834,7 @@ bool Sema::CheckParameterPacksForExpansion(
 
             if (TA.getKind() == TemplateArgument::Type)
               return !TA.getAsType()
-                          ->getAs<PackExpansionType>()
+                          ->castAs<PackExpansionType>()
                           ->getNumExpansions();
 
             if (TA.getKind() == TemplateArgument::Expression)



More information about the cfe-commits mailing list