Skip to content

Commit 23c5b44

Browse files
committed
[clang] [Sema] Enable nodiscard warnings for function pointers
A call through a function pointer has no associated FunctionDecl, but it still might have a nodiscard return type. Ensure there is a codepath to emit the nodiscard warning in this case. Fixes #142453
1 parent 1efa70c commit 23c5b44

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

clang/lib/AST/Expr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,16 @@ bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
27942794
return true;
27952795
}
27962796
}
2797+
if (CE->hasUnusedResultAttr(Ctx)) {
2798+
WarnE = this;
2799+
Loc = getBeginLoc();
2800+
R1 = getSourceRange();
2801+
2802+
if (unsigned NumArgs = CE->getNumArgs())
2803+
R2 = SourceRange(CE->getArg(0)->getBeginLoc(),
2804+
CE->getArg(NumArgs - 1)->getEndLoc());
2805+
return true;
2806+
}
27972807
return false;
27982808
}
27992809

clang/test/SemaCXX/warn-unused-result.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ void test() {
481481
p_return_warnunusedresult(); // expected-warning {{ignoring return value of type 'WarnUnusedResult' declared with 'gnu::warn_unused_result' attribute}}
482482

483483
// Function pointer expression return values
484-
pp_return_nodiscard()(); // no warning: TODO
484+
pp_return_nodiscard()(); // expected-warning {{ignoring return value of type 'NoDiscard' declared with 'nodiscard' attribute}}
485485
pp_return_warnunused()(); // no warning
486-
pp_return_warnunusedresult()(); // no warning: TODO
486+
pp_return_warnunusedresult()(); // expected-warning {{ignoring return value of type 'WarnUnusedResult' declared with 'gnu::warn_unused_result' attribute}}
487487

488488
// From a template
489489
from_a_template<NoDiscard>(); // expected-warning {{ignoring return value of type 'NoDiscard' declared with 'nodiscard' attribute}}

0 commit comments

Comments
 (0)