Fix #11195 Add redundantContinue check (#4283)

This commit is contained in:
chrchr-github 2022-07-16 16:02:58 +02:00 committed by GitHub
parent 9e74da6126
commit 109a031ec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -829,6 +829,9 @@ void CheckOther::checkUnreachableCode()
if (!labelInFollowingLoop && !silencedCompilerWarningOnly)
unreachableCodeError(secondBreak, inconclusive);
tok = Token::findmatch(secondBreak, "[}:]");
} else if (secondBreak->scope() && secondBreak->scope()->isLoopScope() && secondBreak->str() == "}" && tok->str() == "continue") {
redundantContinueError(tok);
tok = secondBreak;
} else
tok = secondBreak;
@ -854,6 +857,12 @@ void CheckOther::unreachableCodeError(const Token *tok, bool inconclusive)
"Statements following return, break, continue, goto or throw will never be executed.", CWE561, inconclusive ? Certainty::inconclusive : Certainty::normal);
}
void CheckOther::redundantContinueError(const Token *tok)
{
reportError(tok, Severity::style, "redundantContinue",
"'continue' is redundant since it is the last statement in a loop.", CWE561, Certainty::normal);
}
//---------------------------------------------------------------------------
// Check scope of variables..
//---------------------------------------------------------------------------

View File

@ -260,6 +260,7 @@ private:
void duplicateExpressionTernaryError(const Token *tok, ErrorPath errors);
void duplicateBreakError(const Token *tok, bool inconclusive);
void unreachableCodeError(const Token* tok, bool inconclusive);
void redundantContinueError(const Token* tok);
void unsignedLessThanZeroError(const Token *tok, const ValueFlow::Value *v, const std::string &varname);
void pointerLessThanZeroError(const Token *tok, const ValueFlow::Value *v);
void unsignedPositiveError(const Token *tok, const ValueFlow::Value *v, const std::string &varname);

View File

@ -117,6 +117,7 @@ private:
TEST_CASE(switchRedundantOperationTest);
TEST_CASE(switchRedundantBitwiseOperationTest);
TEST_CASE(unreachableCode);
TEST_CASE(redundantContinue);
TEST_CASE(suspiciousCase);
TEST_CASE(suspiciousEqualityComparison);
@ -4449,6 +4450,26 @@ private:
ASSERT_EQUALS("", errout.str());
}
void redundantContinue() {
check("void f() {\n" // #11195
" for (int i = 0; i < 10; ++i) {\n"
" printf(\"i = %d\\n\", i);\n"
" continue;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) 'continue' is redundant since it is the last statement in a loop.\n", errout.str());
check("void f() {\n"
" int i = 0;"
" do {\n"
" ++i;\n"
" printf(\"i = %d\\n\", i);\n"
" continue;\n"
" } 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());
}
void suspiciousCase() {
check("void foo() {\n"