Skip to content

Commit c281782

Browse files
[clang][Sema] Add -Wswitch-default warning option (#73077)
Adds a warning, issued by the clang semantic analysis. The patch warns on switch which don't have the default branch. This is a counterpart of gcc's Wswitch-default.
1 parent e1a4b00 commit c281782

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ Improvements to Clang's diagnostics
353353
of a base class is not called in the constructor of its derived class.
354354
- Clang no longer emits ``-Wmissing-variable-declarations`` for variables declared
355355
with the ``register`` storage class.
356+
- Clang's ``-Wswitch-default`` flag now diagnoses whenever a ``switch`` statement
357+
does not have a ``default`` label.
356358
- Clang's ``-Wtautological-negation-compare`` flag now diagnoses logical
357359
tautologies like ``x && !x`` and ``!x || x`` in expressions. This also
358360
makes ``-Winfinite-recursion`` diagnose more cases.

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def ShadowAll : DiagGroup<"shadow-all", [Shadow, ShadowFieldInConstructor,
632632
def Shorten64To32 : DiagGroup<"shorten-64-to-32">;
633633
def : DiagGroup<"sign-promo">;
634634
def SignCompare : DiagGroup<"sign-compare">;
635-
def : DiagGroup<"switch-default">;
635+
def SwitchDefault : DiagGroup<"switch-default">;
636636
def : DiagGroup<"synth">;
637637
def SizeofArrayArgument : DiagGroup<"sizeof-array-argument">;
638638
def SizeofArrayDecay : DiagGroup<"sizeof-array-decay">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10064,6 +10064,8 @@ def warn_missing_case : Warning<"%plural{"
1006410064
"3:enumeration values %1, %2, and %3 not handled in switch|"
1006510065
":%0 enumeration values not handled in switch: %1, %2, %3...}0">,
1006610066
InGroup<Switch>;
10067+
def warn_switch_default : Warning<"'switch' missing 'default' label">,
10068+
InGroup<SwitchDefault>, DefaultIgnore;
1006710069

1006810070
def warn_unannotated_fallthrough : Warning<
1006910071
"unannotated fall-through between switch labels">,

clang/lib/Sema/SemaStmt.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,9 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
13271327
}
13281328
}
13291329

1330+
if (!TheDefaultStmt)
1331+
Diag(SwitchLoc, diag::warn_switch_default);
1332+
13301333
if (!HasDependentValue) {
13311334
// If we don't have a default statement, check whether the
13321335
// condition is constant.

clang/test/Sema/switch-default.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-default %s
2+
3+
int f1(int a) {
4+
switch (a) { // expected-warning {{'switch' missing 'default' label}}
5+
case 1: a++; break;
6+
case 2: a += 2; break;
7+
}
8+
return a;
9+
}
10+
11+
int f2(int a) {
12+
switch (a) { // no-warning
13+
default:
14+
;
15+
}
16+
return a;
17+
}

0 commit comments

Comments
 (0)