diff options
author | Sam McCall <sam.mccall@gmail.com> | 2019-02-15 07:16:11 +0000 |
---|---|---|
committer | Sam McCall <sam.mccall@gmail.com> | 2019-02-15 07:16:11 +0000 |
commit | ce2b40def1765832ac505033bf24a78fe85d06f9 (patch) | |
tree | 7cce1be8d8b8fe20ace2311f8f4920ce7c4bf0be /clang/lib/Analysis/ReachableCode.cpp | |
parent | 184bd7a0d856086d1ed22a6b53f37c8d39e9a2b0 (diff) | |
download | llvm-ce2b40def1765832ac505033bf24a78fe85d06f9.zip llvm-ce2b40def1765832ac505033bf24a78fe85d06f9.tar.gz llvm-ce2b40def1765832ac505033bf24a78fe85d06f9.tar.bz2 |
[Analysis] -Wunreachable-code shouldn't fire on the increment of a foreach loop
Summary:
The idea is that the code here isn't written, so doesn't indicate a bug.
Similar to code expanded from macros.
This means the warning no longer fires on this code:
for (auto C : collection) {
process(C);
return;
}
handleEmptyCollection();
Unclear whether this is more often a bug or not in practice, I think it's a
reasonable idiom in some cases.
Either way, if we want to warn on "loop that doesn't loop", I think it should be
a separate warning, and catch `while(1) break;`
Reviewers: ilya-biryukov, ioeric
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58134
llvm-svn: 354102
Diffstat (limited to 'clang/lib/Analysis/ReachableCode.cpp')
-rw-r--r-- | clang/lib/Analysis/ReachableCode.cpp | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index cc64efa..6f1814d 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<CXXForRangeStmt>(LoopTarget)) + return; + SourceLocation Loc = LoopTarget->getBeginLoc(); SourceRange R1(Loc, Loc), R2; |