From 51c8630bb37c01251d43501e85cb3217471d0b0a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 10 Jun 2022 20:17:57 +0200 Subject: [PATCH] Fix #10618 FP knownConditionTrueFalse with virtual function (#4194) * Fix #10618 FP knownConditionTrueFalse with virtual function * Remove redundant check --- lib/astutils.cpp | 4 ++-- test/testcondition.cpp | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e50d6e86e..4f31f75a7 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -870,9 +870,9 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth) // Abort recursion to avoid stack overflow return true; ++depth; + // calling nonstatic method? - if (Token::Match(expr->previous(), "!!:: %name% (") && expr->function() && expr->function()->nestedIn && - expr->function()->nestedIn->isClassOrStruct()) { + if (Token::Match(expr, "%name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct() && !expr->function()->isStatic()) { // is it a method of this? const Scope* fScope = expr->scope(); while (!fScope->functionOf && fScope->nestedIn) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index c73a04040..de2727299 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4196,6 +4196,29 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (warning) Logical conjunction always evaluates to false: s.bar(1) == 0 && s.bar(1) > 0.\n", errout.str()); + + check("struct B {\n" // #10618 + " void Modify();\n" + " static void Static();\n" + " virtual void CalledByModify();\n" + "};\n" + "struct D : B {\n" + " int i{};\n" + " void testV();\n" + " void testS();\n" + " void CalledByModify() override { i = 0; }\n" + "};\n" + "void D::testV() {\n" + " i = 1;\n" + " B::Modify();\n" + " if (i == 1) {}\n" + "}\n" + "void D::testS() {\n" + " i = 1;\n" + " B::Static();\n" + " if (i == 1) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:20]: (style) Condition 'i==1' is always true\n", errout.str()); } void alwaysTrueSymbolic()