diff --git a/lib/token.cpp b/lib/token.cpp index 5424ac3396e..4d1d2f5890f 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -554,7 +554,7 @@ int multiCompareImpl(const Token *tok, const char *haystack, nonneg int varid) const char *needle = tok->str().c_str(); const char *needlePointer = needle; for (;;) { - if (needlePointer == needle && haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ') { + if (needlePointer == needle && haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ' && haystack[1] != '=') { const int ret = multiComparePercent(tok, haystack, varid); if (ret < 2) return ret; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f7e057da58f..f83d288672f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -903,10 +903,10 @@ static void valueFlowSameExpressions(TokenList& tokenlist, const Settings& setti long long val; - if (Token::Match(tok, "==|>=|<=|/")) { + if (Token::Match(tok, "==|>=|<=|/|/=")) { val = 1; } - else if (Token::Match(tok, "!=|>|<|%|-")) { + else if (Token::Match(tok, "!=|>|<|%|%=|-|-=|^|^=")) { val = 0; } else diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 2b411324dc7..83a514eb963 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -64,6 +64,7 @@ class TestToken : public TestFixture { TEST_CASE(multiCompare3); // false positive for %or% on code using "|=" TEST_CASE(multiCompare4); TEST_CASE(multiCompare5); + TEST_CASE(multiCompare6); TEST_CASE(charTypes); TEST_CASE(stringTypes); TEST_CASE(getStrLength); @@ -322,6 +323,17 @@ class TestToken : public TestFixture { ASSERT_EQUALS(true, TokenTest::multiCompare(&tok, "+|%or%|%oror%", 0) >= 0); } + void multiCompare6() const { + { + const SimpleTokenList stl("x %= y;"); + ASSERT_EQUALS(true, Token::Match(stl.front(), "%name% %= %name%")); + } + { + const SimpleTokenList stl("x += y;"); + ASSERT_EQUALS(false, Token::Match(stl.front(), "%name% %= %name%")); + } + } + void charTypes() const { auto tokensFrontBack = std::make_shared(); Token tok(list, std::move(tokensFrontBack)); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 709be2a204f..2d003e41a09 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -5805,6 +5805,36 @@ class TestValueFlow : public TestFixture { " bool b = x;\n" "}\n"; ASSERT_EQUALS(false, testValueOfX(code, 3U, 1)); + + code = "int f(int a) {\n" + " int x = a ^ a;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); + + code = "int f(int a) {\n" + " int x = a /= a;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); + + code = "int f(int a) {\n" + " int x = a -= a;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); + + code = "int f(int a) {\n" + " int x = a %= a;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); + + code = "int f(int a) {\n" + " int x = a ^= a;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); } void valueFlowUninit() {