diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index db8789593..4af3466a3 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -1270,6 +1270,30 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } + // Reduce "} else .." => "} if .." + if (Token::simpleMatch(tok2, "} else")) + { + int indentlevel = 0; + for (const Token *tok3 = tok2; tok3; tok3 = tok3->previous()) + { + if (tok3->str() == "{") + { + --indentlevel; + if (indentlevel == 0) + { + if (tok3->previous()->str() == "if") + tok2->next()->str("if"); + } + if (indentlevel <= 0) + break; + } + else if (tok3->str() == "}") + { + ++indentlevel; + } + } + } + // Remove "catch ;" if (Token::simpleMatch(tok2->next(), "catch ;")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index b9f8e172d..6da611d44 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -444,11 +444,15 @@ private: ASSERT_EQUALS("; ifv xxx ;", simplifycode("; ifv ; else xxx ;")); { - const char code[] = "; alloc ; if { dealloc ; return ; }"; - ASSERT_EQUALS(code, simplifycode(code)); - ASSERT_EQUALS("; alloc ;", simplifycode(code, true)); + const char code1[] = "; alloc ; if { dealloc ; return ; }"; + ASSERT_EQUALS(code1, simplifycode(code1)); + ASSERT_EQUALS("; alloc ;", simplifycode(code1, true)); + + const char code2[] = "; alloc ; if { dealloc ; return ; } else { return ; }"; + ASSERT_EQUALS("; alloc ; if return ;", simplifycode(code2)); } + // switch.. ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;"));