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

Function isExhaustiveSwitch

lib/astutils.cpp:3795–3823  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3793}
3794
3795bool isExhaustiveSwitch(const Token *startbrace)
3796{
3797 if (!startbrace || !Token::simpleMatch(startbrace->previous(), ") {") || startbrace->scope()->type != ScopeType::eSwitch)
3798 return false;
3799 const Token *rpar = startbrace->previous();
3800 const Token *lpar = rpar->link();
3801
3802 const Token *condition = lpar->astOperand2();
3803 if (!condition->valueType())
3804 return true;
3805
3806 bool hasDefault = false;
3807 const std::set<MathLib::bigint> switchValues = getSwitchValues(startbrace, hasDefault);
3808
3809 if (hasDefault)
3810 return true;
3811
3812 if (condition->valueType()->type == ValueType::Type::BOOL)
3813 return switchValues.count(0) && switchValues.count(1);
3814
3815 if (condition->valueType()->isEnum()) {
3816 const std::vector<Enumerator> &enumList = condition->valueType()->typeScope->enumeratorList;
3817 return std::all_of(enumList.cbegin(), enumList.cend(), [&](const Enumerator &e) {
3818 return !e.value_known || switchValues.count(e.value);
3819 });
3820 }
3821
3822 return false;
3823}
3824
3825bool isUnreachableOperand(const Token *tok)
3826{

Callers 1

checkMissingReturnScopeFunction · 0.85

Calls 5

getSwitchValuesFunction · 0.85
scopeMethod · 0.80
astOperand2Method · 0.80
isEnumMethod · 0.80
simpleMatchFunction · 0.70

Tested by

no test coverage detected