diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index cc64efa7f07f..6f1814deb00b 100644 --- a/clang/lib/Analysis/ReachableCode.cpp +++ b/clang/lib/Analysis/ReachableCode.cpp @@ -631,6 +631,10 @@ void DeadCodeScan::reportDeadCode(const CFGBlock *B, // a for/for-range loop. This is the block that contains // the increment code. if (const Stmt *LoopTarget = B->getLoopTarget()) { + // The increment on a foreach statement is not written. + if (isa(LoopTarget)) + return; + SourceLocation Loc = LoopTarget->getBeginLoc(); SourceRange R1(Loc, Loc), R2; diff --git a/clang/test/SemaCXX/unreachable-code.cpp b/clang/test/SemaCXX/unreachable-code.cpp index fd006c099e7d..61805837dc29 100644 --- a/clang/test/SemaCXX/unreachable-code.cpp +++ b/clang/test/SemaCXX/unreachable-code.cpp @@ -52,6 +52,11 @@ void test3() { } } +void test4() { + for (char c : "abc") // no-warning + break; +} + // PR 6130 - Don't warn about bogus unreachable code with throw's and // temporary objects. class PR6130 {