[clang] [clang] [Sema] Implement CXXConstructExpr::getUnusedResultAttr (PR #152950)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 10 18:21:52 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (halbi2)
<details>
<summary>Changes</summary>
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?
---
Full diff: https://github.com/llvm/llvm-project/pull/152950.diff
2 Files Affected:
- (modified) clang/include/clang/AST/ExprCXX.h (+13)
- (modified) clang/lib/Sema/SemaStmt.cpp (+4-11)
``````````diff
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()) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/152950
More information about the cfe-commits
mailing list