Skip to content

Commit d8eaa52

Browse files
Fix #13678 FN constParameterPointer (calling const function on struct member) (#8624)
1 parent 887b5d2 commit d8eaa52

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

lib/checkother.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,13 +1945,16 @@ void CheckOtherImpl::checkConstPointer()
19451945
if (deref == MEMBER) {
19461946
if (!gparent)
19471947
continue;
1948-
if (parent->astOperand2()) {
1949-
if (parent->astOperand2()->function() && parent->astOperand2()->function()->isConst())
1948+
const Token* funcParent = parent;
1949+
while (Token::simpleMatch(funcParent->astParent(), "."))
1950+
funcParent = funcParent->astParent();
1951+
if (funcParent->astOperand2()) {
1952+
if (funcParent->astOperand2()->function() && funcParent->astOperand2()->function()->isConst())
19501953
continue;
1951-
if (mSettings.library.isFunctionConst(parent->astOperand2()))
1954+
if (mSettings.library.isFunctionConst(funcParent->astOperand2()))
19521955
continue;
1953-
if (parent->astOperand2()->varId()) {
1954-
if (gparent->str() == "?" && astIsLHS(parent))
1956+
if (funcParent->astOperand2()->varId()) {
1957+
if (gparent->str() == "?" && astIsLHS(funcParent))
19551958
continue;
19561959
}
19571960
}

lib/checkunusedvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void Variables::clearAliases(nonneg int varid)
249249

250250
void Variables::eraseAliases(nonneg int varid)
251251
{
252-
VariableUsage *usage = find(varid);
252+
const VariableUsage *usage = find(varid);
253253

254254
if (usage) {
255255
for (auto aliases = usage->_aliases.cbegin(); aliases != usage->_aliases.cend(); ++aliases)
@@ -329,7 +329,7 @@ void Variables::write(nonneg int varid, const Token* tok)
329329

330330
void Variables::writeAliases(nonneg int varid, const Token* tok)
331331
{
332-
VariableUsage *usage = find(varid);
332+
const VariableUsage *usage = find(varid);
333333

334334
if (usage) {
335335
for (auto aliases = usage->_aliases.cbegin(); aliases != usage->_aliases.cend(); ++aliases) {

test/testother.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,6 +4894,18 @@ class TestOther : public TestFixture {
48944894
"}\n");
48954895
ASSERT_EQUALS("[test.cpp:6:10]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n",
48964896
errout_str());
4897+
4898+
check("struct S { std::string a; };\n" // #13678
4899+
"struct T { S s; };\n"
4900+
"bool f(S* s) {\n"
4901+
" return s->a.empty();\n"
4902+
"}\n"
4903+
"bool g(T* t) {\n"
4904+
" return t->s.a.empty();\n"
4905+
"}\n");
4906+
ASSERT_EQUALS("[test.cpp:3:11]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n"
4907+
"[test.cpp:6:11]: (style) Parameter 't' can be declared as pointer to const [constParameterPointer]\n",
4908+
errout_str());
48974909
}
48984910

48994911
void constArray() {

0 commit comments

Comments
 (0)