diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 88c28338b..df48c5fac 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1178,7 +1178,7 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const const Token *parent = tok; bool other = false; bool same = tok->astParent() && isSameExpression(mCpp, false, expr, tok, mLibrary, false, false, nullptr); - while (Token::Match(parent->astParent(), "*|.|::|[")) { + while (!same && Token::Match(parent->astParent(), "*|.|::|[")) { parent = parent->astParent(); if (parent && isSameExpression(mCpp, false, expr, parent, mLibrary, false, false, nullptr)) { same = true; @@ -1189,11 +1189,14 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const mValueFlow.push_back(v); } } - if (!same && Token::Match(parent, ". %var%") && parent->next()->varId() && exprVarIds.find(parent->next()->varId()) == exprVarIds.end()) { + if (Token::Match(parent, ". %var%") && parent->next()->varId() && exprVarIds.find(parent->next()->varId()) == exprVarIds.end()) { other = true; break; } } + if (mWhat != What::ValueFlow && same && Token::simpleMatch(parent->astParent(), "[") && parent == parent->astParent()->astOperand2()) { + return Result(Result::Type::READ); + } if (other) continue; if (Token::simpleMatch(parent->astParent(), "=") && parent == parent->astParent()->astOperand1()) { diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 3efa7e645..940e8bb83 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1019,7 +1019,7 @@ static void valueFlowArray(TokenList *tokenlist) !tok->variable()->isStlType()) { ValueFlow::Value value{1}; value.setKnown(); - // TODO : this leads to too many false positives so it is commented out. + // TODO : this leads to too many false positives so it is commented out. // See for instance https://github.com/danmar/cppcheck/commit/025881cf35fdde1299d16a09059e7305f8c9bd13 // setTokenValue(tok, value, tokenlist->getSettings()); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index aa07afe68..f1b7f0d43 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -109,6 +109,7 @@ private: TEST_CASE(localvar53); // continue TEST_CASE(localvar54); // ast, {} TEST_CASE(localvar55); + TEST_CASE(localvar56); TEST_CASE(localvarloops); // loops TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 @@ -2143,6 +2144,15 @@ private: errout.str()); } + void localvar56() { + functionVariableUsage("void f()\n" + "{\n" + " int x = 31;\n" + " mask[x] |= 123;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void localvarloops() { // loops functionVariableUsage("void fun() {\n"