Kaydet (Commit) 06c281c2 authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:unusedindex fix false+ in nested loops

Change-Id: I31acbf104e49a4d1f077817a68d0b116fd2e0a30
üst ca6d205e
......@@ -18,6 +18,22 @@ void func1()
n += 1;
for (int i = 0; i < 10; ++i)
n += i;
for (int i = 0; i < 10; ++i) // expected-error {{loop variable not used [loplugin:unusedindex]}}
{
for (int j = 0; j < 10; ++j)
{
n += j;
}
}
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
n += j;
n += i;
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
......@@ -38,7 +38,7 @@ public:
bool VisitDeclRefExpr(DeclRefExpr const* stmt);
private:
VarDecl const* mLoopVarDecl = nullptr;
std::vector<VarDecl const*> mLoopVarDecls;
std::unordered_set<VarDecl const*> mFoundSet;
};
......@@ -46,27 +46,28 @@ bool UnusedIndex::TraverseForStmt(ForStmt* stmt)
{
if (ignoreLocation(stmt))
return true;
auto savedCopy = mLoopVarDecl;
mLoopVarDecl = nullptr;
VarDecl const* loopVarDecl = nullptr;
if (stmt->getInit())
{
auto declStmt = dyn_cast<DeclStmt>(stmt->getInit());
if (declStmt && declStmt->isSingleDecl())
{
auto varDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl());
if (varDecl)
mLoopVarDecl = varDecl;
loopVarDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl());
}
}
if (loopVarDecl)
mLoopVarDecls.push_back(loopVarDecl);
// deliberately ignore the other parts of the for stmt, except for the body
auto ret = RecursiveASTVisitor::TraverseStmt(stmt->getBody());
if (mLoopVarDecl && mFoundSet.erase(mLoopVarDecl) == 0)
report(DiagnosticsEngine::Warning, "loop variable not used", mLoopVarDecl->getLocStart())
<< mLoopVarDecl->getSourceRange();
mLoopVarDecl = savedCopy;
if (loopVarDecl && mFoundSet.erase(loopVarDecl) == 0)
report(DiagnosticsEngine::Warning, "loop variable not used", loopVarDecl->getLocStart())
<< loopVarDecl->getSourceRange();
if (loopVarDecl)
mLoopVarDecls.pop_back();
return ret;
}
......@@ -75,7 +76,7 @@ bool UnusedIndex::VisitDeclRefExpr(DeclRefExpr const* stmt)
auto varDecl = dyn_cast<VarDecl>(stmt->getDecl());
if (!varDecl)
return true;
if (mLoopVarDecl && mLoopVarDecl == varDecl)
if (std::find(mLoopVarDecls.begin(), mLoopVarDecls.end(), varDecl) != mLoopVarDecls.end())
mFoundSet.insert(varDecl);
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment