查看以下代码:
private void Foo(object bar)
{
Type type = bar.GetType();
if (type != null) // Expression is always true
{
}
}
Run Code Online (Sandbox Code Playgroud)
Resharper声称type永远不会null.这对我来说很明显,因为总会有类型bar,但Resharper如何知道?怎么知道方法的结果永远不会null.
Type不是一个结构,所以它不可能.如果方法是由我编写的,那么返回值当然可以是null(不一定是GetType,而是其他东西).
Resharper是否足够聪明,只知道那个特定的方法结果永远不会是null?(就像有一个已知的.Net方法的硬编码列表,永远不会返回null)
Mat*_*ira 14
JetBrains完美地解释了ReSharper如何在其功能列表中执行此操作.
链接摘要(这个特殊问题是关于NotNullAttribute):
我们分析了.NET Framework类库以及NUnit Framework的很大一部分,并使用JetBrains.Annotations命名空间中的一组自定义属性,通过外部XML文件对其进行了注释,具体为:
StringFormatMethodAttribute (for methods that take format strings as parameters)
InvokerParameterNameAttribute (for methods with string literal arguments that should match one of caller parameters)
AssertionMethodAttribute (for assertion methods)
AssertionConditionAttribute (for condition parameters of assertion methods)
TerminatesProgramAttribute (for methods that terminate control flow)
CanBeNullAttribute (for values that can be null)
NotNullAttribute (for values that can not be null)
UsedImplicitlyAttribute (for entities that should not be marked as unused)
MeansImplicitUseAttribute (for extending semantics of any other attribute to mean that the corresponding entity should not be marked as unused)
Run Code Online (Sandbox Code Playgroud)
是的,它基本上了解一些众所周知的方法.你也应该找到相同的字符串连接,例如:
string x = null;
string y = null;
string z = x + y;
if (z == null)
{
// ReSharper should warn about this never executing
}
Run Code Online (Sandbox Code Playgroud)
现在通过代码合同也可以获得相同的信息- 我不知道JetBrains是否直接挂钩这些信息,有自己的数据库,还是两者的混合.
| 归档时间: |
|
| 查看次数: |
4949 次 |
| 最近记录: |