Warn when incrementing uninitialized value (#4042)

* Warn when incrementing uninitialized value

* Format
This commit is contained in:
Paul Fultz II 2022-04-24 02:56:58 -05:00 committed by GitHub
parent 5d5562266d
commit 6b9ac6f7a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -1579,7 +1579,7 @@ static ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* se
if (tok->astParent()->isCast())
return ExprUsage::NotUsed;
}
if (indirect == 0 && Token::Match(tok->astParent(), "%cop%|%assign%") && tok->astParent()->str() != "=")
if (indirect == 0 && Token::Match(tok->astParent(), "%cop%|%assign%|++|--") && tok->astParent()->str() != "=")
return ExprUsage::Used;
return getFunctionUsage(tok, indirect, settings);
}

View File

@ -5285,6 +5285,12 @@ private:
" g(p->x);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:8]: (error) Uninitialized variable: p->x\n", errout.str());
valueFlowUninit("void f() {\n"
" int a;\n"
" a++;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
}
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value