Fixed false negatives in CheckOther::checkPassByReference() when assignment operator is used.

This commit is contained in:
PKEuS 2017-03-07 21:49:28 +01:00
parent 3da1de7893
commit 00904ba32a
2 changed files with 29 additions and 1 deletions

View File

@ -1509,7 +1509,15 @@ void CheckOther::checkPassByReference()
}
} else if (parent->isConstOp())
;
else if (Token::Match(tok2, "%var% . %name% (")) {
else if (parent->isAssignmentOp()) {
if (parent->astOperand1() == tok2)
isConst = false;
else if (parent->astOperand1()->str() == "&") {
const Variable* assignedVar = parent->previous()->variable();
if (!assignedVar || !assignedVar->isConst())
isConst = false;
}
} else if (Token::Match(tok2, "%var% . %name% (")) {
const Function* func = tok2->tokAt(2)->function();
if (func && (func->isConst() || func->isStatic()))
;

View File

@ -1435,6 +1435,26 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("void f(std::string str) {\n"
" std::string s2 = str;\n"
"}");
ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by reference.\n", errout.str());
check("void f(std::string str) {\n"
" std::string& s2 = str;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(std::string str) {\n"
" const std::string& s2 = str;\n"
"}");
ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by reference.\n", errout.str());
check("void f(std::string str) {\n"
" str = \"\";\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(std::string str) {\n"
" foo(str);\n" // It could be that foo takes str as non-const-reference
"}");