Skip to content

Commit fd797a9

Browse files
Fix warning C26818 about switch statements needing default cases (#4715)
1 parent 15af446 commit fd797a9

File tree

7 files changed

+92
-1
lines changed

7 files changed

+92
-1
lines changed

stl/inc/chrono

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4177,6 +4177,8 @@ namespace chrono {
41774177
case '+':
41784178
++_First;
41794179
break;
4180+
default:
4181+
break;
41804182
}
41814183

41824184
// For a regular offset hh[mm], simply read four digits, with the option of an EOF or non-digit after

stl/inc/format

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ _NODISCARD constexpr _Decode_result<char> _Decode_utf(const char* _First, const
276276
return {_First + 1, false};
277277
}
278278
break;
279+
default:
280+
break;
279281
}
280282

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

297302
for (int _Idx = 1; _Idx < _Num_bytes; ++_Idx) {
@@ -1258,6 +1263,8 @@ _NODISCARD constexpr const _CharT* _Parse_align(
12581263
case '^':
12591264
_Parsed_align = _Fmt_align::_Center;
12601265
break;
1266+
default:
1267+
break;
12611268
}
12621269

12631270
if (_Parsed_align != _Fmt_align::_None) {
@@ -2849,6 +2856,8 @@ _NODISCARD _OutputIt _Write_integral(
28492856
case 'o':
28502857
_Base = 8;
28512858
break;
2859+
default:
2860+
break;
28522861
}
28532862

28542863
// long long -1 representation in binary is 64 bits + sign
@@ -4447,6 +4456,8 @@ _NODISCARD constexpr const _CharT* _Parse_range_specs(
44474456
_Callbacks._On_type(_Maybe_type);
44484457
++_Begin;
44494458
break;
4459+
default:
4460+
break;
44504461
}
44514462

44524463
return _Begin;
@@ -4534,6 +4545,10 @@ public:
45344545
}
45354546

45364547
break;
4548+
4549+
default:
4550+
_STL_UNREACHABLE; // can't happen, we've handled all possible outputs from _Parse_range_specs()
4551+
break;
45374552
}
45384553

45394554
if (_Specs._No_brackets) {

stl/inc/regex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,6 +3922,9 @@ void _Parser<_FwdIt, _Elem, _RxTraits>::_Trans() { // map character to meta-char
39223922
}
39233923

39243924
break;
3925+
3926+
default:
3927+
break;
39253928
}
39263929
}
39273930

stl/inc/xlocmon

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,15 @@ private:
567567
if (_Pattern.field[_Off] == money_base::space && !_Seen) {
568568
_Bad = true; // fail if no space seen
569569
}
570-
} // parse optional space
570+
571+
break;
572+
}
573+
574+
default:
575+
_STL_ASSERT(false, "Invalid money_base::pattern, see N4981 [locale.moneypunct.general]/1: "
576+
"In the field member of a pattern object, each value symbol, sign, value, "
577+
"and either space or none appears exactly once.");
578+
break;
571579
} // switch
572580
}
573581

@@ -787,6 +795,12 @@ private:
787795
}
788796

789797
break;
798+
799+
default:
800+
_STL_ASSERT(false, "Invalid money_base::pattern, see N4981 [locale.moneypunct.general]/1: "
801+
"In the field member of a pattern object, each value symbol, sign, value, "
802+
"and either space or none appears exactly once.");
803+
break;
790804
}
791805
}
792806

@@ -840,6 +854,13 @@ private:
840854
_Dest = _Rep(_Dest, _Fill, _Fillcount);
841855
_Fillcount = 0;
842856
}
857+
break;
858+
859+
default:
860+
_STL_ASSERT(false, "Invalid money_base::pattern, see N4981 [locale.moneypunct.general]/1: "
861+
"In the field member of a pattern object, each value symbol, sign, value, "
862+
"and either space or none appears exactly once.");
863+
break;
843864
}
844865
}
845866

stl/inc/xloctime

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ protected:
563563

564564
default:
565565
_State |= ios_base::failbit; // unknown specifier
566+
break;
566567
}
567568

568569
if (_First == _Last) {

tests/std/rulesets/stl.ruleset

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
<Rule Id="C26815" Action="Warning" />
88
<!-- The pointer points to memory allocated on the stack (ES.65) -->
99
<Rule Id="C26816" Action="Warning" />
10+
<!-- Switch statement does not cover all cases. Consider adding a 'default' label (es.79). -->
11+
<Rule Id="C26818" Action="Warning" />
1012
</Rules>
1113
</RuleSet>

tests/std/tests/GH_002094_cpp_core_guidelines/test.compile.pass.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,50 @@
22
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

44
#include <__msvc_all_public_headers.hpp>
5+
6+
using namespace std;
7+
8+
void instantiate_regex_machinery() {
9+
const regex r{R"(.+meow.+)"};
10+
assert(regex_match("homeowner", r));
11+
}
12+
13+
#if _HAS_CXX20
14+
void instantiate_chrono_parse_machinery() {
15+
istringstream iss{"10:02:07"};
16+
chrono::seconds s{};
17+
iss >> chrono::parse("%H:%M:%S", s);
18+
assert(s == 36127s);
19+
}
20+
#endif // ^^^ _HAS_CXX20 ^^^
21+
22+
#if _HAS_CXX23
23+
template <class T>
24+
struct WrappedVector : vector<T> {
25+
using vector<T>::vector;
26+
};
27+
28+
template <class T>
29+
struct std::formatter<WrappedVector<T>, char> {
30+
public:
31+
template <class ParseContext>
32+
constexpr auto parse(ParseContext& ctx) {
33+
return underlying.parse(ctx);
34+
}
35+
36+
template <class FormatContext>
37+
auto format(const WrappedVector<T>& rng, FormatContext& ctx) const {
38+
return underlying.format(rng, ctx);
39+
}
40+
41+
private:
42+
range_formatter<T, char> underlying;
43+
};
44+
45+
void instantiate_range_formatter_machinery() {
46+
const WrappedVector<int> v{11, 22, 33, 44};
47+
assert(format("{}", v) == "[11, 22, 33, 44]");
48+
assert(format("{:}", v) == "[11, 22, 33, 44]");
49+
assert(format("{:n}", v) == "11, 22, 33, 44");
50+
}
51+
#endif // ^^^ _HAS_CXX23 ^^^

0 commit comments

Comments
 (0)