How to treat Warning as Error in Visual Studio during build – Part 2

In the previous article you have learnt about how to treat warning messages as errors in Visual Studio. In this article I will explain how to treat selective warnings as error.

How to treat Warning as Error in Visual Studio during build – Part 2

Warning messages do not harm build activity. Instead they provide additional insight and quality controls on your codebase. Warning messages can tell you about obsolete code, unused variables, and many other items that you wouldn’t necessarily see on visual inspection. Warnings can also surface bugs, such as possible null reference exceptions, or expressions etc.

In general, each warning message reported has a different meaning that must be investigated and fixed. But most of the time they are ignored. However it might affect your code stability or performance. As per the good debugging practice, you must consider warning messages as errors and must be cleared by fixing them safely. To make this happen, we must have some mandatory mechanism to consider warning messages as errors.

In the previous article, I have explained how to convert all warning messages as errors. But sometimes not all the warning errors have a negative impact on the application. In this case we can choose selective warning messages (level3) and inform the compiler to treat them as error.

To do this we must have the list of warning messages and its corresponding error codes. The Microsoft repository provides a list of Warning messages and solution. Yow would need to refer these list and prepare the list of warning code to considered as error.

In the below image, I have selected CS0108 and CS0164 warnings.

Once the list is prepared, perform the below steps.

  • Right click on the project you want to enable this on and select the ‘Properties’ in context menu.
  • Select the “Build” tab.
  • Change “Treat warnings as errors” to “Specific Warning” and enter the numeric warning code into the input box. If there are multiple warning code to be treated as error, then enter the multiple numeric code with comma (,) separated

– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Recommended:   How to display detailed output of MSBuild in Visual Studio output Window
Share this:

We will be happy to hear your thoughts

Leave a reply