]> granicus.if.org Git - clang/commitdiff
For classes that have the warn_unused_result attribute, don't apply the
authorKaelyn Uhrain <rikka@google.com>
Tue, 13 Nov 2012 21:23:31 +0000 (21:23 +0000)
committerKaelyn Uhrain <rikka@google.com>
Tue, 13 Nov 2012 21:23:31 +0000 (21:23 +0000)
attribute to the class' methods even when they return an instance of the
class (e.g. assignment operators).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167873 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/SemaCXX/warn-unused-result.cpp

index f2e840494fc2102e2844f086e5173edbdc6f7df8..04f66e5882b98d17181297f91b2ebca0e9b23849 100644 (file)
@@ -5696,7 +5696,11 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
       RetType->getAsCXXRecordDecl() : RetType->getPointeeCXXRecordDecl();
   if (!NewFD->isInvalidDecl() && !NewFD->hasAttr<WarnUnusedResultAttr>() &&
       Ret && Ret->hasAttr<WarnUnusedResultAttr>()) {
-    NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(), Context));
+    const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD);
+    if (!(MD && MD->getCorrespondingMethodInClass(Ret, true))) {
+      NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(),
+                                                        Context));
+    }
   }
 
   if (!getLangOpts().CPlusPlus) {
index 5ce0f98bc48939f5582c4165223bec32b30f2697..b0bf61f381807dcc26b26884b651f5eea12c54b3 100644 (file)
@@ -46,6 +46,12 @@ void bah() {
 namespace warn_unused_CXX11 {
 struct [[clang::warn_unused_result]] Status {
   bool ok() const;
+  Status& operator=(const Status& x);
+  inline void Update(const Status& new_status) {
+    if (ok()) {
+      *this = new_status; //no-warning
+    }
+  }
 };
 Status DoSomething();
 Status& DoSomethingElse();