]> granicus.if.org Git - clang/commitdiff
Sema: Simplify the check if a method returns an instance of the class.
authorBenjamin Kramer <benny.kra@googlemail.com>
Wed, 16 Oct 2013 16:21:04 +0000 (16:21 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Wed, 16 Oct 2013 16:21:04 +0000 (16:21 +0000)
Just checking if the parent of the method is the same as the return type
should be sufficient. Also fixes PR17587.

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

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

index daf135a06e8fcc015adb5fdbe70b09f8a276fb79..c6537637a5f39d2261da1b3cf17d32b422c14942 100644 (file)
@@ -6911,7 +6911,9 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
   if (!NewFD->isInvalidDecl() && !NewFD->hasAttr<WarnUnusedResultAttr>() &&
       Ret && Ret->hasAttr<WarnUnusedResultAttr>()) {
     const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD);
-    if (!(MD && MD->getCorrespondingMethodInClass(Ret, true))) {
+    // Attach the attribute to the new decl. Don't apply the attribute if it
+    // returns an instance of the class (e.g. assignment operators).
+    if (!MD || MD->getParent() != Ret) {
       NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(),
                                                         Context));
     }
index b0bf61f381807dcc26b26884b651f5eea12c54b3..581af09080dba5aae9493e62c77c4d146f1c72cb 100644 (file)
@@ -78,3 +78,19 @@ void lazy() {
   DoYetAnotherThing();
 }
 }
+
+namespace PR17587 {
+struct [[clang::warn_unused_result]] Status;
+
+struct Foo {
+  Status Bar();
+};
+
+struct Status {};
+
+void Bar() {
+  Foo f;
+  f.Bar(); // expected-warning {{ignoring return value}}
+};
+
+}