Fix regression: unreadVariable for trivial initialization (#3698)

This commit is contained in:
chrchr-github 2022-01-12 22:06:03 +01:00 committed by GitHub
parent 1b89c998f5
commit 7aa0ec3e95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -1183,6 +1183,9 @@ void CheckUnusedVar::checkFunctionVariableUsage()
if (!isAssignment && !isInitialization && !isIncrementOrDecrement)
continue;
if (isInitialization && Token::Match(tok, "%var% { }")) // don't warn for trivial initialization
continue;
if (isIncrementOrDecrement && tok->astParent() && precedes(tok, tok->astOperand1()))
continue;

View File

@ -3040,6 +3040,19 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void f(bool b, bool c, double& r) {\n"
" double d{};\n"
" if (b) {\n"
" d = g();\n"
" r += d;\n"
" }\n"
" if (c) {\n"
" d = h();\n"
" r += d;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("int func() {\n"
" std::mutex m;\n"
" std::unique_lock<std::mutex> l{ m };\n"