From d901edd4afe5b8192cb52647b04cca5de8377d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 Sep 2020 08:39:21 +0200 Subject: [PATCH] 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. --- lib/checkother.cpp | 4 ++-- test/testother.cpp | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index e042b3729..7f2346493 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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; diff --git a/test/testother.cpp b/test/testother.cpp index f696b792b..fd58055de 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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()); }