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
15 changes: 7 additions & 8 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,15 @@ static bool isExecutableScope(const Token* tok)
const Token * tok2 = tok->link()->previous();
if (Token::simpleMatch(tok2, "; }"))
return true;
if (Token::Match(tok2, "{|} }")) {
const Token* startTok = tok2->str() == "{" ? tok2 : tok2->link();
if (tok2 == tok)
return false;
if (Token::simpleMatch(tok2, "} }")) { // inner scope
const Token* startTok = tok2->link();
if (Token::Match(startTok->previous(), "do|try|else {"))
return true;
if (Token::simpleMatch(startTok->previous(), ") {"))
if (Token::Match(startTok->previous(), ")|] {"))
return !findLambdaStartToken(tok2);
if (tok->str() == "{")
return false;
else
return isExecutableScope(startTok);
return isExecutableScope(startTok);
}
return false;
}
Expand All @@ -128,7 +127,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
// pointer to current scope
Scope *scope = &scopeList.back();

// Store the edning of init lists
// Store the ending of init lists
std::stack<std::pair<const Token*, const Scope*>> endInitList;
auto inInitList = [&] {
if (endInitList.empty())
Expand Down
8 changes: 8 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3904,6 +3904,14 @@ class TestStl : public TestFixture {
" f(p.c_str());\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("struct S {\n" //#9161
" const char* f() const noexcept {\n"
" return (\"\" + m).c_str();\n"
" }\n"
" std::string m;\n"
"};\n", /*inconclusive*/ true);
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
}

void uselessCalls() {
Expand Down
16 changes: 16 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(structmember17); // #10591
TEST_CASE(structmember18); // #10684
TEST_CASE(structmember19); // #10826, #10848, #10852
TEST_CASE(structmember20); // #10737

TEST_CASE(localvar1);
TEST_CASE(localvar2);
Expand Down Expand Up @@ -1743,6 +1744,21 @@ class TestUnusedVar : public TestFixture {
settings.enforcedLang = Settings::None;
}

void structmember20() { // #10737
checkStructMemberUsage("void f() {\n"
" {\n"
" }\n"
" {\n"
" struct S { int a; };\n"
" S s{};\n"
" {\n"
" if (s.a) {}\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");
Expand Down