From 15f8c71ec335920e0b083240407020343f907330 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 24 Jul 2022 17:52:14 +0200 Subject: [PATCH] Fix #9946 FP: unassignedVariable when operator() is called subsequently (#4303) --- lib/checkunusedvar.cpp | 5 ++++- test/testunusedvar.cpp | 23 +++++++++++++++++++++++ test/testvalueflow.cpp | 4 ++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 630ad66d8..e0f16d3cd 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1078,7 +1078,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // function else if (Token::Match(tok, "%name% (")) { - variables.read(tok->varId(), tok); + 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% )")) { variables.eraseAll(tok->tokAt(4)->varId()); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index eaaedfbd4..a97bc3980 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 4bd7b27c5..f60a12b90 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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"