diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index e95f52196..3db8e0b31 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -543,6 +543,8 @@ static bool iscast(const Token *tok, bool cpp) for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->varId() != 0) return false; + if (cpp && !type && tok2->str() == "new") + return false; while (tok2->link() && Token::Match(tok2, "(|[|<")) tok2 = tok2->link()->next(); @@ -1620,7 +1622,8 @@ static Token * createAstAtToken(Token *tok, bool cpp) Token::Match(tok, "%name% %op%|(|[|.|::|<|?|;") || (cpp && Token::Match(tok, "%name% {") && iscpp11init(tok->next())) || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{") || - Token::Match(tok->previous(), "[;{}] %num%|%str%|%char%")) { + Token::Match(tok->previous(), "[;{}] %num%|%str%|%char%") || + Token::Match(tok->previous(), "[;{}] delete new")) { if (cpp && (Token::Match(tok->tokAt(-2), "[;{}] new|delete %name%") || Token::Match(tok->tokAt(-3), "[;{}] :: new|delete %name%"))) tok = tok->previous(); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index a7a6640b3..676a77955 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2666,6 +2666,17 @@ private: " Ref remove(new StringBuffer());\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // #11039 + " delete new int;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // #11327 + " int* p = (new int[3]) + 1;\n" + " delete[] &p[-1];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void smartPointerFunctionParam() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index fee6f894d..57843ca19 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6074,6 +6074,9 @@ private: ASSERT_EQUALS("a0[T{new=", testAst("a[0] = new T{};")); ASSERT_EQUALS("a0[T::{new=", testAst("a[0] = new ::T{};")); ASSERT_EQUALS("a0[ST::{new=", testAst("a[0] = new S::T{};")); + ASSERT_EQUALS("intnewdelete", testAst("delete new int;")); // #11039 + ASSERT_EQUALS("intnewdelete", testAst("void f() { delete new int; }")); + ASSERT_EQUALS("pint3[new1+=", testAst("p = (new int[3]) + 1;")); // #11327 // placement new ASSERT_EQUALS("X12,3,(new ab,c,", testAst("new (a,b,c) X(1,2,3);"));