Fix "pointer is null" static analyzer warnings. NFCI.

Use castAs<> instead of getAs<> since the pointer is dereferenced immediately in all cases and castAs will perform the null assertion for us.
This commit is contained in:
Simon Pilgrim 2020-01-14 16:30:05 +00:00
parent ab9dbc1d12
commit cfd366ba74
1 changed files with 17 additions and 20 deletions

View File

@ -1501,13 +1501,13 @@ void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) {
// as pointers to member functions. // as pointers to member functions.
if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { if (const ReferenceType *R = NewType->getAs<ReferenceType>()) {
NewType = R->getPointeeType(); NewType = R->getPointeeType();
OldType = OldType->getAs<ReferenceType>()->getPointeeType(); OldType = OldType->castAs<ReferenceType>()->getPointeeType();
} else if (const PointerType *P = NewType->getAs<PointerType>()) { } else if (const PointerType *P = NewType->getAs<PointerType>()) {
NewType = P->getPointeeType(); NewType = P->getPointeeType();
OldType = OldType->getAs<PointerType>()->getPointeeType(); OldType = OldType->castAs<PointerType>()->getPointeeType();
} else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) {
NewType = M->getPointeeType(); NewType = M->getPointeeType();
OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); OldType = OldType->castAs<MemberPointerType>()->getPointeeType();
} }
if (!NewType->isFunctionProtoType()) if (!NewType->isFunctionProtoType())
@ -1633,7 +1633,7 @@ static bool CheckConstexprParameterTypes(Sema &SemaRef,
const FunctionDecl *FD, const FunctionDecl *FD,
Sema::CheckConstexprKind Kind) { Sema::CheckConstexprKind Kind) {
unsigned ArgIndex = 0; unsigned ArgIndex = 0;
const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); const auto *FT = FD->getType()->castAs<FunctionProtoType>();
for (FunctionProtoType::param_type_iterator i = FT->param_type_begin(), for (FunctionProtoType::param_type_iterator i = FT->param_type_begin(),
e = FT->param_type_end(); e = FT->param_type_end();
i != e; ++i, ++ArgIndex) { i != e; ++i, ++ArgIndex) {
@ -9829,7 +9829,7 @@ QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R,
// Rebuild the function type "R" without any type qualifiers (in // Rebuild the function type "R" without any type qualifiers (in
// case any of the errors above fired) and with "void" as the // case any of the errors above fired) and with "void" as the
// return type, since constructors don't have return types. // return type, since constructors don't have return types.
const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); const FunctionProtoType *Proto = R->castAs<FunctionProtoType>();
if (Proto->getReturnType() == Context.VoidTy && !D.isInvalidType()) if (Proto->getReturnType() == Context.VoidTy && !D.isInvalidType())
return R; return R;
@ -10027,7 +10027,7 @@ QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R,
if (!D.isInvalidType()) if (!D.isInvalidType())
return R; return R;
const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); const FunctionProtoType *Proto = R->castAs<FunctionProtoType>();
FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
EPI.Variadic = false; EPI.Variadic = false;
EPI.TypeQuals = Qualifiers(); EPI.TypeQuals = Qualifiers();
@ -10101,7 +10101,7 @@ void Sema::CheckConversionDeclarator(Declarator &D, QualType &R,
D.setInvalidType(); D.setInvalidType();
} }
const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); const auto *Proto = R->castAs<FunctionProtoType>();
// Make sure we don't have any parameters. // Make sure we don't have any parameters.
if (Proto->getNumParams() > 0) { if (Proto->getNumParams() > 0) {
@ -13015,8 +13015,7 @@ void Sema::AdjustDestructorExceptionSpec(CXXDestructorDecl *Destructor) {
// A declaration of a destructor that does not have an exception- // A declaration of a destructor that does not have an exception-
// specification is implicitly considered to have the same exception- // specification is implicitly considered to have the same exception-
// specification as an implicit declaration. // specification as an implicit declaration.
const FunctionProtoType *DtorType = Destructor->getType()-> const auto *DtorType = Destructor->getType()->castAs<FunctionProtoType>();
getAs<FunctionProtoType>();
if (DtorType->hasExceptionSpec()) if (DtorType->hasExceptionSpec())
return; return;
@ -13996,8 +13995,8 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
// The parameter for the "other" object, which we are move from. // The parameter for the "other" object, which we are move from.
ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0);
QualType OtherRefType = Other->getType()-> QualType OtherRefType =
getAs<RValueReferenceType>()->getPointeeType(); Other->getType()->castAs<RValueReferenceType>()->getPointeeType();
// Our location for everything implicitly-generated. // Our location for everything implicitly-generated.
SourceLocation Loc = MoveAssignOperator->getEndLoc().isValid() SourceLocation Loc = MoveAssignOperator->getEndLoc().isValid()
@ -14791,9 +14790,7 @@ Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor,
unsigned NumArgs = ArgsPtr.size(); unsigned NumArgs = ArgsPtr.size();
Expr **Args = ArgsPtr.data(); Expr **Args = ArgsPtr.data();
const FunctionProtoType *Proto const auto *Proto = Constructor->getType()->castAs<FunctionProtoType>();
= Constructor->getType()->getAs<FunctionProtoType>();
assert(Proto && "Constructor without a prototype?");
unsigned NumParams = Proto->getNumParams(); unsigned NumParams = Proto->getNumParams();
// If too few arguments are available, we'll fill in the rest with defaults. // If too few arguments are available, we'll fill in the rest with defaults.
@ -14856,7 +14853,7 @@ CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl,
unsigned DependentParamTypeDiag, unsigned DependentParamTypeDiag,
unsigned InvalidParamTypeDiag) { unsigned InvalidParamTypeDiag) {
QualType ResultType = QualType ResultType =
FnDecl->getType()->getAs<FunctionType>()->getReturnType(); FnDecl->getType()->castAs<FunctionType>()->getReturnType();
// Check that the result type is not dependent. // Check that the result type is not dependent.
if (ResultType->isDependentType()) if (ResultType->isDependentType())
@ -15085,7 +15082,7 @@ bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) {
// Overloaded operators other than operator() cannot be variadic. // Overloaded operators other than operator() cannot be variadic.
if (Op != OO_Call && if (Op != OO_Call &&
FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { FnDecl->getType()->castAs<FunctionProtoType>()->isVariadic()) {
return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic)
<< FnDecl->getDeclName(); << FnDecl->getDeclName();
} }
@ -16458,8 +16455,8 @@ void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) {
bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New,
const CXXMethodDecl *Old) { const CXXMethodDecl *Old) {
const auto *NewFT = New->getType()->getAs<FunctionProtoType>(); const auto *NewFT = New->getType()->castAs<FunctionProtoType>();
const auto *OldFT = Old->getType()->getAs<FunctionProtoType>(); const auto *OldFT = Old->getType()->castAs<FunctionProtoType>();
if (OldFT->hasExtParameterInfos()) { if (OldFT->hasExtParameterInfos()) {
for (unsigned I = 0, E = OldFT->getNumParams(); I != E; ++I) for (unsigned I = 0, E = OldFT->getNumParams(); I != E; ++I)
@ -16506,8 +16503,8 @@ bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New,
bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New,
const CXXMethodDecl *Old) { const CXXMethodDecl *Old) {
QualType NewTy = New->getType()->getAs<FunctionType>()->getReturnType(); QualType NewTy = New->getType()->castAs<FunctionType>()->getReturnType();
QualType OldTy = Old->getType()->getAs<FunctionType>()->getReturnType(); QualType OldTy = Old->getType()->castAs<FunctionType>()->getReturnType();
if (Context.hasSameType(NewTy, OldTy) || if (Context.hasSameType(NewTy, OldTy) ||
NewTy->isDependentType() || OldTy->isDependentType()) NewTy->isDependentType() || OldTy->isDependentType())