問題描述
在 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 中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!