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

Konstantin Tokarev annulen at yandex.ru
Thu May 11 06:36:15 PDT 2017



11.05.2017, 11:18, "Osztrogonác Csaba" <oszi at inf.u-szeged.hu>:
> Hi,
>
> On 2017.05.11. 0:32, Andy Estes wrote:
>>>  On May 10, 2017, at 3:17 PM, Michael Catanzaro <mcatanzaro at igalia.com> wrote:
>>>
>>>  On Tue, May 9, 2017 at 2:57 PM, Ryosuke Niwa <rniwa at webkit.org> wrote:
>>>>  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.
>>>
>>>  In that case, I think I'll go with RELEASE_ASSERT_NOT_REACHED() for now... since otherwise GCC is going to complain in release builds if there is no return statement. (Won't clang complain about that too?)
>>
>>  Clang won’t complain if the switch is exhaustive and all cases return.
>>
>>  Andy
>
> It isn't possible to check if all cases return if there is
> no default label in the switch, see these comments for details:
> https://bugs.webkit.org/show_bug.cgi?id=150842#c3

If you create enum value that is outside of range of defined enum members,
like 2 in your example, it's already unspecified behavior.

In case of enum with holes it may be correct, but I don't think we have many
of those.

>
> GCC's warning is correct and clang won't complain,
> but undefined behaviour can happen in runtime.
>
> br,
> Ossy
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> https://lists.webkit.org/mailman/listinfo/webkit-dev

-- 
Regards,
Konstantin


More information about the webkit-dev mailing list