Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid "Unreachable code" warning for preprocessor-dependent code

I'm trying to figure out if there's any way to avoid getting an "Unreachable code" warning for something that's caused by the preprocessor. I don't want to suppress all such warnings, only those which will be dependent on the preprocessor, e.g.

#if WINDOWS
    public const GamePlatform platform = GamePlatform.PC;
#else
    public const GamePlatform platform = GamePlatform.MAC;
#endif

And later on there's code that goes:

if (platform == GamePlatform.PC)
{
    ...
}
else
{
    ...
}

One of those two sections will always be detected as "Unreachable code", and we've got those all over the place. I'd like to try and get rid of the many warnings it creates, but I still want to get warnings for legitimately unreachable code. (In reality, there's more than just two platforms, so each chunk of platform-specific code creates a pile of unnecessary warnings.)

like image 201
Darrel Hoffman Avatar asked Jul 26 '13 21:07

Darrel Hoffman


1 Answers

Option 1: Add the preprocessor macros wherever you have the if-statements. This will be more performant, but perhaps a bit uglier.

Option 2: Make the platform variable not const. Setting it to static readonly made the warning go away for me.

like image 180
lukegravitt Avatar answered Nov 05 '22 04:11

lukegravitt