Skip to content

Commit 42e5214

Browse files
committed
[clang][Sema][NFC] Clean up ShouldDiagnoseUnusedDecl
Const qualify a few locals, merge two if statements and use dyn_cast_if_present instead of _or_null.
1 parent 258c2ae commit 42e5214

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,12 +2005,12 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
20052005
if (D->isInvalidDecl())
20062006
return false;
20072007

2008-
if (auto *DD = dyn_cast<DecompositionDecl>(D)) {
2008+
if (const auto *DD = dyn_cast<DecompositionDecl>(D)) {
20092009
// For a decomposition declaration, warn if none of the bindings are
20102010
// referenced, instead of if the variable itself is referenced (which
20112011
// it is, by the bindings' expressions).
20122012
bool IsAllPlaceholders = true;
2013-
for (auto *BD : DD->bindings()) {
2013+
for (const auto *BD : DD->bindings()) {
20142014
if (BD->isReferenced())
20152015
return false;
20162016
IsAllPlaceholders = IsAllPlaceholders && BD->isPlaceholderVar(LangOpts);
@@ -2054,7 +2054,7 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
20542054
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
20552055

20562056
const Expr *Init = VD->getInit();
2057-
if (const auto *Cleanups = dyn_cast_or_null<ExprWithCleanups>(Init))
2057+
if (const auto *Cleanups = dyn_cast_if_present<ExprWithCleanups>(Init))
20582058
Init = Cleanups->getSubExpr();
20592059

20602060
const auto *Ty = VD->getType().getTypePtr();
@@ -2068,11 +2068,10 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
20682068

20692069
// Warn for reference variables whose initializtion performs lifetime
20702070
// extension.
2071-
if (const auto *MTE = dyn_cast_or_null<MaterializeTemporaryExpr>(Init)) {
2072-
if (MTE->getExtendingDecl()) {
2073-
Ty = VD->getType().getNonReferenceType().getTypePtr();
2074-
Init = MTE->getSubExpr()->IgnoreImplicitAsWritten();
2075-
}
2071+
if (const auto *MTE = dyn_cast_if_present<MaterializeTemporaryExpr>(Init);
2072+
MTE && MTE->getExtendingDecl()) {
2073+
Ty = VD->getType().getNonReferenceType().getTypePtr();
2074+
Init = MTE->getSubExpr()->IgnoreImplicitAsWritten();
20762075
}
20772076

20782077
// If we failed to complete the type for some reason, or if the type is
@@ -2089,15 +2088,14 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts,
20892088
if (Tag->hasAttr<UnusedAttr>())
20902089
return false;
20912090

2092-
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) {
2091+
if (const auto *RD = dyn_cast<CXXRecordDecl>(Tag)) {
20932092
if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>())
20942093
return false;
20952094

20962095
if (Init) {
2097-
const CXXConstructExpr *Construct =
2098-
dyn_cast<CXXConstructExpr>(Init);
2096+
const auto *Construct = dyn_cast<CXXConstructExpr>(Init);
20992097
if (Construct && !Construct->isElidable()) {
2100-
CXXConstructorDecl *CD = Construct->getConstructor();
2098+
const CXXConstructorDecl *CD = Construct->getConstructor();
21012099
if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>() &&
21022100
(VD->getInit()->isValueDependent() || !VD->evaluateValue()))
21032101
return false;

0 commit comments

Comments
 (0)