Skip to content

Commit

Permalink
Fix warning C26818 about switch statements needing default cases (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanTLavavej authored Jun 18, 2024
1 parent 15af446 commit fd797a9
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions stl/inc/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -4177,6 +4177,8 @@ namespace chrono {
case '+':
++_First;
break;
default:
break;
}

// For a regular offset hh[mm], simply read four digits, with the option of an EOF or non-digit after
Expand Down
15 changes: 15 additions & 0 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ _NODISCARD constexpr _Decode_result<char> _Decode_utf(const char* _First, const
return {_First + 1, false};
}
break;
default:
break;
}

// mask out the "value bits" in the leading byte,
Expand All @@ -292,6 +294,9 @@ _NODISCARD constexpr _Decode_result<char> _Decode_utf(const char* _First, const
case 4:
_Val &= 0b111u;
break;
default:
_STL_UNREACHABLE; // can't happen, see how the value of _Num_bytes is determined
break;
}

for (int _Idx = 1; _Idx < _Num_bytes; ++_Idx) {
Expand Down Expand Up @@ -1258,6 +1263,8 @@ _NODISCARD constexpr const _CharT* _Parse_align(
case '^':
_Parsed_align = _Fmt_align::_Center;
break;
default:
break;
}

if (_Parsed_align != _Fmt_align::_None) {
Expand Down Expand Up @@ -2849,6 +2856,8 @@ _NODISCARD _OutputIt _Write_integral(
case 'o':
_Base = 8;
break;
default:
break;
}

// long long -1 representation in binary is 64 bits + sign
Expand Down Expand Up @@ -4447,6 +4456,8 @@ _NODISCARD constexpr const _CharT* _Parse_range_specs(
_Callbacks._On_type(_Maybe_type);
++_Begin;
break;
default:
break;
}

return _Begin;
Expand Down Expand Up @@ -4534,6 +4545,10 @@ public:
}

break;

default:
_STL_UNREACHABLE; // can't happen, we've handled all possible outputs from _Parse_range_specs()
break;
}

if (_Specs._No_brackets) {
Expand Down
3 changes: 3 additions & 0 deletions stl/inc/regex
Original file line number Diff line number Diff line change
Expand Up @@ -3922,6 +3922,9 @@ void _Parser<_FwdIt, _Elem, _RxTraits>::_Trans() { // map character to meta-char
}

break;

default:
break;
}
}

Expand Down
23 changes: 22 additions & 1 deletion stl/inc/xlocmon
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,15 @@ private:
if (_Pattern.field[_Off] == money_base::space && !_Seen) {
_Bad = true; // fail if no space seen
}
} // parse optional space

break;
}

default:
_STL_ASSERT(false, "Invalid money_base::pattern, see N4981 [locale.moneypunct.general]/1: "
"In the field member of a pattern object, each value symbol, sign, value, "
"and either space or none appears exactly once.");
break;
} // switch
}

Expand Down Expand Up @@ -787,6 +795,12 @@ private:
}

break;

default:
_STL_ASSERT(false, "Invalid money_base::pattern, see N4981 [locale.moneypunct.general]/1: "
"In the field member of a pattern object, each value symbol, sign, value, "
"and either space or none appears exactly once.");
break;
}
}

Expand Down Expand Up @@ -840,6 +854,13 @@ private:
_Dest = _Rep(_Dest, _Fill, _Fillcount);
_Fillcount = 0;
}
break;

default:
_STL_ASSERT(false, "Invalid money_base::pattern, see N4981 [locale.moneypunct.general]/1: "
"In the field member of a pattern object, each value symbol, sign, value, "
"and either space or none appears exactly once.");
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions stl/inc/xloctime
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ protected:

default:
_State |= ios_base::failbit; // unknown specifier
break;
}

if (_First == _Last) {
Expand Down
2 changes: 2 additions & 0 deletions tests/std/rulesets/stl.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
<Rule Id="C26815" Action="Warning" />
<!-- The pointer points to memory allocated on the stack (ES.65) -->
<Rule Id="C26816" Action="Warning" />
<!-- Switch statement does not cover all cases. Consider adding a 'default' label (es.79). -->
<Rule Id="C26818" Action="Warning" />
</Rules>
</RuleSet>
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,50 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <__msvc_all_public_headers.hpp>

using namespace std;

void instantiate_regex_machinery() {
const regex r{R"(.+meow.+)"};
assert(regex_match("homeowner", r));
}

#if _HAS_CXX20
void instantiate_chrono_parse_machinery() {
istringstream iss{"10:02:07"};
chrono::seconds s{};
iss >> chrono::parse("%H:%M:%S", s);
assert(s == 36127s);
}
#endif // ^^^ _HAS_CXX20 ^^^

#if _HAS_CXX23
template <class T>
struct WrappedVector : vector<T> {
using vector<T>::vector;
};

template <class T>
struct std::formatter<WrappedVector<T>, char> {
public:
template <class ParseContext>
constexpr auto parse(ParseContext& ctx) {
return underlying.parse(ctx);
}

template <class FormatContext>
auto format(const WrappedVector<T>& rng, FormatContext& ctx) const {
return underlying.format(rng, ctx);
}

private:
range_formatter<T, char> underlying;
};

void instantiate_range_formatter_machinery() {
const WrappedVector<int> v{11, 22, 33, 44};
assert(format("{}", v) == "[11, 22, 33, 44]");
assert(format("{:}", v) == "[11, 22, 33, 44]");
assert(format("{:n}", v) == "11, 22, 33, 44");
}
#endif // ^^^ _HAS_CXX23 ^^^

0 comments on commit fd797a9

Please sign in to comment.