From 2b63997c2c55339745e8e5d89db6a6d928f9a73b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 2 Jan 2019 20:23:02 +0100 Subject: [PATCH] Fixed #8928 (false positive: (style) Variable 'x' is assigned a value that is never used.) --- lib/astutils.cpp | 7 +++++-- lib/valueflow.cpp | 2 +- test/testunusedvar.cpp | 10 ++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) 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"