r328602 - Fix unused expression warning in co_await.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 26 17:58:16 PDT 2018


Author: ericwf
Date: Mon Mar 26 17:58:16 2018
New Revision: 328602

URL: http://llvm.org/viewvc/llvm-project?rev=328602&view=rev
Log:
Fix unused expression warning in co_await.

Previously, anytime the result of the resume expression in
operator co_await was unused, a warning was generated. This
patch fixes the issue by only generating the unused result warning
if calling `await_resume()` would also generate a warning.

Modified:
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/test/SemaCXX/coroutines.cpp

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=328602&r1=328601&r2=328602&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Mar 26 17:58:16 2018
@@ -2049,6 +2049,9 @@ bool Expr::isUnusedResultAWarning(const
   case GenericSelectionExprClass:
     return cast<GenericSelectionExpr>(this)->getResultExpr()->
       isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
+  case CoawaitExprClass:
+    return cast<CoawaitExpr>(this)->getResumeExpr()->
+      isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
   case ChooseExprClass:
     return cast<ChooseExpr>(this)->getChosenSubExpr()->
       isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);

Modified: cfe/trunk/test/SemaCXX/coroutines.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/coroutines.cpp?rev=328602&r1=328601&r2=328602&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/coroutines.cpp (original)
+++ cfe/trunk/test/SemaCXX/coroutines.cpp Mon Mar 26 17:58:16 2018
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -std=c++14 -fcoroutines-ts -verify %s -fcxx-exceptions -fexceptions
+// RUN: %clang_cc1 -std=c++14 -fcoroutines-ts -verify %s -fcxx-exceptions -fexceptions -Wunused-result
 
 void no_coroutine_traits_bad_arg_await() {
   co_await a; // expected-error {{include <experimental/coroutine>}}
@@ -1337,3 +1337,27 @@ bad_coroutine_calls_with_no_matching_con
 }
 
 } // namespace CoroHandleMemberFunctionTest
+
+
+class awaitable_no_unused_warn {
+public:
+  using handle_type = std::experimental::coroutine_handle<>;
+  constexpr bool await_ready()  { return false; }
+  void await_suspend(handle_type) noexcept {}
+  int await_resume() { return 1; }
+};
+
+
+class awaitable_unused_warn {
+public:
+  using handle_type = std::experimental::coroutine_handle<>;
+  constexpr bool await_ready()  { return false; }
+  void await_suspend(handle_type) noexcept {}
+  [[nodiscard]]
+  int await_resume() { return 1; }
+};
+
+void test_unused_warning() {
+  co_await awaitable_no_unused_warn();
+  co_await awaitable_unused_warn(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+}




More information about the cfe-commits mailing list