Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2596,18 +2596,18 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
if (!vt || !vt->container)
return false;
const auto yield = vt->container->getYield(end->str());
const Token* parent = tok1->astParent();
while (Token::Match(parent, "(|.|::"))
parent = parent->astParent();
if (contains({Library::Container::Yield::START_ITERATOR, Library::Container::Yield::END_ITERATOR, Library::Container::Yield::ITERATOR}, yield)) {
const Token* parent = tok1->astParent();
while (Token::Match(parent, "(|.|::"))
parent = parent->astParent();
if (parent && parent->isComparisonOp())
return true;
// TODO: use AST
if (parent && parent->isAssignmentOp() && tok1->tokAt(-2)->variable() && Token::Match(tok1->tokAt(-2)->variable()->typeEndToken(), "const_iterator|const_reverse_iterator"))
return true;
}
if ((yield == Library::Container::Yield::ITEM || yield == Library::Container::Yield::AT_INDEX) &&
(lhs->isComparisonOp() || lhs->isAssignmentOp() || (lhs->str() == "(" && Token::Match(lhs->astParent(), "%cop%"))))
((parent && parent->isComparisonOp()) || lhs->isAssignmentOp() || (lhs->str() == "(" && Token::Match(lhs->astParent(), "%cop%"))))
return true; // assume that these functions have const overloads
return false;
};
Expand Down
13 changes: 11 additions & 2 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6822,11 +6822,20 @@ class TestClass : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:10]: (style, inconclusive) Technically the member function 'D::f' can be const. [functionConst]\n",
errout_str());

checkConst("struct S {\n" // #12162
checkConst("int g(int);\n" // #12162
"struct S {\n"
" bool has(int i) { return m.find(i) != m.end(); }\n"
" bool isZero(int i) { return m.at(i) == 0; }\n"
" bool isZero() { return v.front() == 0; }\n"
" void f() { g(v.front() + 1); }\n"
" void set(int i) { m.at(i) = 0; }\n"
" std::map<int, int> m;\n"
" std::vector<int> v;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Technically the member function 'S::has' can be const. [functionConst]\n",
ASSERT_EQUALS("[test.cpp:3:10]: (style, inconclusive) Technically the member function 'S::has' can be const. [functionConst]\n"
"[test.cpp:4:10]: (style, inconclusive) Technically the member function 'S::isZero' can be const. [functionConst]\n"
"[test.cpp:5:10]: (style, inconclusive) Technically the member function 'S::isZero' can be const. [functionConst]\n"
"[test.cpp:6:10]: (style, inconclusive) Technically the member function 'S::f' can be const. [functionConst]\n",
errout_str());
}

Expand Down
Loading