-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[Sema] Warn about omitting deprecated enumerator in switch #138562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This undoes part of 3e4e3b1 which added the "Omitting a deprecated constant is ok; it should never materialize." logic. That seems wrong: deprecated means the enumerator is likely to be removed in future versions, not that it canot materialize.
@llvm/pr-subscribers-clang Author: Hans Wennborg (zmodem) ChangesThis undoes part of 3e4e3b1 which added the "Omitting a deprecated constant is ok; it should never materialize." logic. That seems wrong: deprecated means the enumerator is likely to be removed in future versions, not that it cannot materialize. Full diff: https://github.com/llvm/llvm-project/pull/138562.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index e8c1f8490342a..990d2fadaf5aa 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -1667,8 +1667,12 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
// Don't warn about omitted unavailable EnumConstantDecls.
switch (EI->second->getAvailability()) {
case AR_Deprecated:
- // Omitting a deprecated constant is ok; it should never materialize.
+ // Deprecated enumerators still need to be handled: they may be
+ // deprecated, but can still occur.
+ break;
+
case AR_Unavailable:
+ // Omitting an unavailable enumerator is ok; it should never occur.
continue;
case AR_NotYetIntroduced:
diff --git a/clang/test/Sema/switch-availability.c b/clang/test/Sema/switch-availability.c
index 888edddac463d..b4f8726addc0b 100644
--- a/clang/test/Sema/switch-availability.c
+++ b/clang/test/Sema/switch-availability.c
@@ -15,7 +15,7 @@ enum SwitchTwo {
};
void testSwitchTwo(enum SwitchTwo st) {
- switch (st) {} // expected-warning{{enumeration values 'Vim' and 'Emacs' not handled in switch}}
+ switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim' and 'Emacs' not handled in switch}}
}
enum SwitchThree {
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I think this is sufficiently niche that we don't need a flag flip to manage the diagnostic change fallout.
@@ -15,7 +15,7 @@ enum SwitchTwo { | |||
}; | |||
|
|||
void testSwitchTwo(enum SwitchTwo st) { | |||
switch (st) {} // expected-warning{{enumeration values 'Vim' and 'Emacs' not handled in switch}} | |||
switch (st) {} // expected-warning{{enumeration values 'Ed', 'Vim' and 'Emacs' not handled in switch}} | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have some test showing that the statement after the switch is considered reachable. You can build one with -Wreturn-type and an int return:
int doswitch(SwitchTwo e) {
switch (e) {
case Emacs: return 42;
}
} // expected-warning {{asdf}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Fly-by comment: I drafted the following alternative comment in another thread. Please feel free to disregard as I don't know the tone and style of comments in these files. (But I appreciate your consideration for the contents in the message I'd like to see!)
|
My patch changes the behavior so that deprecated constants are not ignored by the switch coverage warning. |
I wonder if we should also do something special about the If the user fixes the -Wswitch / -Wreturn-type warnings from
They will get Maybe we should suppress that warning for switch cases? Or suggest adding a |
This undoes part of 3e4e3b1 which added the "Omitting a deprecated constant is ok; it should never materialize." logic.
That seems wrong: deprecated means the enumerator is likely to be removed in future versions, not that it cannot materialize.