Fixed false negatives in CheckOther::checkPassByReference() when assignment operator is used.
This commit is contained in:
parent
3da1de7893
commit
00904ba32a
|
@ -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()))
|
||||
;
|
||||
|
|
|
@ -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
|
||||
"}");
|
||||
|
|
Loading…
Reference in New Issue