Fixed #1882 (false negative: function can be declared const)

This commit is contained in:
Robert Reif 2010-07-19 08:40:46 +02:00 committed by Daniel Marjamäki
parent 7842658cb4
commit 37b37218cf
2 changed files with 22 additions and 1 deletions

View File

@ -2329,7 +2329,7 @@ bool CheckClass::checkConstFunc(const std::string &classname, const Var *varlist
} }
// function call.. // function call..
else if ((tok1->str() != "return" && Token::Match(tok1, "%var% (") && tok1->str() != "c_str") || else if ((Token::Match(tok1, "%var% (") && !Token::Match(tok1, "return|c_str|if")) ||
Token::Match(tok1, "%var% < %any% > (")) Token::Match(tok1, "%var% < %any% > ("))
{ {
isconst = false; isconst = false;

View File

@ -129,6 +129,7 @@ private:
TEST_CASE(const24); // ticket #1708 TEST_CASE(const24); // ticket #1708
TEST_CASE(const25); // ticket #1724 TEST_CASE(const25); // ticket #1724
TEST_CASE(const26); // ticket #1847 TEST_CASE(const26); // ticket #1847
TEST_CASE(const27); // ticket #1882
TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator1); // operator< can often be const
TEST_CASE(constoperator2); // operator<< TEST_CASE(constoperator2); // operator<<
TEST_CASE(constincdec); // increment/decrement => non-const TEST_CASE(constincdec); // increment/decrement => non-const
@ -3611,6 +3612,26 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void const27() // ticket #1882
{
checkConst("class A {\n"
"public:\n"
" A(){m_d=1.0; m_iRealVal=2.0;}\n"
" double dGetValue();\n"
"private:\n"
" double m_d;\n"
" double m_iRealVal;\n"
"};\n"
"double A::dGetValue() {\n"
" double dRet = m_iRealVal;\n"
" if( m_d != 0 )\n"
" return dRet / m_d;\n"
" return dRet;\n"
"};\n"
);
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:4]: (style) The function 'A::dGetValue' can be const\n", errout.str());
}
// increment/decrement => not const // increment/decrement => not const
void constincdec() void constincdec()
{ {