问题描述
在 Visual Studio 中,我可以选择将警告视为错误"选项,以防止在出现任何警告时编译我的代码.我们的团队使用此选项,但我们希望保留两个警告作为警告.
有一个禁止警告的选项,但我们确实希望它们显示为警告,所以这不起作用.
似乎获得我们想要的行为的唯一方法是在特定警告"文本框中输入每个 C# 警告编号的列表,除了我们希望视为警告的两个.
除了令人头疼的维护之外,这种方法的最大缺点是一些警告没有数字,因此无法明确引用.例如,无法解析此引用.找不到程序集 'Data....'"
有人知道更好的方法吗?
<小时>为那些没有立即明白为什么这是有用的人澄清.想想大多数警告是如何工作的.他们告诉你,你刚写的代码有些不对劲.修复它们大约需要 10 秒,这使代码库更加整洁.
过时"警告与此大不相同.有时修复它意味着只使用一个新的方法签名.但是,如果整个类已经过时,并且您在数十万行代码中分散使用它,则可能需要数周或更长时间才能修复.您不希望构建被破坏那么久,但您肯定希望看到有关它的警告.这不仅仅是一个假设的案例——这已经发生在我们身上.
文字#warning"警告也是独一无二的.我经常想要检查它,但我不想破坏构建.
在 Visual Studio 2022 中,我们有一个新的项目属性 UI,其中包括一个编辑器.
在构建下 |错误和警告 如果您将 Treat warnings as errors 设置为 All,则会出现另一个属性,允许您免除特定警告被视为错误:p>
这会将以下属性添加到您的项目中:
<WarningsNotAsErrors>618,1030,1701,1702</WarningsNotAsErrors>
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.
Besides the maintenance headache, 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.
In Visual Studio 2022 we have a new Project Properties UI which includes an editor for this.
Under Build | Errors and Warnings if you set Treat warnings as errors to All, then another property appears which allows you to exempt specific warnings from being treated as errors:
This will add the following property to your project:
<WarningsNotAsErrors>618,1030,1701,1702</WarningsNotAsErrors>
这篇关于“将所有警告视为错误,除了..."在 Visual Studio 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!