diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 4bc3bc799..6270aa5e9 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1448,6 +1448,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) continue; } + // Reduce "alloc loop !var alloc ;" => "alloc ;" + if (Token::Match(tok2, "[;{}] alloc ; loop !var alloc ;")) + { + Token::eraseTokens(tok2, tok2->tokAt(5)); + done = false; + } + // Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else.. if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index e660c15d0..0fb93508b 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -555,6 +555,12 @@ private: Token::eraseTokens(tok, tok->tokAt(5)); tok->str("if(!var)"); } + + else if (Token::simpleMatch(tok, "! var")) + { + tok->deleteNext(); + tok->str("!var"); + } } Settings settings; @@ -635,6 +641,8 @@ private: ASSERT_EQUALS("; alloc ; alloc ;", simplifycode("; alloc ; do { alloc ; } loop ;")); ASSERT_EQUALS("; exit ;", simplifycode("; alloc ; do { } loop ; exit ;")); + ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; loop !var alloc ;")); + ASSERT_EQUALS("; alloc ; dealloc ; return ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } }")); ASSERT_EQUALS("; alloc ; dealloc ; return ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } if { continue ; } }")); ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } if { break ; } }"));