Skip to content

Commit 27a4500

Browse files
Fix #13472, #13516 FP unreachableCode (GNU inline function, custom loop macro) (danmar#7199)
1 parent 01005d2 commit 27a4500

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

lib/checkother.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,8 @@ void CheckOther::checkUnreachableCode()
895895
const bool printInconclusive = mSettings->certainty.isEnabled(Certainty::inconclusive);
896896
const SymbolDatabase* symbolDatabase = mTokenizer->getSymbolDatabase();
897897
for (const Scope * scope : symbolDatabase->functionScopes) {
898+
if (scope->hasInlineOrLambdaFunction(nullptr, /*onlyInline*/ true))
899+
continue;
898900
for (const Token* tok = scope->bodyStart; tok && tok != scope->bodyEnd; tok = tok->next()) {
899901
const Token* secondBreak = nullptr;
900902
const Token* labelName = nullptr;

lib/symboldatabase.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5520,7 +5520,7 @@ static bool hasEmptyCaptureList(const Token* tok) {
55205520
return Token::simpleMatch(listTok, "[ ]");
55215521
}
55225522

5523-
bool Scope::hasInlineOrLambdaFunction(const Token** tokStart) const
5523+
bool Scope::hasInlineOrLambdaFunction(const Token** tokStart, bool onlyInline) const
55245524
{
55255525
return std::any_of(nestedList.begin(), nestedList.end(), [&](const Scope* s) {
55265526
// Inline function
@@ -5530,12 +5530,12 @@ bool Scope::hasInlineOrLambdaFunction(const Token** tokStart) const
55305530
return true;
55315531
}
55325532
// Lambda function
5533-
if (s->type == Scope::eLambda && !hasEmptyCaptureList(s->bodyStart)) {
5533+
if (!onlyInline && s->type == Scope::eLambda && !hasEmptyCaptureList(s->bodyStart)) {
55345534
if (tokStart)
55355535
*tokStart = s->bodyStart;
55365536
return true;
55375537
}
5538-
if (s->hasInlineOrLambdaFunction(tokStart))
5538+
if (s->hasInlineOrLambdaFunction(tokStart, onlyInline))
55395539
return true;
55405540
return false;
55415541
});

lib/symboldatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ class CPPCHECKLIB Scope {
11321132
}
11331133

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

11371137
/**
11381138
* @brief find a function

test/testother.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5724,6 +5724,27 @@ class TestOther : public TestFixture {
57245724
" return 3;\n"
57255725
"}\n");
57265726
TODO_ASSERT_EQUALS("[test.cpp:6]: (style) Statements following 'return' will never be executed.\n", "", errout_str());
5727+
5728+
check("int f() {\n" // #13472
5729+
" int var;\n"
5730+
" auto int ret();\n"
5731+
" int ret() {\n"
5732+
" return var;\n"
5733+
" }\n"
5734+
" var = 42;\n"
5735+
" return ret();\n"
5736+
"}\n", /*cpp*/ false);
5737+
ASSERT_EQUALS("", errout_str());
5738+
5739+
check("void f() {\n" // #13516
5740+
" io_uring_for_each_cqe(&ring, head, cqe) {\n"
5741+
" if (cqe->res == -EOPNOTSUPP)\n"
5742+
" printf(\"error\");\n"
5743+
" goto ok;\n"
5744+
" }\n"
5745+
" usleep(10000);\n"
5746+
"}\n");
5747+
ASSERT_EQUALS("", errout_str());
57275748
}
57285749

57295750
void redundantContinue() {

0 commit comments

Comments
 (0)