燮pressing"从未使用"和"永远不会分配给"在C#中的警告 [英] Suppressing "is never used" and "is never assigned to" warnings in C#

查看:708
本文介绍了燮pressing"从未使用"和"永远不会分配给"在C#中的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在托管code,基本上描述了旧的Windows ISAPI消费C#项目HTTPSystemDefinitions.cs文件。

这包括一整套相关的并不是所有或由code消耗的ISAPI结构。在编译时这些结构的所有字段成员造成像下面这样的警告: -


  

报警现场'UnionSquare.ISAPI.HTTP_FILTER_ prePROC_HEADERS.SetHeader从未分配,永远有它的默认值空



  

警告字段'UnionSquare.ISAPI.HTTP_FILTER_ prePROC_HEADERS.HttpStatus'从未使用过


难道这与禁用的#pragma警告禁用?如果是的话会显示相应的错误号码是什么?如果不是还有什么我能做什么?请记住,我只说我得到像看到这些警告,从其他文件来怎么办这个文件的,其重要的。

修改

例结构: -

 结构HTTP_FILTER_ prePROC_HEADERS
{
    //
    //对于SF_NOTIFY_ prePROC_HEADERS,检索指定头值。
    //头名称应包括末尾的':'。特殊值
    //'法','链接'和'版本'可以用来检索单个
    //请求行的部分
    //    内部GetHeaderDelegate GetHeader;
    内部SetHeaderDelegate SetHeader可以;
    内部AddHeaderDelegate的AddHeader;    UInt32的的HTTPStatus; //新在4.0,SEND_RESPONSE状态
    UInt32的dwReserved;在4.0 //新
}


解决方案

是的,这些都可以燮pressed。

通常情况下,我反对燮pressing警告,但在这种情况下,用于互操作的结构确实需要某些字段是present,即使你从来没有打算(或可以)使用它们,所以在这种情况下,我觉得应该是有道理的。

通常情况下,用晚餐preSS这两个警告,您将解决违规code。第一个(...从未使用过),通常是从早期版本的code的剩菜的code-气味。也许code已被删除,但场留下的。

第二个通常是code-闻之正确使用领域。例如,您可能会错误地写一个属性的新值回财产本身,从来没有写入支持字段。


要为共进晚餐preSS警告从未使用过场XYZ ,你这样做:

 的#pragma警告禁止0169
...字段声明
的#pragma警告恢复0169

要想喝preSS警告字段XYZ从未分配到,并且总是有它的默认值XX ,你这样做:

 的#pragma警告禁止0649
...字段声明
的#pragma警告恢复0649

要找到这样的警告自己的数字(即我怎么知道使用0169和0649),你这样做:


  • 编译code为正常,这将一些警告添加到Visual Studio中
  • 您的错误列表
  • 切换到输出窗口,并生成输出,并且寻找同样的警告

  • 从相关消息,这应该是这样的复制4位数的警告code:


      

    C:\\开发\\ VS.NET \\ ConsoleApplication19 \\ ConsoleApplication19 \\ Program.cs的(10,28):
          警告CS 0649 :字段'ConsoleApplication19.Program.dwReserved'是永远不会
          分配给,永远有它的默认值0




买者:按照注释通过 @乔恩汉娜,也许几警告是为了让这个情况,问题和答案的未来发现者。


  • 首先,也是最重要的,SUP $ P $的pssing警告行为类似于吞咽药片头痛。当然,这可能是有时做正确的事,但它不是一个包罗万象的解决方案。有时,头疼的是一个真实的现象,你不应该掩盖,同样有警告。它总是最好尝试通过固定自己的事业来对待警告,而不只是一味地从构建输出删除它们。

  • 说了这么多,如果你需要用晚餐preSS的警告,请按照我上面布置的格局。第一code线,的#pragma警告禁止XYZK ,禁用警告的该文件的其余部分的,或者至少要等到相应的的#pragma警告恢复XYZK 中找到。最小的行数禁用这些警告的。上面的图案禁用警告只是一条线。

  • 此外,作为乔恩提及,至于为什么你在做这个评论是一个好主意。禁用警告,绝对是一个code-气味时无故完成,评论将prevent未来的维护者从,甚至被删除,并试图修复警告花费时间或者不知道你为什么这样做。

