20 December 2011

Treating most warnings as errors

A common scenario in C# I find is that I would like to:

  1. Treat most warnings as errors by default
  2. But, treat a few specific warnings as just warnings.

I still want them to show up as warnings, which is to say that I don't want them to disappear altogether. 'CS0618 Obsolete' is a good example of this.

This cannot be achieved through the C# Project Properties editor, but it can be done by manually editing the .csproj file to add these two MSBuild properties:

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>618</WarningsNotAsErrors>

This is vaguely referred to in the docs here, and the list of error codes can be found here.

No comments: