Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get VS2008 to stop warning me about unreachable code?

I have a few config options in my application along the lines of

const bool ExecuteThis=true; const bool ExecuteThat=false; 

and then code that uses it like

if(ExecuteThis){ DoThis(); } if(ExecuteThat){ DoThat(); } //unreachable code warning here 

The thing is, we may make slightly different releases and not ExecuteThis or ExecuteThat and we want to be able to use consts so that we don't have any speed penalties from such things at run time. But I am tired of seeing warnings about unreachable code. I'm a person that likes to eliminate all of the warnings, but I can't do anything about these. Is there some option I can use to turn just these warnings off?

like image 298
Earlz Avatar asked Dec 18 '09 21:12

Earlz


People also ask

What is unreachable code detected?

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.

Why is my code unreachable C++?

Unreachable code is a program code fragment which is never executed. Don't mix it up with dead code which is, unlike unreachable code, a code fragment that can be executed but whose result is never used in any other computation.


1 Answers

To disable:

#pragma warning disable 0162 

To restore:

#pragma warning restore 0162 

For more on #pragma warning, see MSDN.

Please note that the C# compiler is optimized enough to not emit unreachable code. This is called dead code elimination and it is one of the few optimizations that the C# compiler performs.

And you shouldn't willy-nilly disable the warnings. The warnings are a symptom of a problem. Please see this answer.

like image 125
jason Avatar answered Sep 21 '22 07:09

jason