Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/fwdanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *
return Result(Result::Type::NONE);
}

static bool isSimpleIndexExpression(const Token* tok)
{
const Token* idx = tok->astOperand2();
if (!idx)
return false;
if (idx->isIncDecOp())
idx = idx->astOperand1();
return idx->variable() && idx->variable()->scope() == tok->scope();
}

std::set<nonneg int> FwdAnalysis::getExprVarIds(const Token* expr, bool* localOut, bool* unknownVarIdOut) const
{
// all variable ids in expr.
Expand All @@ -418,7 +428,7 @@ std::set<nonneg int> FwdAnalysis::getExprVarIds(const Token* expr, bool* localOu
bool unknownVarId = false;
visitAstNodes(expr,
[&](const Token *tok) {
if (tok->str() == "[" && mWhat == What::UnusedValue)
if (tok->str() == "[" && mWhat == What::UnusedValue && isSimpleIndexExpression(tok))
return ChildrenToVisit::op1;
if (tok->varId() == 0 && tok->isName() && tok->strAt(-1) != ".") {
// unknown variable
Expand Down
16 changes: 16 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5813,6 +5813,22 @@ class TestUnusedVar : public TestFixture {
" {}\n"
"}");
ASSERT_EQUALS("", errout_str());

functionVariableUsage("void f(const int* b, int x) {\n" // #11125
" int a[6];\n"
" int i = 0;\n"
" for (int j = 0; j < 6; ++j) {\n"
" if (b[j] != 0) {\n"
" a[i] = j;\n"
" ++i;\n"
" }\n"
" }\n"
" if (i > 1) {\n"
" a[i] = a[0];\n"
" (void)a[x];\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void localvarForEach() { // #4155 - foreach
Expand Down
Loading