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 you wouldn’t necessarily see on visual inspection. Sometimes, warning messages notifies 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, the developer ignores the warning messages. However, it might affect your code stability or performance. As per the good debugging practice, you must consider warning messages as errors and 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 into errors. But sometimes, not all the warning errors harm the application. In this case, we can choose selective warning messages (level3) and inform the compiler to treat them as errors.

To do this, we must have a list of warning messages and their corresponding error codes. The Microsoft repository provides a list of Warning messages and solution. Yow would need to refer to this list and prepare the list of warning codes to be considered as an 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.

Share this:
We will be happy to hear your thoughts

      Leave a reply