From 176eefcbf31bcb4d1e47d1fc9d178e976c8c2fc3 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jun 2022 23:08:17 +0200 Subject: [PATCH] Fix #10655 FN bitwiseOnBoolean with unseen function (#4214) --- lib/checkbool.cpp | 3 +-- test/testbool.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index ef63ce9f1..a069cc452 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -104,8 +104,7 @@ void CheckBool::checkBitwiseOnBoolean() continue; if (tok->str() == "|" && !isConvertedToBool(tok) && !(astIsBool(tok->astOperand1()) && astIsBool(tok->astOperand2()))) continue; - if (!isConstExpression(tok->astOperand1(), mSettings->library, true, mTokenizer->isCPP())) - continue; + // first operand will always be evaluated if (!isConstExpression(tok->astOperand2(), mSettings->library, true, mTokenizer->isCPP())) continue; if (tok->astOperand2()->variable() && tok->astOperand2()->variable()->nameToken() == tok->astOperand2()) diff --git a/test/testbool.cpp b/test/testbool.cpp index 775bac519..9a69d6c95 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -914,6 +914,24 @@ private: " return ((p - xm >= d) << 1) | (x - p > d);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("int g();\n" // #10655 + "void f(bool b) {\n" + " if (g() | b) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. Did you mean '||'?\n", errout.str()); + + check("int g();\n" + "void f(bool b) {\n" + " if (b | g()) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("int g();\n" + "bool f(bool b, bool c) {\n" + " return b | g() | c;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Boolean expression 'c' is used in bitwise operation. Did you mean '||'?\n", errout.str()); } void incrementBoolean() {