diff --git a/lib/fwdanalysis.cpp b/lib/fwdanalysis.cpp index 6363100b083..b30b928cfae 100644 --- a/lib/fwdanalysis.cpp +++ b/lib/fwdanalysis.cpp @@ -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 FwdAnalysis::getExprVarIds(const Token* expr, bool* localOut, bool* unknownVarIdOut) const { // all variable ids in expr. @@ -418,7 +428,7 @@ std::set 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 diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index e5f4c08f043..75fe4e85e1d 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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