diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d210d49c0..c6f11f1ba 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -490,7 +490,8 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger, bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + " is assigned in conditional code"); break; } - if (Token::findmatch(start, "++|--| %varid% ++|--|=", end, varid)) { + if (Token::findmatch(start, "++|-- %varid%", end, varid) || + Token::findmatch(start, "%varid% ++|--|=", end, varid)) { if (number_of_if == 0 && Token::simpleMatch(tok2, "if (") && !(Token::simpleMatch(end, "} else {") && @@ -517,6 +518,13 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger, break; } + // bailout increment/decrement for now.. + if (Token::Match(tok2->previous(), "++|-- %var%") || Token::Match(tok, "%var% ++|--")) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, tok2, "increment/decrement of " + tok2->str()); + break; + } + // skip if variable is conditionally used in ?: expression if (const Token *parent = skipValueInConditionalExpression(tok2)) { if (settings->debugwarnings) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 8b42e762e..03e268373 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -477,6 +477,13 @@ private: "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 123)); + code = "void f() {\n" + " int x = 9;\n" + " --x;\n" + " return x;\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 4U, 9)); + // function code = "void f() {\n" " char *x = 0;\n" @@ -508,6 +515,16 @@ private: "}"; ASSERT_EQUALS(false, testValueOfX(code, 4U, 123)); + code = "void f(int a) {\n" + " int x = 123;\n" + " if (a > 1)\n" + " ++x;\n" + " else\n" + " ++x;\n" + " return 2 + x;\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 4U, 123)); + code = "void f() {\n" " int x = 1;\n" " if (condition1) x = 2;\n"