Fix FP when using non-const members and also check return type of function (#1283)

This commit is contained in:
Paul Fultz II 2018-06-09 01:05:19 -05:00 committed by Daniel Marjamäki
parent d9f9bdda73
commit 8d53bcfd63
3 changed files with 30 additions and 5 deletions

View File

@ -433,8 +433,13 @@ bool isUniqueExpression(const Token* tok)
const Scope * scope = fun->nestedIn;
if (!scope)
return true;
std::string returnType = fun->retType ? fun->retType->name() : fun->retDef->stringifyList(fun->tokenDef);
for (const Function& f:scope->functionList) {
if (f.argumentList.size() == fun->argumentList.size() && f.name() != fun->name()) {
std::string freturnType = f.retType ? f.retType->name() : f.retDef->stringifyList(f.tokenDef);
if (f.argumentList.size() == fun->argumentList.size() &&
f.type == Function::eFunction &&
returnType == freturnType &&
f.name() != fun->name()) {
return false;
}
}

View File

@ -1947,7 +1947,7 @@ void CheckOther::checkDuplicateExpression()
tok->next()->tokType() != Token::eType &&
tok->next()->tokType() != Token::eName &&
isSameExpression(_tokenizer->isCPP(), true, tok->next(), nextAssign->next(), _settings->library, true) &&
isSameExpression(_tokenizer->isCPP(), true, tok->astOperand2(), nextAssign->astOperand2(), _settings->library, false) &&
isSameExpression(_tokenizer->isCPP(), true, tok->astOperand2(), nextAssign->astOperand2(), _settings->library, true) &&
!isUniqueExpression(tok->astOperand2())) {
duplicateAssignExpressionError(var1, var2);
}

View File

@ -3980,11 +3980,12 @@ private:
void duplicateVarExpression() {
check("int f() __attribute__((pure));\n"
"int g() __attribute__((pure));\n"
"void test() {\n"
" int i = f();\n"
" int j = f();\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str());
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str());
check("struct Foo { int f() const; int g() const; };\n"
"void test() {\n"
@ -4004,18 +4005,20 @@ private:
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:5]: (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 = 1 + f();\n"
" int j = 1 + f();\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str());
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() + f();\n"
" int j = f() + f();\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str());
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"
@ -4058,6 +4061,7 @@ private:
ASSERT_EQUALS("", errout.str());
check("int f();\n"
"int g();\n"
"void test() {\n"
" int i = f() || f();\n"
" int j = f() && f();\n"
@ -4078,6 +4082,22 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("struct Foo { int f() const; float g() const; };\n"
"void test() {\n"
" Foo f = Foo{};\n"
" int i = f.f();\n"
" int j = f.f();\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct Foo { int f(); int g(); };\n"
"void test() {\n"
" Foo f = Foo{};\n"
" int i = f.f();\n"
" int j = f.f();\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void test() {\n"
" int i = f();\n"
" int j = f();\n"