Skip to content

Commit

Permalink
Fix #13472, #13516 FP unreachableCode (GNU inline function, custom lo…
Browse files Browse the repository at this point in the history
…op macro) (danmar#7199)
  • Loading branch information
chrchr-github authored Jan 10, 2025
1 parent 01005d2 commit 27a4500
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,8 @@ void CheckOther::checkUnreachableCode()
const bool printInconclusive = mSettings->certainty.isEnabled(Certainty::inconclusive);
const SymbolDatabase* symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope * scope : symbolDatabase->functionScopes) {
if (scope->hasInlineOrLambdaFunction(nullptr, /*onlyInline*/ true))
continue;
for (const Token* tok = scope->bodyStart; tok && tok != scope->bodyEnd; tok = tok->next()) {
const Token* secondBreak = nullptr;
const Token* labelName = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5520,7 +5520,7 @@ static bool hasEmptyCaptureList(const Token* tok) {
return Token::simpleMatch(listTok, "[ ]");
}

bool Scope::hasInlineOrLambdaFunction(const Token** tokStart) const
bool Scope::hasInlineOrLambdaFunction(const Token** tokStart, bool onlyInline) const
{
return std::any_of(nestedList.begin(), nestedList.end(), [&](const Scope* s) {
// Inline function
Expand All @@ -5530,12 +5530,12 @@ bool Scope::hasInlineOrLambdaFunction(const Token** tokStart) const
return true;
}
// Lambda function
if (s->type == Scope::eLambda && !hasEmptyCaptureList(s->bodyStart)) {
if (!onlyInline && s->type == Scope::eLambda && !hasEmptyCaptureList(s->bodyStart)) {
if (tokStart)
*tokStart = s->bodyStart;
return true;
}
if (s->hasInlineOrLambdaFunction(tokStart))
if (s->hasInlineOrLambdaFunction(tokStart, onlyInline))
return true;
return false;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ class CPPCHECKLIB Scope {
}

// Is there lambda/inline function(s) in this scope?
bool hasInlineOrLambdaFunction(const Token** tokStart = nullptr) const;
bool hasInlineOrLambdaFunction(const Token** tokStart = nullptr, bool onlyInline = false) const;

/**
* @brief find a function
Expand Down
21 changes: 21 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5724,6 +5724,27 @@ class TestOther : public TestFixture {
" return 3;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:6]: (style) Statements following 'return' will never be executed.\n", "", errout_str());

check("int f() {\n" // #13472
" int var;\n"
" auto int ret();\n"
" int ret() {\n"
" return var;\n"
" }\n"
" var = 42;\n"
" return ret();\n"
"}\n", /*cpp*/ false);
ASSERT_EQUALS("", errout_str());

check("void f() {\n" // #13516
" io_uring_for_each_cqe(&ring, head, cqe) {\n"
" if (cqe->res == -EOPNOTSUPP)\n"
" printf(\"error\");\n"
" goto ok;\n"
" }\n"
" usleep(10000);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void redundantContinue() {
Expand Down

0 comments on commit 27a4500

Please sign in to comment.