diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a45dfdf672e..a90aa0b180f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1945,13 +1945,16 @@ void CheckOtherImpl::checkConstPointer() if (deref == MEMBER) { if (!gparent) continue; - if (parent->astOperand2()) { - if (parent->astOperand2()->function() && parent->astOperand2()->function()->isConst()) + const Token* funcParent = parent; + while (Token::simpleMatch(funcParent->astParent(), ".")) + funcParent = funcParent->astParent(); + if (funcParent->astOperand2()) { + if (funcParent->astOperand2()->function() && funcParent->astOperand2()->function()->isConst()) continue; - if (mSettings.library.isFunctionConst(parent->astOperand2())) + if (mSettings.library.isFunctionConst(funcParent->astOperand2())) continue; - if (parent->astOperand2()->varId()) { - if (gparent->str() == "?" && astIsLHS(parent)) + if (funcParent->astOperand2()->varId()) { + if (gparent->str() == "?" && astIsLHS(funcParent)) continue; } } diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 8ed249f05e2..fe85260fc8d 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -249,7 +249,7 @@ void Variables::clearAliases(nonneg int varid) void Variables::eraseAliases(nonneg int varid) { - VariableUsage *usage = find(varid); + const VariableUsage *usage = find(varid); if (usage) { for (auto aliases = usage->_aliases.cbegin(); aliases != usage->_aliases.cend(); ++aliases) @@ -329,7 +329,7 @@ void Variables::write(nonneg int varid, const Token* tok) void Variables::writeAliases(nonneg int varid, const Token* tok) { - VariableUsage *usage = find(varid); + const VariableUsage *usage = find(varid); if (usage) { for (auto aliases = usage->_aliases.cbegin(); aliases != usage->_aliases.cend(); ++aliases) { diff --git a/test/testother.cpp b/test/testother.cpp index 1d69cf6763e..b8134623a37 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4894,6 +4894,18 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:6:10]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n", errout_str()); + + check("struct S { std::string a; };\n" // #13678 + "struct T { S s; };\n" + "bool f(S* s) {\n" + " return s->a.empty();\n" + "}\n" + "bool g(T* t) {\n" + " return t->s.a.empty();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3:11]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" + "[test.cpp:6:11]: (style) Parameter 't' can be declared as pointer to const [constParameterPointer]\n", + errout_str()); } void constArray() {