Skip to content

Commit 7f3645f

Browse files
author
Your Name
committed
Fix cppcheck debug warning
1 parent a6ded13 commit 7f3645f

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

lib/checkclass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,10 @@ bool CheckClassImpl::isMemberVar(const Scope *scope, const Token *tok) const
23342334
if (Token::Match(tok->tokAt(-3), "%name% ) . %name%")) {
23352335
tok = tok->tokAt(-3);
23362336
again = true;
2337+
} else if (Token::Match(tok->tokAt(-2), ") . %name%")) {
2338+
// function call chain: the member is accessed through the return value
2339+
tok = tok->linkAt(-2)->previous();
2340+
again = true;
23372341
} else if (Token::Match(tok->tokAt(-2), "%name% . %name%")) {
23382342
tok = tok->tokAt(-2);
23392343
again = true;

test/testclass.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class TestClass : public TestFixture {
228228
TEST_CASE(constRefQualified);
229229
TEST_CASE(staticArrayPtrOverload);
230230
TEST_CASE(qualifiedNameMember); // #10872
231+
TEST_CASE(memberVarCallChain);
231232

232233
TEST_CASE(initializerListOrder);
233234
TEST_CASE(initializerListArgument);
@@ -7856,6 +7857,19 @@ class TestClass : public TestFixture {
78567857
ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:6:9]: (style, inconclusive) Technically the member function 'S::f' can be const. [functionConst]\n", errout_str());
78577858
}
78587859

7860+
void memberVarCallChain() { // member variable accessed through a function call chain
7861+
const Settings s = settingsBuilder().severity(Severity::style).debugwarnings().library("std.cfg").certainty(Certainty::inconclusive).build();
7862+
checkConst("struct S {\n"
7863+
" std::string nodeType;\n"
7864+
" std::vector<std::shared_ptr<S>> children;\n"
7865+
" bool f();\n"
7866+
"};\n"
7867+
"bool S::f() {\n"
7868+
" return !children.empty() && children.back()->nodeType == \"Foo\";\n"
7869+
"}\n", s);
7870+
ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:6:9]: (style, inconclusive) Technically the member function 'S::f' can be const. [functionConst]\n", errout_str());
7871+
}
7872+
78597873
#define checkInitializerListOrder(...) checkInitializerListOrder_(__FILE__, __LINE__, __VA_ARGS__)
78607874
template<size_t size>
78617875
void checkInitializerListOrder_(const char* file, int line, const char (&code)[size]) {

0 commit comments

Comments
 (0)