diff options
Diffstat (limited to 'test/SemaCXX/warn-unused-result.cpp')
-rw-r--r-- | test/SemaCXX/warn-unused-result.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/test/SemaCXX/warn-unused-result.cpp b/test/SemaCXX/warn-unused-result.cpp index 01bc457ec206..88f5ab1e85c0 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 |