views:

2736

answers:

8

In Visual Studio, I can select the "Treat warnings as errors" option to prevent my code from compiling if there are any warnings. Our team uses this option, but there are two warnings we would like to keep as warnings.

There is an option to suppress warnings, but we DO want them to show up as warnings, so that won't work.

It appears that the only way to get the behavior we want is to enter a list of every C# warning number into the "Specific warnings" text box, except for the two we want treated as warnings.

I am checking a file in to source control to keep track of which ones we are treating as errors


C# 3.5 warning numbers to treat as errors:

183,184,197,420,465,602,626,657,658,672,684,688,809,824,1058,1060,1200,
1201,1202,1203,1522,1570,1574,1580,1581,1584,1589,1590,1592,1598,1607,
1616,1633,1634,1635,1645,1658,1682,1683,1684,1685,1687,1690,1691,1692,
1694,1695,1696,1697,1699,1707,1709,1720,1723,1911,1956,1957,2002,2014,
2023,2029,3000,3001,3002,3003,3004,3005,3006,3007,3008,3009,3010,3011,
3012,3013,3014,3015,3016,3017,3018,3022,3023,3026,3027,5000,108,114,162,
164,251,252,253,278,279,280,435,436,437,440,444,458,464,467,469,472,618,
652,728,1571,1572,1587,1668,1698,1710,1711,1927,3019,3021,67,105,168,169,
219,282,414,419,642,659,660,661,665,675,693,1700,1717,1718,28,78,109,402,
422,429,628,649,1573,1591,1610,1712,3024

C# 3.5 warning numbers that remain as warnings:

612 - X is obsolete
1030 - #warning This is a warning
1701, 1702 - Warnings that are suppressed by the C# compiler by 
    default and shouldn't show up at all.


The biggest disadvantage to this approach is that a few warnings do not have numbers, so they can't be referenced explicitly. For example, "Could not resolve this reference. Could not locate assembly 'Data....'"

Does anyone know of a better way to do this?


Clarifying for those who don't see immediately why this is useful. Think about how most warnings work. They tell you something is a little off in the code you just wrote. It takes about 10 seconds to fix them, and that keeps the code base cleaner.

The "Obsolete" warning is very different from this. Sometimes fixing it means just consuming a new method signature. But if an entire class is obsolete, and you have usage of it scattered through hundreds of thousands of lines of code, it could take weeks or more to fix. You don't want the build to be broken for that long, but you definitely DO want to see a warning about it. This isn't just a hypothetical case--this has happened to us.

Literal "#warning" warnings are also unique. I often want to check it in, but I don't want to break the build.

A: 

Why do you want to keep seeing warnings that you are not treating as errors? I am confused about why this is desirable - either you fix them or you don't.

Would two different build/solution files work - or a script to copy one and then so modify the warnings/warning level be suitable. It seems that perhaps you want some executions of the compiler to squawk, but others you want to keep going.

So different compiler switches seems like a good way to go. You could do this with different targets - one labeled debug or release and the others labeled suitably about the warnings.

Tim
Most warnings happen because of a simple confusion in my code (e.g. I declared a variable that I don't use).An Obsolete warning often happens because of a change in someone else's code. Fixing this could take weeks of development. #warning is a warning I want in the code (likely a long-term fix).
Neil Whitaker
In other words, there are warnings that I don't want to break my build. If #warning breaks the build, then I can't ever check one in. If Obsolete breaks the build, then another team we depend on could unknowingly break our team's build just by adding an obsolete attribute.
Neil Whitaker
A: 

It seems to me the root problem is really a combination of your treating warnings as errors, when they are clearly not, and your apparent policy of permitting check-ins which violate this. As you say, you want to be able to continue working despite a warning. You've only mentioned a few warnings you want to be able to ignore, but what if someone else on the team caused any other type of warning, which would take you equally long to fix? Wouldn't you want to be able to ignore that as well?

The logical solution would be to either 1) Disallow checkins if the code doesn't compile (which means those who created the warnings will have to fix them, since in effect, they broke the build), or 2) treat warnings as warnings. Create two build configurations, one which treats warnings as errors, which can be run regularly to ensure that the code is warning-free, and another, which only treats them as warnings, and allows you to work even if someone else introduced a warning.

jalf
Using the selected answer, it's easy to add to the list of warnings treated as warnings. That works much better than either of your proposed solutions. Warnings clearly are not errors, but treating most warnings as errors means code will never be checked in with those warnings.
Neil Whitaker
A: 

pragma warning (C# Reference)

pragma warning may be used to enable or disable certain warnings.

http://msdn.microsoft.com/en-us/library/441722ys(VS.80).aspx

+3  A: 

/warnaserror /warnaserror-:618

Thank you for your input. Even though they don't solve the IDE problem, these are the most helpful comments so far.
Neil Whitaker
+2  A: 

or more specifically, in your case:

/warnaserror /warnaserror-:612,1030,1701,1702

this should treat all warnings as errors except for the ones in your comma separated list

+14  A: 

You can add a WarningsNotAsErrors-tag in the project file.

<PropertyGroup>
  ...
  ...
  <WarningsNotAsErrors>618,1030,1701,1702</WarningsNotAsErrors>
</PropertyGroup>

Note: 612 and 618 are both warnings about Obsolete, don't know the difference but the project i'm working on is reporting Obsolete with warning 618.

SvenL
I had given up on this, but you found it. I just tested it, and it worked. Thank you!
Neil Whitaker
Sven you made my day
Rob Fonseca-Ensor
A: 

I'm using treat warnings as errors.

In a rare cases, when some acceptable warning shows up (i.e. referencing obsolete member, or missing documentation on XML serialization classes), then it has to be explicitly suppressed with #pragma disable (and optionally reason for not having a clean code could be provided as a comment along).

Presence of this directive also allows to find out, who accepted this warning violation (by "blame" action of version control) in case there are some questions.

Rinat Abdullin
I use this also, although it doesn't solve the problems mentioned in my description. I want certain warnings treated as warnings, not hidden.
Neil Whitaker
A: 

Why not simply have a rule saying "Whoever checks in code with any warning inside it other than 612, 1030, 1701 or 1702 in it must go to the whiteboard and write a hundred times 'I will not check in code with disallowed warnings again.'"

erikkallen