Skip to content

Commit e063656

Browse files
checkLibraryCheckType: handle global scope operator / Fix FN unreadVariable (#5199)
1 parent 977e132 commit e063656

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
12131213
continue;
12141214
tok = tok->next();
12151215
}
1216-
if (tok->astParent() && !tok->astParent()->isAssignmentOp() && tok->str() != "(") {
1216+
if (!isInitialization && tok->astParent() && !tok->astParent()->isAssignmentOp() && tok->str() != "(") {
12171217
const Token *parent = tok->astParent();
12181218
while (Token::Match(parent, "%oror%|%comp%|!|&&"))
12191219
parent = parent->astParent();
@@ -1268,7 +1268,9 @@ void CheckUnusedVar::checkFunctionVariableUsage()
12681268
op1Var->isClass() &&
12691269
(!op1Var->valueType() || op1Var->valueType()->type == ValueType::Type::UNKNOWN_TYPE)) {
12701270
// Check in the library if we should bailout or not..
1271-
const std::string typeName = op1Var->getTypeName();
1271+
std::string typeName = op1Var->getTypeName();
1272+
if (typeName.compare(0, 2, "::") == 0)
1273+
typeName.erase(typeName.begin(), typeName.begin() + 2);
12721274
switch (mSettings->library.getTypeCheck("unusedvar", typeName)) {
12731275
case Library::TypeCheck::def:
12741276
bailoutTypeName = typeName;

test/testunusedvar.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6197,6 +6197,15 @@ class TestUnusedVar : public TestFixture {
61976197
" s[0] = 0;\n"
61986198
"}\n");
61996199
ASSERT_EQUALS("", errout.str());
6200+
6201+
functionVariableUsage("struct S {\n"
6202+
" std::mutex m;\n"
6203+
" void f();\n"
6204+
"};\n"
6205+
"void S::f() {\n"
6206+
" const ::std::lock_guard g(m);\n"
6207+
"}\n");
6208+
ASSERT_EQUALS("", errout.str());
62006209
}
62016210

62026211
void localVarClass() {
@@ -6478,6 +6487,11 @@ class TestUnusedVar : public TestFixture {
64786487
" std::list<std::list<int>>::value_type a{ 1, 2, 3, 4 };\n"
64796488
"}\n");
64806489
TODO_ASSERT_EQUALS("", "[test.cpp:2]: (information) --check-library: Provide <type-checks><unusedvar> configuration for std::list::value_type\n", errout.str());
6490+
6491+
functionVariableUsage("void f(int* p) {\n"
6492+
" int* q{ p };\n"
6493+
"}\n");
6494+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", errout.str());
64816495
}
64826496

64836497
void localvarRangeBasedFor() {

0 commit comments

Comments
 (0)