I have a HTTPSystemDefinitions.cs file in C# project which basically describes the older windows ISAPI for consumption by managed code.

This includes the complete set of Structures relevant to the ISAPI not all or which are consumed by code. On compilation all the field members of these structures are causing a warning like the following:-

Warning Field 'UnionSquare.ISAPI.HTTP_FILTER_PREPROC_HEADERS.SetHeader' is never assigned to, and will always have its default value null

or

Warning The field 'UnionSquare.ISAPI.HTTP_FILTER_PREPROC_HEADERS.HttpStatus' is never used

Can these be disabled with #pragma warning disable? If so what would the corresponding error numbers be? If not is there anything else I can do? Bear in mind that I only what to do this for this file, its important that I get see warnings like these coming from other files.

Edit

Example struct:-

struct HTTP_FILTER_PREPROC_HEADERS
{
    //
    //  For SF_NOTIFY_PREPROC_HEADERS, retrieves the specified header value.
    //  Header names should include the trailing ':'.  The special values
    //  'method', 'url' and 'version' can be used to retrieve the individual
    //  portions of the request line
    //

    internal GetHeaderDelegate GetHeader;
    internal SetHeaderDelegate SetHeader;
    internal AddHeaderDelegate AddHeader;

    UInt32  HttpStatus;               // New in 4.0, status for SEND_RESPONSE
    UInt32  dwReserved;               // New in 4.0
}

解决方案

Yes, these can be suppressed.

Normally, I'm opposed to suppressing warnings, but in this case, structs used for interop absolutely requires some fields to be present, even though you never intend to (or can) use them, so in this case I think it should be justified.

Normally, to suppress those two warnings, you would fix the offending code. The first ("... is never used") is usually a code-smell of leftovers from earlier versions of the code. Perhaps code was deleted, but fields left behind.

The second is usually a code-smell for incorrectly used fields. For instance, you might incorrectly write the new value of a property back to the property itself, never writing to the backing field.


To suppress warnings for "Field XYZ is never used", you do this:

#pragma warning disable 0169
... field declaration
#pragma warning restore 0169

To suppress warnings for "Field XYZ is never assigned to, and will always have its default value XX", you do this:

#pragma warning disable 0649
... field declaration
#pragma warning restore 0649

To find such warning numbers yourself (ie. how did I know to use 0169 and 0649), you do this:

  • Compile the code as normal, this will add some warnings to your error list in Visual Studio
  • Switch to the Output window, and the Build output, and hunt for the same warnings
  • Copy the 4-digit warning code from the relevant message, which should look like this:

    C:\Dev\VS.NET\ConsoleApplication19\ConsoleApplication19\Program.cs(10,28): warning CS0649: Field 'ConsoleApplication19.Program.dwReserved' is never assigned to, and will always have its default value 0


Caveat: As per the comment by @Jon Hanna, perhaps a few warnings is in order for this, for future finders of this question and answer.

  • First, and foremost, the act of suppressing a warning is akin to swallowing pills for headache. Sure, it might be the right thing to do sometimes, but it's not a catch-all solution. Sometimes, a headache is a real symptom that you shouldn't mask, same with warnings. It is always best to try to treat the warnings by fixing their cause, instead of just blindly removing them from the build output.
  • Having said that, if you need to suppress a warning, follow the pattern I laid out above. The first code line, #pragma warning disable XYZK, disables the warning for the rest of that file, or at least until a corresponding #pragma warning restore XYZK is found. Minimize the number of lines you disable these warnings on. The pattern above disables the warning for just one line.
  • Also, as Jon mentions, a comment as to why you're doing this is a good idea. Disabling a warning is definitely a code-smell when done without cause, and a comment will prevent future maintainers from spending time either wondering why you did it, or even by removing it and trying to fix the warnings.

这篇关于燮pressing"从未使用"和"永远不会分配给"在C#中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