diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f72756d6e..be8b8d310 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2234,7 +2234,7 @@ void CheckOther::nullConstantDereference() else if (Token::simpleMatch(tok, "* 0")) { - if (Token::Match(tok->previous(), "[;{}=+-/(,]") || + if (Token::Match(tok->previous(), "[<>;{}=+-*/(,]") || Token::Match(tok->previous(), "return|<<")) { nullPointerError(tok); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fd01220ba..50879c00e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5521,10 +5521,11 @@ bool Tokenizer::simplifyKnownVariables() } // Variable is used in calculation.. - if (Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;]]", varid) || + if (Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;])]", varid) || Token::Match(tok3, "[(=+-*/[] %varid% <<|>>", varid) || Token::Match(tok3, "<< %varid% [+-*/;])]", varid) || - Token::Match(tok3, ">> %varid% [+-*/])]", varid)) + Token::Match(tok3, ">> %varid% [+-*/])]", varid) || + Token::Match(tok3->previous(), "[=+-*/[] ( %varid%", varid)) { tok3 = tok3->next(); tok3->str(value); diff --git a/test/testother.cpp b/test/testother.cpp index 5da1aed1f..6ef9eca19 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1050,7 +1050,7 @@ private: " int *p = 0;\n" " if (3 > *p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); checkNullPointer("void f()\n" "{\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 0e75a3058..72ef45dca 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -109,6 +109,7 @@ private: TEST_CASE(simplifyKnownVariables24); TEST_CASE(simplifyKnownVariables25); TEST_CASE(simplifyKnownVariables26); + TEST_CASE(simplifyKnownVariables27); TEST_CASE(match1); @@ -1316,6 +1317,24 @@ private: simplifyKnownVariables(code)); } + void simplifyKnownVariables27() + { + // This testcase is related to ticket #1633 + const char code[] = "void foo()\n" + "{\n" + " int i1 = 1;\n" + " int i2 = 2;\n" + " int i3 = (i1 + i2) * 3;\n" + "}\n"; + ASSERT_EQUALS( + "void foo ( ) " + "{" + " int i1 ; i1 = 1 ;" + " int i2 ; i2 = 2 ;" + " int i3 ; i3 = ( 1 + 2 ) * 3 ; " + "}", + simplifyKnownVariables(code)); + } void match1() {