Avoid code warnings being missed or ignored : Treating Warnings as Errors in Visual Studio

Avoid code warnings being missed or ignored : Treating Warnings as Errors in Visual Studio

While developing application often we ignored several “build warnings and only focused on fixing the “build errors”. What does those warning means? Are they really meant for ignore ? Or We are creating defects for future ?  When compilers compile the code and that can’t be built, Visual Studio shows the build error and In case of success it shows the build succeeded . However, the compiler also shows warnings when the code compilation  is successfully but there are areas which may need attention or violating some rules.

Most of the time, we ignored them or try to fix them later point when they come up in build report or as hidden defects.  As a better practice we can stop avoid ignoring them  and treat them as same priority as error.

 

Related Post : Did you know – You can play sound when Build Succeeded or Failed in Visual Studio ?

 

Any compiler messages (compiler level warning) that would come up as warning will now display as errors; so that developer can fix them to move ahead.

 

Let’s take an example of Compiler Warning (level 3) CS1718 – Comparison made to same variable; did you mean to compare something else? .. If you meant to compare to something else, then you should simply correct the statement. Refer to this post for the details of warning.. and we often ignore this first time as warning.

 

image

 

Related Post : Back to Basic – Building Solutions in Visual Studio – Build Vs. Rebuild

 

If you try to build this code, you will get an warning  as “Comparison made to same variable; did you mean to compare something else?

image

Which seems to be a Warning, but could be potential error .  So, it always better to be set them as error to avoid late comer defects.

 

Must Read : Back to Basic : Displaying detailed output of MSBuild in Visual Studio output Window

 

From the Solution Explorer, Right click on the project –> Project Properties –> Navigate to Build Tab and then focus on the section called “Treat warnings as error

image

By default it is set to “None” – This will continue showing all warning message with same preference . If you Select “All”, the compiler will start throwing error for the same warning messages.

Well, now select it as “All

image

And then rebuild the same code base; Visual Studio will start throwing the error for the same.

image

If you want these to be works only on specific warning instead all ; you can select the “’Specific warnings” options and specify the warning code without “cs”. For multiple; please provide comma separated code.

image

Hope this helps and you will start exploring this.

Thanks.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

3 Comments to “Avoid code warnings being missed or ignored : Treating Warnings as Errors in Visual Studio”

Comments are closed.