Skip to content

Commit b4e182d

Browse files
NSProgrammerfacebook-github-bot
authored andcommitted
Fix exhaustive switches 29/x
Summary: https://www.internalfb.com/wiki/IOS_at_Meta/switch_statements_in_C_languages/ *Exhaustive Switches*, i.e. using `-Wswitch-enum` and `-Wswitch-default`, are the ideal default for all C language code at Meta. This diff is part of the migration of *Instagram* to adopting *Exhaustive Switches* by default. Currently only `-Wswitch-enum` is enabled for *Instagram* apps, but we want to add `-Wswitch-default` to prevent bugs from undefined/unpredicted behavior (which means adding these `default:` cases will NOT interfere with the compiler putting up an error when a new value is added to an enum, like we want). We will follow the recommended solutions as outlined in https://www.internalfb.com/wiki/Apple_Build_Errors/Missing_switch_default_Label/ 1. **Fail Fast**: [the vast majority of cases] use an `abort()` to forcibly crash the app on unexpected values. Always use this when the existing behavior would be unpredictable/undefined if you were to introduce `default: break;`. Also use this if one of the other options do not fit. 2. **Handle unexpected values (A)**: when there is minimal code after the `switch` that only the unhandled values would reach, pull it into the `default:` 3. **Handle unexpected values (B)**: when the code after the `switch` is safe for unhandled values but also needed for handled values, add a `default: break;` statement (potentially with an `FBCReportMustFix(...)`). 4. **Handle unexpected values (C)**: when the enum type is from the platform itself (not our monorepo), and is an `NS_ENUM` type, we can make a judgement call if think the enum might expand and thus add a default handling behavior. If there is a reasonable chance the platform could expand the enum in a future OS release, we can avoid crashing until we start building against the new SDK where the `-Wswitch-enum` will help us explicitly handle the new case. 5. **Convert to *Flexible Switch* or to `if` checks**: on the rare occasion, it can be evident that we do not need an *exhaustive* switch and can use `FB_FLEXIBLE_SWITCH_BEGIN|END` or just use `if` checks. Reviewed By: aary Differential Revision: D76546644 fbshipit-source-id: 7c1fe1112bc4de10f83e97c1c9981d39c369761e
1 parent 92cca74 commit b4e182d

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

third-party/folly/src/folly/json/dynamic-inl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,8 @@ class FormatValue<dynamic> {
13591359
case dynamic::OBJECT:
13601360
FormatValue(val_.at(arg.splitKey().toString())).format(arg, cb);
13611361
break;
1362+
default:
1363+
folly::assume_unreachable();
13621364
}
13631365
}
13641366

0 commit comments

Comments
 (0)