Fix FP with duplicate assign (#1330)

This commit is contained in:
Paul Fultz II 2018-08-08 01:31:31 -05:00 committed by Daniel Marjamäki
parent c3a44ce56b
commit 80290a15e0
2 changed files with 33 additions and 0 deletions

View File

@ -1965,6 +1965,7 @@ void CheckOther::checkDuplicateExpression()
tok->next()->tokType() != Token::eName &&
isSameExpression(mTokenizer->isCPP(), true, tok->next(), nextAssign->next(), mSettings->library, true) &&
isSameExpression(mTokenizer->isCPP(), true, tok->astOperand2(), nextAssign->astOperand2(), mSettings->library, true) &&
tok->astOperand2()->expressionString() == nextAssign->astOperand2()->expressionString() &&
!isUniqueExpression(tok->astOperand2())) {
bool assigned = false;
const Scope * varScope = var1->scope() ? var1->scope() : &scope;

View File

@ -4249,6 +4249,23 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str());
check("int f() __attribute__((pure));\n"
"int g() __attribute__((pure));\n"
"void test() {\n"
" int i = f() + 1;\n"
" int j = 1 + f();\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f() __attribute__((pure));\n"
"int g() __attribute__((pure));\n"
"void test() {\n"
" int x = f();\n"
" int i = x + 1;\n"
" int j = f() + 1;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f() __attribute__((pure));\n"
"int g() __attribute__((pure));\n"
"void test() {\n"
@ -4265,6 +4282,15 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str());
check("int f(int) __attribute__((pure));\n"
"int g(int) __attribute__((pure));\n"
"void test() {\n"
" const int x = 0;\n"
" int i = f(0);\n"
" int j = f(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void test(int * p, int * q) {\n"
" int i = *p;\n"
" int j = *p;\n"
@ -4364,6 +4390,12 @@ private:
" int j = x--;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void test(int x) {\n"
" int i = x + 1;\n"
" int j = 1 + x;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void duplicateVarExpressionUnique() {