Diagnose an unused result from a call through a function pointer whose return type is marked [[nodiscard]].
When a function returns a type and that type was declared [[nodiscard]], we diagnose any unused results from that call as though the function were marked nodiscard. The same behavior should apply to calls through a function pointer.
This addresses PR31526.
llvm-svn: 350317
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 47130cf..dacf8d0 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -259,17 +259,16 @@
if (E->getType()->isVoidType())
return;
+ if (const Attr *A = CE->getUnusedResultAttr(Context)) {
+ Diag(Loc, diag::warn_unused_result) << A << R1 << R2;
+ return;
+ }
+
// If the callee has attribute pure, const, or warn_unused_result, warn with
// a more specific message to make it clear what is happening. If the call
// is written in a macro body, only warn if it has the warn_unused_result
// attribute.
if (const Decl *FD = CE->getCalleeDecl()) {
- if (const Attr *A = isa<FunctionDecl>(FD)
- ? cast<FunctionDecl>(FD)->getUnusedResultAttr()
- : FD->getAttr<WarnUnusedResultAttr>()) {
- Diag(Loc, diag::warn_unused_result) << A << R1 << R2;
- return;
- }
if (ShouldSuppress)
return;
if (FD->hasAttr<PureAttr>()) {