-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] Diagnose [[nodiscard]] return types in Objective-C++ #142541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
c683b2a
0c5aa22
c0e5f84
9bad608
32987a8
c2dee93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
|
|
||
| #include "clang/AST/ExprObjC.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/AST/Attr.h" | ||
| #include "clang/AST/ComputeDependence.h" | ||
| #include "clang/AST/SelectorLocationsKind.h" | ||
| #include "clang/AST/Type.h" | ||
|
|
@@ -272,6 +273,26 @@ QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const { | |
| return Ctx.getReferenceQualifiedType(this); | ||
| } | ||
|
|
||
| std::pair<const NamedDecl *, const Attr *> | ||
| ObjCMessageExpr::getUnusedResultAttr(ASTContext &Ctx) const { | ||
| // If the callee is marked nodiscard, return that attribute | ||
| if (const ObjCMethodDecl *MD = getMethodDecl()) | ||
| if (const auto *A = MD->getAttr<WarnUnusedResultAttr>()) | ||
| return {nullptr, A}; | ||
|
||
|
|
||
| // If the return type is a struct, union, or enum that is marked nodiscard, | ||
| // then return the return type attribute. | ||
| if (const TagDecl *TD = getCallReturnType(Ctx)->getAsTagDecl()) | ||
| if (const auto *A = TD->getAttr<WarnUnusedResultAttr>()) | ||
| return {TD, A}; | ||
|
|
||
| for (const auto *TD = getCallReturnType(Ctx)->getAs<TypedefType>(); TD; | ||
| TD = TD->desugar()->getAs<TypedefType>()) | ||
| if (const auto *A = TD->getDecl()->getAttr<WarnUnusedResultAttr>()) | ||
| return {TD->getDecl(), A}; | ||
| return {nullptr, nullptr}; | ||
| } | ||
|
|
||
| SourceRange ObjCMessageExpr::getReceiverRange() const { | ||
| switch (getReceiverKind()) { | ||
| case Instance: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // RUN: %clang_cc1 -fsyntax-only -verify %s | ||
|
|
||
| struct [[nodiscard]] expected {}; | ||
|
|
||
| typedef expected E; | ||
|
|
||
| @interface INTF | ||
| - (int) a [[nodiscard]]; | ||
| + (int) b [[nodiscard]]; | ||
| - (expected) c; | ||
| + (expected) d; | ||
| - (E) e; | ||
| + (E) f; | ||
| - (void) g [[nodiscard]]; // expected-warning {{attribute 'nodiscard' cannot be applied to Objective-C method without return value}} | ||
| @end | ||
|
|
||
| void foo(INTF *a) { | ||
| [a a]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| [INTF b]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| [a c]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
| [INTF d]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
| [a e]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
| [INTF f]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
| [a g]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // RUN: %clang_cc1 -fsyntax-only -verify %s | ||
|
|
||
| template<class T> | ||
| struct [[nodiscard]] expected {}; | ||
|
|
||
| using E = expected<int>; | ||
|
|
||
| @interface INTF | ||
| - (int) a [[nodiscard]]; | ||
| + (int) b [[nodiscard]]; | ||
| - (expected<int>) c; | ||
| + (expected<int>) d; | ||
| - (E) e; | ||
| + (E) f; | ||
| - (void) g [[nodiscard]]; // expected-warning {{attribute 'nodiscard' cannot be applied to Objective-C method without return value}} | ||
| @end | ||
|
|
||
| void foo(INTF *a) { | ||
| [a a]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| [INTF b]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| [a c]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
| [INTF d]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
| [a e]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
| [INTF f]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
| [a g]; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.