Disallow [[nodiscard]] on a function pointer declaration.

This is not allowed by [dcl.attr.nodiscard]p1 for the standard attribute, but
is still supported for the [[clang::warn_unused_result]] spelling.
This commit is contained in:
Aaron Ballman 2020-04-16 09:27:37 -04:00
parent fdf9bad573
commit 2ec5520a54
3 changed files with 15 additions and 1 deletions

View File

@ -2826,6 +2826,12 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
StringRef Str;
if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) {
// The standard attribute cannot be applied to variable declarations such
// as a function pointer.
if (isa<VarDecl>(D))
S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
<< AL << "functions, classes, or enumerations";
// If this is spelled as the standard C++17 attribute, but not in C++17,
// warn about using it as an extension. If there are attribute arguments,
// then claim it's a C++2a extension instead.

View File

@ -26,7 +26,7 @@ void f() {
(void)get_e();
}
[[nodiscard]] volatile char &(*fp)();
[[nodiscard]] volatile char &(*fp)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}
void g() {
fp(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}

View File

@ -246,3 +246,11 @@ void g() {
f(b); // expected-warning {{ignoring return value}}
}
} // namespace PR39837
namespace PR45520 {
[[nodiscard]] bool (*f)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}
[[clang::warn_unused_result]] bool (*g)();
__attribute__((warn_unused_result)) bool (*h)();
void i([[nodiscard]] bool (*fp)()); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}}
}