[PATCH] D55955: Properly diagnose [[nodiscard]] on the body of a range-based for loop

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 20 13:54:18 PST 2018

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.



Index: test/SemaCXX/warn-unused-result.cpp
--- test/SemaCXX/warn-unused-result.cpp
+++ test/SemaCXX/warn-unused-result.cpp
@@ -206,3 +206,13 @@
 } // 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,
+  DiagnoseUnusedExprResult(B);
   DiagnoseForRangeVariableCopies(*this, ForStmt);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55955.179147.patch
Type: text/x-patch
Size: 839 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181220/798613bc/attachment.bin>

More information about the cfe-commits mailing list