xref: /llvm-project/clang/test/Sema/switch-default.cpp (revision 033ec098be730bff04bfb929d254ce57e5ec8534)
1*033ec098Shstk30-hw // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wswitch-default %s
2*033ec098Shstk30-hw 
f1(int a)3*033ec098Shstk30-hw int f1(int a) {
4*033ec098Shstk30-hw   switch (a) {                // expected-warning {{'switch' missing 'default' label}}
5*033ec098Shstk30-hw     case 1: a++; break;
6*033ec098Shstk30-hw     case 2: a += 2; break;
7*033ec098Shstk30-hw   }
8*033ec098Shstk30-hw   return a;
9*033ec098Shstk30-hw }
10*033ec098Shstk30-hw 
f2(int a)11*033ec098Shstk30-hw int f2(int a) {
12*033ec098Shstk30-hw   switch (a) {                // no-warning
13*033ec098Shstk30-hw     default:
14*033ec098Shstk30-hw       ;
15*033ec098Shstk30-hw   }
16*033ec098Shstk30-hw   return a;
17*033ec098Shstk30-hw }
18*033ec098Shstk30-hw 
19*033ec098Shstk30-hw // Warn even completely covered Enum cases(GCC compatibility).
20*033ec098Shstk30-hw enum E { A, B };
check_enum(enum E e)21*033ec098Shstk30-hw enum E check_enum(enum E e) {
22*033ec098Shstk30-hw   switch (e) {                // expected-warning {{'switch' missing 'default' label}}
23*033ec098Shstk30-hw     case A: break;
24*033ec098Shstk30-hw     case B: break;
25*033ec098Shstk30-hw   }
26*033ec098Shstk30-hw   return e;
27*033ec098Shstk30-hw }
28*033ec098Shstk30-hw 
29*033ec098Shstk30-hw template<typename Index>
t1(Index i)30*033ec098Shstk30-hw int t1(Index i)
31*033ec098Shstk30-hw {
32*033ec098Shstk30-hw   switch (i) {              // expected-warning {{'switch' missing 'default' label}}
33*033ec098Shstk30-hw     case 0: return 0;
34*033ec098Shstk30-hw     case 1: return 1;
35*033ec098Shstk30-hw   }
36*033ec098Shstk30-hw   return 0;
37*033ec098Shstk30-hw }
38*033ec098Shstk30-hw 
39*033ec098Shstk30-hw template<typename Index>
t2(Index i)40*033ec098Shstk30-hw int t2(Index i)
41*033ec098Shstk30-hw {
42*033ec098Shstk30-hw   switch (i) {            // no-warning
43*033ec098Shstk30-hw     case 0: return 0;
44*033ec098Shstk30-hw     case 1: return 1;
45*033ec098Shstk30-hw     default: return 2;
46*033ec098Shstk30-hw   }
47*033ec098Shstk30-hw   return 0;
48*033ec098Shstk30-hw }
49*033ec098Shstk30-hw 
main()50*033ec098Shstk30-hw int main() {
51*033ec098Shstk30-hw   return t1(1);       // expected-note {{in instantiation of function template specialization 't1<int>' requested here}}
52*033ec098Shstk30-hw }
53*033ec098Shstk30-hw 
54