diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ee6735f82..eb317f750 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -504,6 +504,16 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger, ++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) || Token::findmatch(start, "%varid% ++|--|=", end, varid)) { if (number_of_if == 0 && diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 6eccb8d20..382d22e38 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -547,6 +547,14 @@ private: "}"; 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 code = "void f(int a) {\n" " int x = a;\n"