MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / checkSuspiciousCaseInSwitch

Method checkSuspiciousCaseInSwitch

lib/checkother.cpp:924–956  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Check for statements like case A||B: in switch() ---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

922// Check for statements like case A||B: in switch()
923//---------------------------------------------------------------------------
924void CheckOtherImpl::checkSuspiciousCaseInSwitch()
925{
926 if (!mSettings.certainty.isEnabled(Certainty::inconclusive) || !mSettings.severity.isEnabled(Severity::warning))
927 return;
928
929 logChecker("CheckOther::checkSuspiciousCaseInSwitch"); // warning,inconclusive
930
931 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
932
933 for (const Scope & scope : symbolDatabase->scopeList) {
934 if (scope.type != ScopeType::eSwitch)
935 continue;
936
937 for (const Token* tok = scope.bodyStart->next(); tok != scope.bodyEnd; tok = tok->next()) {
938 if (tok->str() == "case") {
939 const Token* finding = nullptr;
940 for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) {
941 if (tok2->str() == ":")
942 break;
943 if (Token::Match(tok2, "[;}{]"))
944 break;
945
946 if (tok2->str() == "?")
947 finding = nullptr;
948 else if (Token::Match(tok2, "&&|%oror%"))
949 finding = tok2;
950 }
951 if (finding)
952 suspiciousCaseInSwitchError(finding, finding->str());
953 }
954 }
955 }
956}
957
958void CheckOtherImpl::suspiciousCaseInSwitchError(const Token* tok, const std::string& operatorString)
959{

Callers 1

runChecksMethod · 0.80

Calls 3

nextMethod · 0.80
isEnabledMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected