Skip to content

Commit 767ab81

Browse files
authored
[NFC] [Clang] [Sema] Implement CXXConstructExpr::getUnusedResultAttr (llvm#152950)
This continues my patch series started as llvm#142541 where multiple kinds of Expr all use the same getUnusedResultAttrImpl. The test suite indicates there is no change in behavior happening here.
1 parent aba0ce1 commit 767ab81

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

clang/include/clang/AST/ExprCXX.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,19 @@ class CXXConstructExpr : public Expr {
17121712
CXXConstructExprBits.IsImmediateEscalating = Set;
17131713
}
17141714

1715+
/// Returns the WarnUnusedResultAttr that is declared on the callee
1716+
/// or its return type declaration, together with a NamedDecl that
1717+
/// refers to the declaration the attribute is attached to.
1718+
std::pair<const NamedDecl *, const WarnUnusedResultAttr *>
1719+
getUnusedResultAttr(const ASTContext &Ctx) const {
1720+
return getUnusedResultAttrImpl(getConstructor(), getType());
1721+
}
1722+
1723+
/// Returns true if this call expression should warn on unused results.
1724+
bool hasUnusedResultAttr(const ASTContext &Ctx) const {
1725+
return getUnusedResultAttr(Ctx).second != nullptr;
1726+
}
1727+
17151728
SourceLocation getBeginLoc() const LLVM_READONLY;
17161729
SourceLocation getEndLoc() const LLVM_READONLY;
17171730
SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }

clang/lib/Sema/SemaStmt.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,10 @@ void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) {
316316
}
317317
}
318318
} else if (const auto *CE = dyn_cast<CXXConstructExpr>(E)) {
319-
if (const CXXConstructorDecl *Ctor = CE->getConstructor()) {
320-
const NamedDecl *OffendingDecl = nullptr;
321-
const auto *A = Ctor->getAttr<WarnUnusedResultAttr>();
322-
if (!A) {
323-
OffendingDecl = Ctor->getParent();
324-
A = OffendingDecl->getAttr<WarnUnusedResultAttr>();
325-
}
326-
if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
327-
/*isCtor=*/true))
328-
return;
329-
}
319+
auto [OffendingDecl, A] = CE->getUnusedResultAttr(S.Context);
320+
if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
321+
/*isCtor=*/true))
322+
return;
330323
} else if (const auto *ILE = dyn_cast<InitListExpr>(E)) {
331324
if (const TagDecl *TD = ILE->getType()->getAsTagDecl()) {
332325

0 commit comments

Comments
 (0)