Fix #12176 false positive: unassignedVariable (#5656)

This commit is contained in:
chrchr-github 2023-11-14 12:26:12 +01:00 committed by GitHub
parent 6ac804d209
commit 3bd5a034bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -1356,7 +1356,8 @@ void CheckUnusedVar::checkFunctionVariableUsage()
unassignedVariableError(usage._var->nameToken(), varname);
}
// variable has been read but not written
else if (!usage._write && !usage._allocateMemory && var && !var->isStlType() && !isEmptyType(var->type()))
else if (!usage._write && !usage._allocateMemory && var && !var->isStlType() && !isEmptyType(var->type()) &&
!(var->type() && var->type()->needInitialization == Type::NeedInitialization::False))
unassignedVariableError(usage._var->nameToken(), varname);
else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) {
const Token* vnt = var->nameToken();

View File

@ -140,6 +140,7 @@ private:
TEST_CASE(localvar67); // #9946
TEST_CASE(localvar68);
TEST_CASE(localvar69);
TEST_CASE(localvar70);
TEST_CASE(localvarloops); // loops
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
@ -3827,6 +3828,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvar70() {
functionVariableUsage("struct S { int i = 0; };\n" // #12176
"void f(S s) {\n"
" S s1;\n"
" if (s == s1) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvarloops() {
// loops
functionVariableUsage("void fun(int c) {\n"