diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index ed4fd69a375..7946f337f71 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -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(); @@ -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; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index fcdacb425ab..6bccad45f18 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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() { @@ -6478,6 +6487,11 @@ class TestUnusedVar : public TestFixture { " std::list>::value_type a{ 1, 2, 3, 4 };\n" "}\n"); TODO_ASSERT_EQUALS("", "[test.cpp:2]: (information) --check-library: Provide 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() {