Avoid clarifyCalculation warning for 'x % 16 ? 1 : 0' expression. Fixes FP seen in daca@home. It seems likely that the order is understood otherwise the ternary calculation could easily be simplified away.

This commit is contained in:
Daniel Marjamäki 2020-09-29 08:39:21 +02:00
parent e32ccb591b
commit d901edd4af
2 changed files with 7 additions and 4 deletions

View File

@ -168,8 +168,8 @@ void CheckOther::clarifyCalculation()
if (tok->astOperand1()->tokType() == Token::eBitOp && Token::Match(tok->astOperand2()->astOperand1(), "%char%") && Token::Match(tok->astOperand2()->astOperand2(), "%char%"))
continue;
// bitand operation in lhs with known integer value => probably no mistake
if (tok->astOperand1()->str() == "&" && tok->astOperand1()->isBinaryOp()) {
// 2nd operand in lhs has known integer value => probably no mistake
if (tok->astOperand1()->isBinaryOp() && tok->astOperand1()->astOperand2()->hasKnownIntValue()) {
const Token *op = tok->astOperand1()->astOperand2();
if (op->isNumber())
continue;

View File

@ -4236,12 +4236,12 @@ private:
ASSERT_EQUALS("", errout.str());
check("void f(char c) {\n"
" printf(\"%i\", 1 + 1 ? 1 : 2);\n" // "1+1" is simplified away
" printf(\"%i\", a + b ? 1 : 2);\n"
"}",nullptr,false,false,false);
ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for '+' and '?'.\n", errout.str());
check("void f() {\n"
" std::cout << x << 1 ? 2 : 3;\n"
" std::cout << x << y ? 2 : 3;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for '<<' and '?'.\n", errout.str());
@ -4281,6 +4281,9 @@ private:
check("void f(int x) { return x & 16 ? 1 : 0; }");
ASSERT_EQUALS("", errout.str());
check("void f(int x) { return x % 16 ? 1 : 0; }");
ASSERT_EQUALS("", errout.str());
check("enum {X,Y}; void f(int x) { return x & Y ? 1 : 0; }");
ASSERT_EQUALS("", errout.str());
}