Fixed #8928 (false positive: (style) Variable 'x' is assigned a value that is never used.)

This commit is contained in:
Daniel Marjamäki 2019-01-02 20:23:02 +01:00
parent 39ceb53578
commit 2b63997c2c
3 changed files with 16 additions and 3 deletions

View File

@ -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()) {

View File

@ -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());
}

View File

@ -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"