Skip to content

Commit

Permalink
checkLibraryCheckType: handle global scope operator / Fix FN unreadVa…
Browse files Browse the repository at this point in the history
…riable (#5199)
  • Loading branch information
chrchr-github committed Jun 27, 2023
1 parent 977e132 commit e063656
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
continue;
tok = tok->next();
}
if (tok->astParent() && !tok->astParent()->isAssignmentOp() && tok->str() != "(") {
if (!isInitialization && tok->astParent() && !tok->astParent()->isAssignmentOp() && tok->str() != "(") {
const Token *parent = tok->astParent();
while (Token::Match(parent, "%oror%|%comp%|!|&&"))
parent = parent->astParent();
Expand Down Expand Up @@ -1268,7 +1268,9 @@ void CheckUnusedVar::checkFunctionVariableUsage()
op1Var->isClass() &&
(!op1Var->valueType() || op1Var->valueType()->type == ValueType::Type::UNKNOWN_TYPE)) {
// Check in the library if we should bailout or not..
const std::string typeName = op1Var->getTypeName();
std::string typeName = op1Var->getTypeName();
if (typeName.compare(0, 2, "::") == 0)
typeName.erase(typeName.begin(), typeName.begin() + 2);
switch (mSettings->library.getTypeCheck("unusedvar", typeName)) {
case Library::TypeCheck::def:
bailoutTypeName = typeName;
Expand Down
14 changes: 14 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6197,6 +6197,15 @@ class TestUnusedVar : public TestFixture {
" s[0] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("struct S {\n"
" std::mutex m;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" const ::std::lock_guard g(m);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void localVarClass() {
Expand Down Expand Up @@ -6478,6 +6487,11 @@ class TestUnusedVar : public TestFixture {
" std::list<std::list<int>>::value_type a{ 1, 2, 3, 4 };\n"
"}\n");
TODO_ASSERT_EQUALS("", "[test.cpp:2]: (information) --check-library: Provide <type-checks><unusedvar> configuration for std::list::value_type\n", errout.str());

functionVariableUsage("void f(int* p) {\n"
" int* q{ p };\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'q' is assigned a value that is never used.\n", errout.str());
}

void localvarRangeBasedFor() {
Expand Down

0 comments on commit e063656

Please sign in to comment.