Fix #10846 FP unreadVariable within a macro (regression) (#3874)

* Fix #10846 FP unreadVariable within a macro (regression)

* Format

* Format
This commit is contained in:
chrchr-github 2022-03-05 08:14:57 +01:00 committed by GitHub
parent 27baa20f38
commit 0b0a8cad39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View File

@ -1337,14 +1337,21 @@ void CheckUnusedVar::checkFunctionVariableUsage()
else if (!usage._write && !usage._allocateMemory && var && !var->isStlType() && !isEmptyType(var->type())) else if (!usage._write && !usage._allocateMemory && var && !var->isStlType() && !isEmptyType(var->type()))
unassignedVariableError(usage._var->nameToken(), varname); unassignedVariableError(usage._var->nameToken(), varname);
else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) { else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) {
if (usage._lastAccess->linenr() == var->nameToken()->linenr() && var->nameToken()->next()->isSplittedVarDeclEq()) { const Token* vnt = var->nameToken();
bool error = true; bool error = false;
if (vnt->next()->isSplittedVarDeclEq()) {
const Token* nextStmt = vnt->tokAt(2);
while (nextStmt && nextStmt->str() != ";")
nextStmt = nextStmt->next();
error = precedes(usage._lastAccess, nextStmt);
}
if (error) {
if (mTokenizer->isCPP() && var->isClass() && if (mTokenizer->isCPP() && var->isClass() &&
(!var->valueType() || var->valueType()->type == ValueType::Type::UNKNOWN_TYPE)) { (!var->valueType() || var->valueType()->type == ValueType::Type::UNKNOWN_TYPE)) {
const std::string typeName = var->getTypeName(); const std::string typeName = var->getTypeName();
switch (mSettings->library.getTypeCheck("unusedvar", typeName)) { switch (mSettings->library.getTypeCheck("unusedvar", typeName)) {
case Library::TypeCheck::def: case Library::TypeCheck::def:
reportLibraryCfgError(var->nameToken(), typeName); reportLibraryCfgError(vnt, typeName);
break; break;
case Library::TypeCheck::check: case Library::TypeCheck::check:
break; break;
@ -1353,7 +1360,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
} }
} }
if (error) if (error)
unreadVariableError(usage._var->nameToken(), varname, false); unreadVariableError(vnt, varname, false);
} }
} }
} }

View File

@ -3333,8 +3333,8 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'var' is assigned a value that is never used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'var' is assigned a value that is never used.\n", errout.str());
} }
void localvar62() { // #10824 void localvar62() {
functionVariableUsage("void f() {\n" functionVariableUsage("void f() {\n" // #10824
" S* s = nullptr;\n" " S* s = nullptr;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
@ -3343,6 +3343,23 @@ private:
" S* s{};\n" " S* s{};\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", "", errout.str());
functionVariableUsage("int f() {\n"
" int i = 0, j = 1;\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'j' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("int f() {\n"
" int i = 0, j = 1;\n"
" return j;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void f() {\n" // #10846
" int i = 1; while (i) { i = g(); }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void localvarloops() { void localvarloops() {