Skip to content

Commit

Permalink
Fix #12527 false negative: redundantContinue with continue in last scope
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 19, 2024
1 parent 967323e commit 436ee7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,18 @@ void CheckOther::suspiciousCaseInSwitchError(const Token* tok, const std::string
"Using an operator like '" + operatorString + "' in a case label is suspicious. Did you intend to use a bitwise operator, multiple case labels or if/else instead?", CWE398, Certainty::inconclusive);
}

static bool isLastStmtContinue(const Token* secondBreak, const Token* tok)
{
if (tok->str() != "continue")
return false;
while (Token::simpleMatch(secondBreak, "}")) {
if (secondBreak->scope() && secondBreak->scope()->isLoopScope())
return true;
secondBreak = secondBreak->next();
}
return false;
}

//---------------------------------------------------------------------------
// Find consecutive return, break, continue, goto or throw statements. e.g.:
// break; break;
Expand Down Expand Up @@ -863,7 +875,7 @@ void CheckOther::checkUnreachableCode()
if (!labelInFollowingLoop && !silencedCompilerWarningOnly)
unreachableCodeError(secondBreak, tok, inconclusive);
tok = Token::findmatch(secondBreak, "[}:]");
} else if (secondBreak->scope() && secondBreak->scope()->isLoopScope() && secondBreak->str() == "}" && tok->str() == "continue") {
} else if (isLastStmtContinue(secondBreak, tok)) {
redundantContinueError(tok);
tok = secondBreak;
} else
Expand Down
9 changes: 9 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5322,6 +5322,15 @@ class TestOther : public TestFixture {
" } while (i < 10);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (style) 'continue' is redundant since it is the last statement in a loop.\n", errout_str());

check("void f(bool b, const std::list<int>& l) {\n" // #12527
" for (int i : l) {\n"
" (void)i;\n"
" if (b)\n"
" continue;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (style) 'continue' is redundant since it is the last statement in a loop.\n", errout_str());
}


Expand Down

0 comments on commit 436ee7b

Please sign in to comment.