[clang] [clang] [Sema] Implement CXXConstructExpr::getUnusedResultAttr (PR #152950)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 10 18:21:20 PDT 2025
https://github.com/halbi2 created https://github.com/llvm/llvm-project/pull/152950
This continues my patch series started as #142541 where multiple kinds of Expr all use the same getUnusedResultAttrImpl.
The test suite indicates there is no change in behavior happening here. @Sirraide could you help me commit this?
>From 7b7f1a87acda00ffed70cb545e1cd06bb2d96b53 Mon Sep 17 00:00:00 2001
From: halbi2 <hehiralbi at gmail.com>
Date: Sun, 10 Aug 2025 21:17:08 -0400
Subject: [PATCH] [clang] [Sema] Implement
CXXConstructExpr::getUnusedResultAttr
---
clang/include/clang/AST/ExprCXX.h | 13 +++++++++++++
clang/lib/Sema/SemaStmt.cpp | 15 ++++-----------
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h
index 5508890f12b58..9fedb230ce397 100644
--- a/clang/include/clang/AST/ExprCXX.h
+++ b/clang/include/clang/AST/ExprCXX.h
@@ -1712,6 +1712,19 @@ class CXXConstructExpr : public Expr {
CXXConstructExprBits.IsImmediateEscalating = Set;
}
+ /// Returns the WarnUnusedResultAttr that is declared on the callee
+ /// or its return type declaration, together with a NamedDecl that
+ /// refers to the declaration the attribute is attached to.
+ std::pair<const NamedDecl *, const WarnUnusedResultAttr *>
+ getUnusedResultAttr(const ASTContext &Ctx) const {
+ return getUnusedResultAttrImpl(getConstructor(), getType());
+ }
+
+ /// Returns true if this call expression should warn on unused results.
+ bool hasUnusedResultAttr(const ASTContext &Ctx) const {
+ return getUnusedResultAttr(Ctx).second != nullptr;
+ }
+
SourceLocation getBeginLoc() const LLVM_READONLY;
SourceLocation getEndLoc() const LLVM_READONLY;
SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index efc0b35792613..bc1ddb80961a2 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -316,17 +316,10 @@ void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) {
}
}
} else if (const auto *CE = dyn_cast<CXXConstructExpr>(E)) {
- if (const CXXConstructorDecl *Ctor = CE->getConstructor()) {
- const NamedDecl *OffendingDecl = nullptr;
- const auto *A = Ctor->getAttr<WarnUnusedResultAttr>();
- if (!A) {
- OffendingDecl = Ctor->getParent();
- A = OffendingDecl->getAttr<WarnUnusedResultAttr>();
- }
- if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
- /*isCtor=*/true))
- return;
- }
+ auto [OffendingDecl, A] = CE->getUnusedResultAttr(S.Context);
+ if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
+ /*isCtor=*/true))
+ return;
} else if (const auto *ILE = dyn_cast<InitListExpr>(E)) {
if (const TagDecl *TD = ILE->getType()->getAsTagDecl()) {
More information about the cfe-commits
mailing list