AssignIf: better handling of function calls

This commit is contained in:
Daniel Marjamäki 2012-09-29 19:22:34 +02:00
parent 0024310efe
commit 4e1bef5535
2 changed files with 17 additions and 0 deletions

View File

@ -76,6 +76,8 @@ bool CheckAssignIf::assignIfParseScope(const Token * const assignTok,
return true;
if (tok2->str() == "}")
return false;
if (!islocal && Token::Match(tok2, "%var% (") && !Token::simpleMatch(tok2->next()->link(), ") {"))
return true;
if (Token::Match(tok2, "if|while (")) {
if (!islocal && tok2->str() == "while")
continue;

View File

@ -126,6 +126,21 @@ private:
" while (y==8);\n" // non-local variable => no error
"}");
ASSERT_EQUALS("", errout.str());
// calling function
check("void f(int x) {\n"
" int y = x & 7;\n"
" do_something();\n"
" if (y==8);\n" // local variable => always false
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'y==8' is always false.\n", errout.str());
check("void f(int x) {\n"
" extern int y; y = x & 7;\n"
" do_something();\n"
" if (y==8);\n" // non-local variable => no error
"}");
ASSERT_EQUALS("", errout.str());
}
void compare() {