value flow: added bailout for conditional return/continue/break when number_of_if is bigger than 0

This commit is contained in:
Daniel Marjamäki 2014-01-26 15:50:25 +01:00
parent 7e43498bcc
commit be082a8e4a
2 changed files with 18 additions and 0 deletions

View File

@ -504,6 +504,16 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
++it; ++it;
} }
} }
// noreturn scopes..
if (number_of_if > 0 &&
(Token::findmatch(start, "return|continue|break", end) ||
(Token::Match(end,"} else {") && Token::findmatch(end, "return|continue|break", end->linkAt(2))))) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + ". noreturn conditional scope.");
break;
}
if (Token::findmatch(start, "++|-- %varid%", end, varid) || if (Token::findmatch(start, "++|-- %varid%", end, varid) ||
Token::findmatch(start, "%varid% ++|--|=", end, varid)) { Token::findmatch(start, "%varid% ++|--|=", end, varid)) {
if (number_of_if == 0 && if (number_of_if == 0 &&

View File

@ -547,6 +547,14 @@ private:
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 5U, 1)); ASSERT_EQUALS(false, testValueOfX(code, 5U, 1));
code = "void f(){\n"
" int x = 0;\n"
" if (a>=0) { x = getx(); }\n"
" if (x==0) { return; }\n"
" return 123 / x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));
// multivariables // multivariables
code = "void f(int a) {\n" code = "void f(int a) {\n"
" int x = a;\n" " int x = a;\n"