diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2013-10-16 16:21:04 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2013-10-16 16:21:04 +0000 |
commit | 9940a5df86d36ecc69ee3a82e1a4fbfe841960ab (patch) | |
tree | 28cb501a3e5001acc599c016ca169a48bb40f976 | |
parent | 069b90463d933b7d5756402178d11de3f6223d1a (diff) | |
download | bcm5719-llvm-9940a5df86d36ecc69ee3a82e1a4fbfe841960ab.tar.gz bcm5719-llvm-9940a5df86d36ecc69ee3a82e1a4fbfe841960ab.zip |
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.
llvm-svn: 192802
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 4 | ||||
-rw-r--r-- | clang/test/SemaCXX/warn-unused-result.cpp | 16 |
2 files changed, 19 insertions, 1 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index daf135a06e8..c6537637a5f 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -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)); } diff --git a/clang/test/SemaCXX/warn-unused-result.cpp b/clang/test/SemaCXX/warn-unused-result.cpp index b0bf61f3818..581af09080d 100644 --- a/clang/test/SemaCXX/warn-unused-result.cpp +++ b/clang/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}} +}; + +} |