silencing warning "control reaches end of non-void function"

Miklos Vajna vmiklos at collabora.co.uk
Mon Jul 31 07:57:53 UTC 2017


Hi Lionel,

On Sun, Jul 30, 2017 at 07:53:21PM +0200, Lionel Elie Mamane <lionel at mamane.lu> wrote:
> On Sun, Jul 30, 2017 at 07:26:02PM +0200, Lionel Elie Mamane wrote:
> 
> > Consider:
> 
> > enum t {a, b};
> > 
> > OUString f(t i)
> > {
> >   switch(i)
> >   {
> >   case t::a;
> >     return "it was an a";
> >   case t::b;
> >     return "it was a b";
> >   }
> > }
> > 
> 
> > gcc -Werror fails with
> > error: control reaches end of non-void function [-Werror=return-type]
> 
> An Internet search
> https://stackoverflow.com/questions/33607284/control-reaches-end-of-non-void-function-with-fully-handled-case-switch-over-a
> teaches me that it is legal to construct a value of an enum type that
> is not among the enumerated values (when the number of enumerated
> values is not a power of two). <sigh>
> 
> I added the fake return. I prefer not to put a default case in the
> switch, so that the "switch is not exhaustive over enum" warning can
> still appear.

sal/osl/all/compat.cxx uses:

for (;;) { std::abort(); }

to avoid such cases. If this is a common pattern in the codebase, maybe
it can be extracted to a named macro, something like SAL_UNREACHABLE,
which translates to __builtin_unreachable() for gcc/clang, and perhaps
there is an MSVC equivalent.

Regards,

Miklos
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://lists.freedesktop.org/archives/libreoffice/attachments/20170731/e4987f04/attachment.sig>


More information about the LibreOffice mailing list