Detect referenced by value errors for std::map also.

This commit is contained in:
Reijo Tomperi 2009-10-07 00:04:54 +03:00
parent ab762b1a00
commit d9ae2c171e
2 changed files with 42 additions and 38 deletions

View File

@ -561,6 +561,26 @@ void CheckOther::checkConstantFunctionParameter()
passedByValueError(tok, tok->strAt(10));
}
else if (Token::Match(tok, "[,(] const std :: %type% < std :: %type% , std :: %type% > %var% [,)]"))
{
passedByValueError(tok, tok->strAt(14));
}
else if (Token::Match(tok, "[,(] const std :: %type% < %type% , std :: %type% > %var% [,)]"))
{
passedByValueError(tok, tok->strAt(12));
}
else if (Token::Match(tok, "[,(] const std :: %type% < std :: %type% , %type% > %var% [,)]"))
{
passedByValueError(tok, tok->strAt(12));
}
else if (Token::Match(tok, "[,(] const std :: %type% < %type% , %type% > %var% [,)]"))
{
passedByValueError(tok, tok->strAt(10));
}
else if (Token::Match(tok, "[,(] const %type% %var% [,)]"))
{
// Check if type is a struct or class.

View File

@ -1019,54 +1019,38 @@ private:
void passedByValue()
{
{
testPassedByValue("void f(const std::string str)\n"
"{\n"
"}\n");
testPassedByValue("void f(const std::string str) {}");
ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'str' is passed by value. It could be passed by reference instead.\n", errout.str());
}
{
testPassedByValue("class Foo;\n"
"void f(const Foo foo)\n"
"{\n"
"}\n");
testPassedByValue("class Foo;\nvoid f(const Foo foo) {}");
ASSERT_EQUALS("[test.cpp:2]: (style) Function parameter 'foo' is passed by value. It could be passed by reference instead.\n", errout.str());
}
{
testPassedByValue("void f(const std::string &str)\n"
"{\n"
"}\n");
testPassedByValue("void f(const std::string &str) {}");
ASSERT_EQUALS("", errout.str());
}
{
testPassedByValue("void f(const std::vector<int> v)\n"
"{\n"
"}\n");
testPassedByValue("void f(const std::vector<int> v) {}");
ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str());
}
{
testPassedByValue("void f(const std::vector<std::string> v)\n"
"{\n"
"}\n");
testPassedByValue("void f(const std::vector<std::string> v) {}");
ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str());
}
{
testPassedByValue("void f(const std::vector<int> &v)\n"
"{\n"
"}\n");
testPassedByValue("void f(const std::vector<int> &v) {}");
ASSERT_EQUALS("", errout.str());
}
testPassedByValue("void f(const std::map<int,int> &v) {}");
ASSERT_EQUALS("", errout.str());
testPassedByValue("void f(const std::map<int,int> v) {}");
ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str());
testPassedByValue("void f(const std::map<std::string,std::string> v) {}");
ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str());
testPassedByValue("void f(const std::map<int,std::string> v) {}");
ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str());
testPassedByValue("void f(const std::map<std::string,int> v) {}");
ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str());
}
};