aaron.ballman created this revision.
aaron.ballman added reviewers: rsmith, Quuxplusone, erik.pilkington.

If the range-based for loop function body is not a compound statement, we would 
fail to diagnose discarded results from the statement. This only impacted 
range-based for loops because other kinds of for loops already manually check 
the body for unused expression results.

This addresses PR39837.


https://reviews.llvm.org/D55955

Files:
  lib/Sema/SemaStmt.cpp
  test/SemaCXX/warn-unused-result.cpp


Index: test/SemaCXX/warn-unused-result.cpp
===================================================================
--- test/SemaCXX/warn-unused-result.cpp
+++ test/SemaCXX/warn-unused-result.cpp
@@ -206,3 +206,13 @@
   (void)++p;
 }
 } // namespace
+
+namespace PR39837 {
+[[clang::warn_unused_result]] int f(int);
+
+void g() {
+  int a[2];
+  for (int b : a)
+    f(b); // expected-warning {{ignoring return value}}
+}
+} // namespace PR39837
Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -2843,6 +2843,7 @@
 
   DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B,
                         diag::warn_empty_range_based_for_body);
+  DiagnoseUnusedExprResult(B);
 
   DiagnoseForRangeVariableCopies(*this, ForStmt);
 


Index: test/SemaCXX/warn-unused-result.cpp
===================================================================
--- test/SemaCXX/warn-unused-result.cpp
+++ test/SemaCXX/warn-unused-result.cpp
@@ -206,3 +206,13 @@
   (void)++p;
 }
 } // namespace
+
+namespace PR39837 {
+[[clang::warn_unused_result]] int f(int);
+
+void g() {
+  int a[2];
+  for (int b : a)
+    f(b); // expected-warning {{ignoring return value}}
+}
+} // namespace PR39837
Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -2843,6 +2843,7 @@
 
   DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B,
                         diag::warn_empty_range_based_for_body);
+  DiagnoseUnusedExprResult(B);
 
   DiagnoseForRangeVariableCopies(*this, ForStmt);
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to