为什么 CodeContracts 静态分析器不发出警告?
我有几个用户控件,它们订阅了另一个类中的事件处理程序。 我正在学习 C# 中的 CodeContracts,我想知道为什么静态分析器允许编写这样的代码:
void MyUserControl_MouseEnter(object sender, MouseEventArgs e)
{
MyUserControl item = sender as MyUserControl;
item.DoSomething(); // I expect some warning here, because item can be null
sender.Equals(testObject); // This doesn't yield warning either
}
这里我有一个潜在不安全的代码,可能会导致空引用异常。 我知道,静态分析器可能无法检查 sender
的实际类型是什么。但如果它无法证明这一点,我预计会出现一些警告,例如 CodeContracts:可能在空引用上调用方法。
还是我对合同的理解有误?我如何才能收到此类错误的通知?
UPD:
是的,我确实按照答案中的建议启用了隐式非空义务
,但我仍然没有收到来自静态分析器的警告。我还尝试使用 Microsoft All Rules
规则集运行代码分析,也没有警告。 (但我更喜欢处理代码契约并使用 Contract 类执行一些额外的检查,而不是使用 if-then- throw 或其他东西)
I have several user controls, that are subscribed to the event handler in another class.
I'm learning CodeContracts in C#, and I wonder, why does Static Analyzer allows writing code like this:
void MyUserControl_MouseEnter(object sender, MouseEventArgs e)
{
MyUserControl item = sender as MyUserControl;
item.DoSomething(); // I expect some warning here, because item can be null
sender.Equals(testObject); // This doesn't yield warning either
}
Here I have a potentially unsafe code, that can lead to null-reference exception.
I understand, that static analyzer probably cannot check, what will the actual type of sender
be. But in case it cannot prove it, I expect some warning, like CodeContracts: Possibly calling a method on a null reference
.
Or do I get some idea of contracts wrong? How can I get notified of errors like this?
UPD:
Yes, I did enable Implicit Non-Null Obligation
as it was suggested in the answers, but I still don't get a warning from Static Analyzer. Also I tried to run Code Analysis with Microsoft All Rules
rules set, also no warning. (But I'd prefer dealing with Code Contracts and perform some additional checks using Contract class, rather then with if-then-throw or something else)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该在静态分析器选项(项目选项|代码分析)中启用“隐式非空义务”。
You should enable "Implicit Non-Null obligations" in the static analyzer options (Project Options|Code Analysis).
“我怎样才能收到这样的错误通知?”:在这种情况下,Resharper 会警告您。
如果“要求”对象非空,代码契约将警告您该对象可能为空。您要求对对象取消引用隐式“要求”,这从表面上看似乎很合理,但出于某种原因,CC 似乎没有提供。http://msdn.microsoft.com/en-us/library/dd264808.aspx 说它确实执行了这样的隐性合同。我正在进一步研究。
RedHat 比我先一步。更详细信息:您应该在项目属性的“代码合同”选项卡中选中“静态检查”下的“隐式非空义务”框。
"How can I get notified of errors like this?": Resharper will warn you in that case.
Code contracts will warn you that the object might be null if there is a "Requires" that the object be non-null. You're asking for an implicit "Requires" for an object dereference, which seems reasonable on the face of it, but which CC for whatever reason doesn't seem to provide.The documentation at http://msdn.microsoft.com/en-us/library/dd264808.aspx says that it does enforce such an implicit contract. I'm looking into it further.
RedHat beat me to it. More detail: You should check the "Implicit Non-Null Obligations" box under "Static Checking" in the Code Contracts tab of your project properties.
我有类似的问题。我必须在与“隐式非空义务”复选框相同的面板上打开警告级别滑块。
I had a similar problem. I had to turn up the warning level slider on the same panel as the "Implicit Non-Null obligations" checkbox.
在项目的属性页面的代码分析选项卡上,您可以更改规则。
In properties page of your project on Code Analysis tab you can change Rules.