Fix #9946 FP: unassignedVariable when operator() is called subsequently (#4303)

This commit is contained in:
chrchr-github 2022-07-24 17:52:14 +02:00 committed by GitHub
parent 479533a80a
commit 15f8c71ec3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View File

@ -1078,6 +1078,9 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
// function
else if (Token::Match(tok, "%name% (")) {
if (tok->varId() && !tok->function()) // operator()
variables.use(tok->varId(), tok);
else
variables.read(tok->varId(), tok);
useFunctionArgs(tok->next()->astOperand2(), variables);
} else if (Token::Match(tok, "std :: ref ( %var% )")) {

View File

@ -136,6 +136,7 @@ private:
TEST_CASE(localvar64); // #9997
TEST_CASE(localvar65); // #9876, #10006
TEST_CASE(localvar66); // #11143
TEST_CASE(localvar67); // #9946
TEST_CASE(localvarloops); // loops
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
@ -3612,6 +3613,28 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvar67() { // #9946
functionVariableUsage("struct B {\n"
" virtual ~B() {}\n"
" bool operator() () const { return true; }\n"
" virtual bool f() const = 0;\n"
"};\n"
"class D : B {\n"
"public:\n"
" bool f() const override { return false; }\n"
"};\n"
"void f1() {\n"
" const D d1;\n"
" d1.f();\n"
"}\n"
"void f2() {\n"
" const D d2;\n"
" d2();\n"
" B() {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvarloops() {
// loops
functionVariableUsage("void fun(int c) {\n"

View File

@ -4520,8 +4520,8 @@ private:
code = "struct base {\n"
" virtual int f() { return 0; }\n"
"};\n"
"struct derived {\n"
" virtual int f() { return 1; }\n"
"struct derived : base {\n"
" int f() override { return 1; }\n"
"};\n"
"void g(derived* d) {\n"
" base* b = d;\n"