[webkit-dev] Idiom for functions with all return values in a switch case

Ryosuke Niwa rniwa at webkit.org
Tue May 9 12:57:14 PDT 2017


On Tue, May 9, 2017 at 12:11 PM, Maciej Stachowiak <mjs at apple.com> wrote:
>
>
>> On May 9, 2017, at 11:35 AM, Michael Catanzaro <mcatanzaro at igalia.com> wrote:
>>
>> On Tue, May 9, 2017 at 1:13 PM, Maciej Stachowiak <mjs at apple.com> wrote:
>>> I think this second option may suppress the warning when you have forgotten to list one of the enum values, since there is now a default case. I believe that's the reason for the recommended option.
>>
>> Ah, good point. Normally we do want a warning when a new enum value has been introduced. That could be avoided by this variant:
>>
>>      static WKAutoplayEvent toWKAutoplayEvent(WebCore::AutoplayEvent event)
>>      {
>>          switch (event) {
>>          // ...
>>          }
>>
>>          RELEASE_ASSERT_NOT_REACHED();
>>      }
>>
>> That seems nicer than this:
>>
>>      static WKAutoplayEvent toWKAutoplayEvent(WebCore::AutoplayEvent event)
>>      {
>>          switch (event) {
>>          // ...
>>          }
>>
>>          ASSERT_NOT_REACHED();
>>          return static_cast<WKAutoplayEvent>(0);
>>      }
>

One annoying thing is that I've seen clang complaining about that
return statement being an unreachable statement while gcc doesn't :(.
So we probably shouldn't have return statement, not to mention that 0
may not be a valid enum value.

- R. Niwa


More information about the webkit-dev mailing list