From ad068daf677eb1f2cb7ffdb9eb0fe6985e7afa70 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 20 Mar 2026 15:03:12 +0100 Subject: [PATCH 1/3] Update testunusedvar.cpp --- test/testunusedvar.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 From e6476f24ecaa99fcabeed9a059e842a5905bc3c8 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 20 Mar 2026 15:06:43 +0100 Subject: [PATCH 2/3] Update fwdanalysis.cpp --- lib/fwdanalysis.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/fwdanalysis.cpp b/lib/fwdanalysis.cpp index 6363100b083..9229407c96a 100644 --- a/lib/fwdanalysis.cpp +++ b/lib/fwdanalysis.cpp @@ -410,6 +410,14 @@ 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->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 +426,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 From c62e7d611408ccb9a6d2f78b339c2b5b1a3597e8 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 20 Mar 2026 16:02:29 +0100 Subject: [PATCH 3/3] Update fwdanalysis.cpp --- lib/fwdanalysis.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/fwdanalysis.cpp b/lib/fwdanalysis.cpp index 9229407c96a..b30b928cfae 100644 --- a/lib/fwdanalysis.cpp +++ b/lib/fwdanalysis.cpp @@ -413,6 +413,8 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token * 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();