Fixed #8928 (false positive: (style) Variable 'x' is assigned a value that is never used.)
This commit is contained in:
parent
39ceb53578
commit
2b63997c2c
|
@ -1178,7 +1178,7 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
|
||||||
const Token *parent = tok;
|
const Token *parent = tok;
|
||||||
bool other = false;
|
bool other = false;
|
||||||
bool same = tok->astParent() && isSameExpression(mCpp, false, expr, tok, mLibrary, false, false, nullptr);
|
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();
|
parent = parent->astParent();
|
||||||
if (parent && isSameExpression(mCpp, false, expr, parent, mLibrary, false, false, nullptr)) {
|
if (parent && isSameExpression(mCpp, false, expr, parent, mLibrary, false, false, nullptr)) {
|
||||||
same = true;
|
same = true;
|
||||||
|
@ -1189,11 +1189,14 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
|
||||||
mValueFlow.push_back(v);
|
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;
|
other = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (mWhat != What::ValueFlow && same && Token::simpleMatch(parent->astParent(), "[") && parent == parent->astParent()->astOperand2()) {
|
||||||
|
return Result(Result::Type::READ);
|
||||||
|
}
|
||||||
if (other)
|
if (other)
|
||||||
continue;
|
continue;
|
||||||
if (Token::simpleMatch(parent->astParent(), "=") && parent == parent->astParent()->astOperand1()) {
|
if (Token::simpleMatch(parent->astParent(), "=") && parent == parent->astParent()->astOperand1()) {
|
||||||
|
|
|
@ -1019,7 +1019,7 @@ static void valueFlowArray(TokenList *tokenlist)
|
||||||
!tok->variable()->isStlType()) {
|
!tok->variable()->isStlType()) {
|
||||||
ValueFlow::Value value{1};
|
ValueFlow::Value value{1};
|
||||||
value.setKnown();
|
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
|
// See for instance https://github.com/danmar/cppcheck/commit/025881cf35fdde1299d16a09059e7305f8c9bd13
|
||||||
// setTokenValue(tok, value, tokenlist->getSettings());
|
// setTokenValue(tok, value, tokenlist->getSettings());
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,7 @@ private:
|
||||||
TEST_CASE(localvar53); // continue
|
TEST_CASE(localvar53); // continue
|
||||||
TEST_CASE(localvar54); // ast, {}
|
TEST_CASE(localvar54); // ast, {}
|
||||||
TEST_CASE(localvar55);
|
TEST_CASE(localvar55);
|
||||||
|
TEST_CASE(localvar56);
|
||||||
TEST_CASE(localvarloops); // loops
|
TEST_CASE(localvarloops); // loops
|
||||||
TEST_CASE(localvaralias1);
|
TEST_CASE(localvaralias1);
|
||||||
TEST_CASE(localvaralias2); // ticket #1637
|
TEST_CASE(localvaralias2); // ticket #1637
|
||||||
|
@ -2143,6 +2144,15 @@ private:
|
||||||
errout.str());
|
errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void localvar56() {
|
||||||
|
functionVariableUsage("void f()\n"
|
||||||
|
"{\n"
|
||||||
|
" int x = 31;\n"
|
||||||
|
" mask[x] |= 123;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
|
|
||||||
void localvarloops() {
|
void localvarloops() {
|
||||||
// loops
|
// loops
|
||||||
functionVariableUsage("void fun() {\n"
|
functionVariableUsage("void fun() {\n"
|
||||||
|
|
Loading…
Reference in New Issue