Fixed #8916 (FP: followVar does not seem to handle non-const method call properly)

This commit is contained in:
Daniel Marjamäki 2020-04-18 19:54:55 +02:00
parent de53f63f76
commit 54978847c5
2 changed files with 28 additions and 4 deletions

View File

@ -571,7 +571,9 @@ static const Token * followVariableExpression(const Token * tok, bool cpp, const
Token::Match(tok2, "%name% .|[|++|--|%assign%")) {
return tok;
}
if (Token::Match(tok2, "%name% ("))
// Bailout when function call is seen
return tok;
if (const Variable * var2 = tok2->variable()) {
if (!var2->scope())
return tok;

View File

@ -140,6 +140,7 @@ private:
TEST_CASE(duplicateExpression8);
TEST_CASE(duplicateExpression9); // #9320
TEST_CASE(duplicateExpression10); // #9485
TEST_CASE(duplicateExpression11); // #8916 (function call)
TEST_CASE(duplicateExpressionLoop);
TEST_CASE(duplicateValueTernary);
TEST_CASE(duplicateExpressionTernary); // #6391
@ -4530,13 +4531,13 @@ private:
" const int i = sizeof(int);\n"
" if ( i != sizeof (int)){}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value.\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value.\n", "", errout.str());
check("void f() {\n"
" const int i = sizeof(int);\n"
" if ( sizeof (int) != i){}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value.\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value.\n", "", errout.str());
check("void f(int a = 1) { if ( a != 1){}}\n");
ASSERT_EQUALS("", errout.str());
@ -4589,7 +4590,7 @@ private:
ASSERT_EQUALS("", errout.str());
check("const int a = 1;\n"
" void f() {\n"
"void f() {\n"
" if ( a != 1){} \n"
"}\n");
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:3]: (style) The comparison 'a != 1' is always false.\n", errout.str());
@ -4755,6 +4756,27 @@ private:
ASSERT_EQUALS("", errout.str());
}
void duplicateExpression11() {
check("class Fred {\n"
"public:\n"
" double getScale() const { return m_range * m_zoom; }\n"
" void setZoom(double z) { m_zoom = z; }\n"
" void dostuff(int);\n"
"private:\n"
" double m_zoom;\n"
" double m_range;\n"
"};\n"
"\n"
"void Fred::dostuff(int x) {\n"
" if (x == 43) {\n"
" double old_scale = getScale();\n"
" setZoom(m_zoom + 1);\n"
" double scale_ratio = getScale() / old_scale;\n" // <- FP
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void duplicateExpressionLoop() {
check("void f() {\n"
" int a = 1;\n"