How can I disable certain compiler warnings?
As you try to reach the nirvana of a "clean build spew" (having no warnings in your build process), it will become evident that some warnings are unavoidable. While we don't encourage the behavior of disabling compiler warnings, we recognize that it's pragmatic to do so sometimes.
There are different ways of disabling warnings, depending on whether you're using VS and what kind of project you have.
VS, C# Project
The C# project system has a nice UI for managing project properties. On the "Build" tab, there's a "Suppress Warnings" control where you can enter a comma-delimited list of warning numbers (without the letter prefix).
VS, VB Project
The VB project system has a nice UI for managing project properties. On the "Compile" tab, you'll find a variety of controls for manipulating compiler diagnostics on the subtleties of code correctness. The most common options are there, but you might still run into some warnings that you can't code around and still need to disable. Unfortunately, there is no "back door" inside the IDE to add warnings to ignore or use other random command-line parameters for the compiler.
Thus, in the case of a VB project, you'll need to modify the project file directly as outlined below in the
'MSBuild Project' case.
MSBuild Project
If you're using a
MSBuild project with either the normal
Microsoft.VisualBasic.targets or the
Microsoft.CSharp.targets files (or a VB project you're using with Visual Studio), the compiler attributes map to the
NoWarn property. Setting this property to a comma-delimited list the compiler's warning numbers to ignore will make sure the compiler gets them and they don't generate the diagnostic messages.
In some cases with the shipping .targets files the
NoWarn property already has some warnings listed in it. It's for this reason you'll need to make sure you account for the case of an empty or non-empty property list, and append (or prepend) the comma only if necessary.
You'll modify your local project file instead of the global .targets file. The easiest way to make the modifications without conflicting with other things going on in the project is to put these statements in a separate
PropertyGroup.
[<PropertyGroup>]
[<NoWarn Condition="'$(NoWarn)'!=''">$(NoWarn),</NoWarn>]
[<NoWarn>$(NoWarn)42300</NoWarn>]
[</PropertyGroup>]
The condition in the first property set will make sure that you get a comma at the end of the list if you need it, and the second property set is where you can put the warnings you want to ignore. Also, make sure your new
PropertyGroup is added after those at the top that belong to your project configurations.
VBC & CSC Tasks
If you're not using the normal
Microsoft.VisualBasic.targets or the
Microsoft.CSharp.targets file, you'll need to set the attributes on the task invocations for the VB & C# compiler tasks. The
MSBuild tasks CSC & VBC use the
DisabledWarnings attribute. Set this to a comma-delimited list of warning numbers (without the letter prefix) where you invoke the task.
You can use the pattern above in the
'MSBuild Project' case, just substitute
DisabledWarnings in place of
NoWarn.
Team Build
In the Team Build proj file find the
CustomPropertiesForBuild and add '
NoWarn=42300' to it. This will pass the
NoWarn property to the MSBuild project files for the projects in the build. You can also specify it only for some solutions by using the
Properties metadata item of the
SolutionToBuild ItemGroup.
Note: The topic here also generally applies to forcing specific warnings to be errors, in which case you'd use the
TreatWarningsAsErrors property instead of the
NoWarn property.