Skip to content

Commit 9c50136

Browse files
Fix #10824 FN unreadVariable for pointer assignment when destructor exists (regression) (#3864)
* Fix #10824 FN unreadVariable for pointer assignment when destructor exists (regression) * unused variable * Issue warning for missing cfg * Format
1 parent de728f4 commit 9c50136

3 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎lib/checkunusedvar.cpp‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -972,13 +972,6 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
972972
} else if (varid1 && Token::Match(tok, "%varid% .", varid1)) {
973973
variables.read(varid1, tok);
974974
variables.write(varid1, start);
975-
} else if (var &&
976-
var->mType == Variables::pointer &&
977-
Token::Match(tok, "%name% ;") &&
978-
tok->varId() == 0 &&
979-
tok->hasKnownIntValue() &&
980-
tok->values().front().intvalue == 0) {
981-
variables.use(varid1, tok);
982975
} else {
983976
variables.write(varid1, tok);
984977
}
@@ -1156,6 +1149,15 @@ void CheckUnusedVar::checkFunctionVariableUsage()
11561149
// Parse all executing scopes..
11571150
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
11581151

1152+
auto reportLibraryCfgError = [this](const Token* tok, const std::string& typeName) {
1153+
if (mSettings->checkLibrary && mSettings->severity.isEnabled(Severity::information)) {
1154+
reportError(tok,
1155+
Severity::information,
1156+
"checkLibraryCheckType",
1157+
"--check-library: Provide <type-checks><unusedvar> configuration for " + typeName);
1158+
}
1159+
};
1160+
11591161
// only check functions
11601162
for (const Scope * scope : symbolDatabase->functionScopes) {
11611163
// Bailout when there are lambdas or inline functions
@@ -1281,12 +1283,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
12811283
const Token* scopeEnd = getEndOfExprScope(expr, scope, /*smallest*/ false);
12821284
if (fwdAnalysis.unusedValue(expr, start, scopeEnd)) {
12831285
if (!bailoutTypeName.empty() && bailoutTypeName != "auto") {
1284-
if (mSettings->checkLibrary && mSettings->severity.isEnabled(Severity::information)) {
1285-
reportError(tok,
1286-
Severity::information,
1287-
"checkLibraryCheckType",
1288-
"--check-library: Provide <type-checks><unusedvar> configuration for " + bailoutTypeName);
1289-
}
1286+
reportLibraryCfgError(tok, bailoutTypeName);
12901287
continue;
12911288
}
12921289

@@ -1339,6 +1336,26 @@ void CheckUnusedVar::checkFunctionVariableUsage()
13391336
// variable has been read but not written
13401337
else if (!usage._write && !usage._allocateMemory && var && !var->isStlType() && !isEmptyType(var->type()))
13411338
unassignedVariableError(usage._var->nameToken(), varname);
1339+
else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) {
1340+
if (usage._lastAccess->linenr() == var->nameToken()->linenr() && var->nameToken()->next()->isSplittedVarDeclEq()) {
1341+
bool error = true;
1342+
if (mTokenizer->isCPP() && var->isClass() &&
1343+
(!var->valueType() || var->valueType()->type == ValueType::Type::UNKNOWN_TYPE)) {
1344+
const std::string typeName = var->getTypeName();
1345+
switch (mSettings->library.getTypeCheck("unusedvar", typeName)) {
1346+
case Library::TypeCheck::def:
1347+
reportLibraryCfgError(var->nameToken(), typeName);
1348+
break;
1349+
case Library::TypeCheck::check:
1350+
break;
1351+
case Library::TypeCheck::suppress:
1352+
error = false;
1353+
}
1354+
}
1355+
if (error)
1356+
unreadVariableError(usage._var->nameToken(), varname, false);
1357+
}
1358+
}
13421359
}
13431360
}
13441361
}

‎lib/tokenlist.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,6 @@ static void compileTerm(Token *&tok, AST_state& state)
791791
tok = tok->tokAt(2);
792792
}
793793
} else if (!state.cpp || !Token::Match(tok, "new|delete %name%|*|&|::|(|[")) {
794-
Token* tok2 = tok;
795794
std::vector<Token*> inner;
796795
tok = skipDecl(tok, &inner);
797796
for (Token* tok3 : inner) {

‎test/testunusedvar.cpp‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class TestUnusedVar : public TestFixture {
128128
TEST_CASE(localvar59); // #9737
129129
TEST_CASE(localvar60);
130130
TEST_CASE(localvar61); // #9407
131+
TEST_CASE(localvar62); // #10824
131132
TEST_CASE(localvarloops); // loops
132133
TEST_CASE(localvaralias1);
133134
TEST_CASE(localvaralias2); // ticket #1637
@@ -2384,7 +2385,9 @@ class TestUnusedVar : public TestFixture {
23842385
" int i = 0;\n"
23852386
" int &j = i;\n"
23862387
"}");
2387-
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'j' is assigned a value that is never used.\n", errout.str());
2388+
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'j' is assigned a value that is never used.\n"
2389+
"[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n",
2390+
errout.str());
23882391

23892392
functionVariableUsage("void foo()\n"
23902393
"{\n"
@@ -3330,6 +3333,18 @@ class TestUnusedVar : public TestFixture {
33303333
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'var' is assigned a value that is never used.\n", errout.str());
33313334
}
33323335

3336+
void localvar62() { // #10824
3337+
functionVariableUsage("void f() {\n"
3338+
" S* s = nullptr;\n"
3339+
"}\n");
3340+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
3341+
3342+
functionVariableUsage("void f() {\n"
3343+
" S* s{};\n"
3344+
"}\n");
3345+
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", "", errout.str());
3346+
}
3347+
33333348
void localvarloops() {
33343349
// loops
33353350
functionVariableUsage("void fun(int c) {\n"

0 commit comments

Comments
 (0)