From: Benjamin Kramer Date: Wed, 16 Oct 2013 16:21:04 +0000 (+0000) Subject: Sema: Simplify the check if a method returns an instance of the class. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a32966fd8474e7a7002f65d32ace6b7e18dee66b;p=clang Sema: Simplify the check if a method returns an instance of the class. 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 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index daf135a06e..c6537637a5 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -6911,7 +6911,9 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, if (!NewFD->isInvalidDecl() && !NewFD->hasAttr() && Ret && Ret->hasAttr()) { const CXXMethodDecl *MD = dyn_cast(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)); } diff --git a/test/SemaCXX/warn-unused-result.cpp b/test/SemaCXX/warn-unused-result.cpp index b0bf61f381..581af09080 100644 --- a/test/SemaCXX/warn-unused-result.cpp +++ b/test/SemaCXX/warn-unused-result.cpp @@ -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}} +}; + +}