Fixed #2085 (False negative: function can be const when member variable is compared)

This commit is contained in:
Robert Reif 2010-10-12 07:57:09 +02:00 committed by Daniel Marjamäki
parent 2943bf0f70
commit 907ed0ac6f
2 changed files with 33 additions and 1 deletions

View File

@ -2200,7 +2200,8 @@ bool CheckClass::checkConstFunc(const SpaceInfo *info, const Token *tok)
}
// function call..
else if ((Token::Match(tok1, "%var% (") && !Token::Match(tok1, "return|c_str|if|string")) ||
else if ((Token::Match(tok1, "%var% (") &&
!(Token::Match(tok1, "return|c_str|if|string") || tok1->isStandardType())) ||
Token::Match(tok1, "%var% < %any% > ("))
{
isconst = false;

View File

@ -144,6 +144,7 @@ private:
TEST_CASE(const34); // ticket #1964
TEST_CASE(const35); // ticket #2001
TEST_CASE(const36); // ticket #2003
TEST_CASE(const37); // ticket #2081 and #2085
TEST_CASE(constoperator1); // operator< can often be const
TEST_CASE(constoperator2); // operator<<
TEST_CASE(constoperator3);
@ -4046,6 +4047,36 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const37() // ticket #2081 and #2085
{
checkConst("class A\n"
"{\n"
"public:\n"
" A(){};\n"
" std::string operator+(const char *c)\n"
" {\n"
" return m_str+std::string(c);\n"
" }\n"
"private:\n"
" std::string m_str;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (style) The function 'A::operator+' can be const\n", errout.str());
checkConst("class Fred\n"
"{\n"
"private:\n"
" long x;\n"
"public:\n"
" Fred() {\n"
" x = 0;\n"
" }\n"
" bool isValid() {\n"
" return bool(x == 0x11224488);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (style) The function 'Fred::isValid' can be const\n", errout.str());
}
// increment/decrement => not const
void constincdec()
{