Fix #11879 FN unreadVariable (regression) (#5345)

This commit is contained in:
chrchr-github 2023-08-21 10:44:17 +02:00 committed by GitHub
parent 725c431ecc
commit 3281fc91db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View File

@ -41,22 +41,22 @@ void CheckStatistics::addItem(const QString &tool, ShowTypes::ShowType type)
const QString lower = tool.toLower(); const QString lower = tool.toLower();
switch (type) { switch (type) {
case ShowTypes::ShowStyle: case ShowTypes::ShowStyle:
::addItem(mStyle, tool); ::addItem(mStyle, lower);
break; break;
case ShowTypes::ShowWarnings: case ShowTypes::ShowWarnings:
::addItem(mWarning, tool); ::addItem(mWarning, lower);
break; break;
case ShowTypes::ShowPerformance: case ShowTypes::ShowPerformance:
::addItem(mPerformance, tool); ::addItem(mPerformance, lower);
break; break;
case ShowTypes::ShowPortability: case ShowTypes::ShowPortability:
::addItem(mPortability, tool); ::addItem(mPortability, lower);
break; break;
case ShowTypes::ShowErrors: case ShowTypes::ShowErrors:
::addItem(mError, tool); ::addItem(mError, lower);
break; break;
case ShowTypes::ShowInformation: case ShowTypes::ShowInformation:
::addItem(mInformation, tool); ::addItem(mInformation, lower);
break; break;
case ShowTypes::ShowNone: case ShowTypes::ShowNone:
default: default:

View File

@ -1361,7 +1361,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) { else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) {
const Token* vnt = var->nameToken(); const Token* vnt = var->nameToken();
bool error = false; bool error = false;
if (vnt->next()->isSplittedVarDeclEq()) { if (vnt->next()->isSplittedVarDeclEq() || (!var->isReference() && vnt->next()->str() == "=")) {
const Token* nextStmt = vnt->tokAt(2); const Token* nextStmt = vnt->tokAt(2);
if (nextStmt->isExpandedMacro()) { if (nextStmt->isExpandedMacro()) {
const Token* parent = nextStmt; const Token* parent = nextStmt;

View File

@ -5689,8 +5689,7 @@ private:
"{\n" "{\n"
" static int i = 0;\n" " static int i = 0;\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n", ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n",
"",
errout.str()); errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
@ -6305,6 +6304,15 @@ private:
" const ::std::lock_guard g(m);\n" " const ::std::lock_guard g(m);\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
functionVariableUsage("void f(const std::string& str, int i) {\n" // #11879
" const std::string s = str;\n"
" switch (i) {\n"
" default:\n"
" break;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
} }
void localVarClass() { void localVarClass() {