diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 79697a2e6d7..7ec517c8cca 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -4383,7 +4383,7 @@ void CheckOtherImpl::checkKnownArgument() for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) { if (!tok->hasKnownIntValue() || tok->isExpandedMacro()) continue; - if (Token::Match(tok, "++|--|%assign%")) + if (Token::Match(tok, "++|--|%assign%|%comp%")) continue; if (!Token::Match(tok->astParent(), "(|{|,")) continue; @@ -4408,17 +4408,16 @@ void CheckOtherImpl::checkKnownArgument() tok2 = tok2->astOperand2(); if (isVariableExpression(tok2)) continue; - if (tok->isComparisonOp() && - isSameExpression( - true, tok->astOperand1(), tok->astOperand2(), mSettings, true, true)) - continue; // ensure that there is a integer variable in expression with unknown value const Token* vartok = nullptr; visitAstNodes(tok, [&](const Token* child) { if (Token::Match(child, "%var%|.|[")) { if (child->hasKnownIntValue()) return ChildrenToVisit::none; - if (astIsIntegral(child, false) && !astIsPointer(child) && child->values().empty()) { + if (astIsIntegral(child, false) && !astIsPointer(child) && + std::none_of(child->values().begin(), child->values().end(), [](const ValueFlow::Value& v) { + return v.isIntValue() && !v.isImpossible(); + })) { vartok = child; return ChildrenToVisit::done; } diff --git a/test/testother.cpp b/test/testother.cpp index adfc2ff0f2a..112e56e13c0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -13888,6 +13888,19 @@ class TestOther : public TestFixture { "[test.cpp:5:18]: (style) Argument 'true||x' to function dostuff is always 1. Constant literal calculation disable/hide variable expression 'x'. [knownArgumentHiddenVariableExpression]\n" "[test.cpp:6:15]: (style) Argument 'x*0' to function dostuff is always 0. Constant literal calculation disable/hide variable expression 'x'. [knownArgumentHiddenVariableExpression]\n" "[test.cpp:7:15]: (style) Argument '0*x' to function dostuff is always 0. Constant literal calculation disable/hide variable expression 'x'. [knownArgumentHiddenVariableExpression]\n", errout_str()); + + check("void f1(int x) {\n" // #15145 + " if (x != 0)\n" + " g(0 * x);\n" + "}\n" + "void f2(unsigned x) {\n" + " g(x >= 0);\n" + " if (x != 5)\n" + " g(x == 5);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6:9]: (style) Unsigned expression 'x' can't be negative so it is unnecessary to test it. [unsignedPositive]\n" + "[test.cpp:3:13]: (style) Argument '0*x' to function g is always 0. Constant literal calculation disable/hide variable expression 'x'. [knownArgumentHiddenVariableExpression]\n", + errout_str()); } void knownArgumentTernaryOperator() { // #10374