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
This commit is contained in:
Benjamin Kramer 2013-10-16 16:21:04 +00:00
parent 069b90463d
commit 9940a5df86
2 changed files with 19 additions and 1 deletions

View 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));
}

View 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}}
};
}