diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4bf6ddade..68186c60e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6314,7 +6314,7 @@ bool Tokenizer::simplifyKnownVariables() if (tok3->varId() == varid) { // Continue - tok2 = bailOutFromLoop; + //tok2 = bailOutFromLoop; break; } else if (tok3 == bailOutFromLoop) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 670f776fd..b81c698fc 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -125,6 +125,7 @@ private: TEST_CASE(simplifyKnownVariables34); TEST_CASE(simplifyKnownVariables35); // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;' TEST_CASE(simplifyKnownVariables36); // ticket #2304 - known value for strcpy parameter + TEST_CASE(simplifyKnownVariables37); // ticket #2398 - false positive caused by no simplification in for loop TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor2); @@ -1918,6 +1919,28 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + void simplifyKnownVariables37() + { + // Ticket #2398 - no simplication in for loop + const char code[] = "void f() {\n" + " double x = 0;\n" + " for (int iter=0; iter<42; iter++) {\n" + " int EvaldF = 1;\n" + " if (EvaldF)\n" + " Eval (x);\n" + " }\n" + "}"; + const char expected[] = "void f ( ) {\n" + "double x ; x = 0 ;\n" + "for ( int iter = 0 ; iter < 42 ; iter ++ ) {\n" + ";\n" + "{\n" + "Eval ( x ) ; }\n" + "}\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesBailOutAssign() { const char code[] = "int foo() {\n"