diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index 18fbfc252..7c905a3fc 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -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; diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 7ef661098..f0a85e064 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -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() {