From 7e2fcea3d9b131762c7e24e5f9153e952778c729 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 23 Oct 2023 11:18:05 +0200 Subject: [PATCH] Fix #12044 FN knownConditionTrueFalse comparing qualified constant with number (#5518) --- lib/astutils.cpp | 4 ++++ test/testother.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e735a078f..a66bcf1bc 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1506,8 +1506,12 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 if (cpp) { if (tok1->str() == "." && tok1->astOperand1() && tok1->astOperand1()->str() == "this") tok1 = tok1->astOperand2(); + while (Token::simpleMatch(tok1, "::") && tok1->astOperand2()) + tok1 = tok1->astOperand2(); if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this") tok2 = tok2->astOperand2(); + while (Token::simpleMatch(tok2, "::") && tok2->astOperand2()) + tok2 = tok2->astOperand2(); } // Skip double not if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=") && astIsBoolLike(tok2)) { diff --git a/test/testother.cpp b/test/testother.cpp index b3d0fc0d2..6715ce90b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6875,6 +6875,31 @@ private: "[test.cpp:4]: (style) The comparison 'E0 > 0' is always false.\n" "[test.cpp:5]: (style) The comparison 'E0 == 0' is always true.\n", errout.str()); + + check("struct S {\n" // #12040, #12044 + " static const int I = 0;\n" + " enum { E0 };\n" + " enum F { F0 };\n" + " void f() {\n" + " if (0 > I) {}\n" + " if (0 > S::I) {}\n" + " if (0 > E0) {}\n" + " if (0 > S::E0) {}\n" + " }\n" + "};\n" + "void g() {\n" + " if (0 > S::I) {}\n" + " if (0 > S::E0) {}\n" + " if (0 > S::F::F0) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:6]: (style) The comparison '0 > I' is always false.\n" + "[test.cpp:2] -> [test.cpp:7]: (style) The comparison '0 > S::I' is always false.\n" + "[test.cpp:8]: (style) The comparison '0 > E0' is always false.\n" + "[test.cpp:9]: (style) The comparison '0 > S::E0' is always false.\n" + "[test.cpp:2] -> [test.cpp:13]: (style) The comparison '0 > S::I' is always false.\n" + "[test.cpp:14]: (style) The comparison '0 > S::E0' is always false.\n" + "[test.cpp:15]: (style) The comparison '0 > S::F::F0' is always false.\n", + errout.str()); } void duplicateExpressionLoop() {