From: Erich Keane Date: Wed, 19 Apr 2017 21:24:55 +0000 (+0000) Subject: Corrrect warn_unused_result attribute X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=50cdd18bb1c87febc93cd84eaa1bafd74d9d1c16;p=clang Corrrect warn_unused_result attribute The original idea was that if the attribute on an operator, that the return-value unused-ness wouldn't matter. However, all of the operators except postfix inc/dec return references! References don't result in this warning anyway, so those are already excluded. Differential Revision: https://reviews.llvm.org/D32207 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@300764 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index ad723a3e2b..573ea55de1 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -2082,10 +2082,7 @@ public: const Attr *getUnusedResultAttr() const; /// \brief Returns true if this function or its return type has the - /// warn_unused_result attribute. If the return type has the attribute and - /// this function is a method of the return type's class, then false will be - /// returned to avoid spurious warnings on member methods such as assignment - /// operators. + /// warn_unused_result attribute. bool hasUnusedResultAttr() const { return getUnusedResultAttr() != nullptr; } /// \brief Returns the storage class as written in the source. For the diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 2b22e5bb50..094e8dcff0 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -3003,9 +3003,7 @@ SourceRange FunctionDecl::getExceptionSpecSourceRange() const { const Attr *FunctionDecl::getUnusedResultAttr() const { QualType RetType = getReturnType(); if (RetType->isRecordType()) { - const CXXRecordDecl *Ret = RetType->getAsCXXRecordDecl(); - const auto *MD = dyn_cast(this); - if (Ret && !(MD && MD->getCorrespondingMethodInClass(Ret, true))) { + if (const CXXRecordDecl *Ret = RetType->getAsCXXRecordDecl()) { if (const auto *R = Ret->getAttr()) return R; } diff --git a/test/SemaCXX/warn-unused-result.cpp b/test/SemaCXX/warn-unused-result.cpp index 01bc457ec2..88f5ab1e85 100644 --- a/test/SemaCXX/warn-unused-result.cpp +++ b/test/SemaCXX/warn-unused-result.cpp @@ -160,3 +160,49 @@ void g() { (void)noexcept(f(), false); // Should not warn. } } + +namespace { +// C++ Methods should warn even in their own class. +struct [[clang::warn_unused_result]] S { + S DoThing() { return {}; }; + S operator++(int) { return {}; }; + S operator--(int) { return {}; }; + // Improperly written prefix. + S operator++() { return {}; }; + S operator--() { return {}; }; +}; + +struct [[clang::warn_unused_result]] P { + P DoThing() { return {}; }; +}; + +P operator++(const P &, int) { return {}; }; +P operator--(const P &, int) { return {}; }; +// Improperly written prefix. +P operator++(const P &) { return {}; }; +P operator--(const P &) { return {}; }; + +void f() { + S s; + P p; + s.DoThing(); // expected-warning {{ignoring return value}} + p.DoThing(); // expected-warning {{ignoring return value}} + // Only postfix is expected to warn when written correctly. + s++; // expected-warning {{ignoring return value}} + s--; // expected-warning {{ignoring return value}} + p++; // expected-warning {{ignoring return value}} + p--; // expected-warning {{ignoring return value}} + // Improperly written prefix operators should still warn. + ++s; // expected-warning {{ignoring return value}} + --s; // expected-warning {{ignoring return value}} + ++p; // expected-warning {{ignoring return value}} + --p; // expected-warning {{ignoring return value}} + + // Silencing the warning by cast to void still works. + (void)s.DoThing(); + (void)s++; + (void)p++; + (void)++s; + (void)++p; +} +} // namespace