我在resharper中有一个规则来查找对Nullable.HasValue的调用
T? foo;
//...
if(foo.HasValue)
{}
//And it offers to replace with a comparison directly with null:
if(foo != null)
{}
Run Code Online (Sandbox Code Playgroud)
这很有效,但当遇到否定时.HasValue,结果有点奇怪.
if(!foo.HasValue) {}
//is replaced with
if(!(foo != null)) {}
Run Code Online (Sandbox Code Playgroud)
然后resharper希望我简化声明 if(foo == null)
//ideally it would automatically get to this without the extra step:
if(foo == null) {}
Run Code Online (Sandbox Code Playgroud)
该规则定义为:
type: System.ValueType or derived
nullable: expression of type System.Nullable<$type$>
search pattern:
$nullable$.HasValue
replace pattern:
$nullable$ != null
Run Code Online (Sandbox Code Playgroud)
('替换后格式'和'缩短参考'都被选中)
有没有办法可以编写这个规则,以便ReSharper智能地处理它?我尝试制定第二条规则!$nullable$.HasValue,但这会导致两条规则匹配,这使得工具提示建议看起来令人困惑:replace with == null和replace with != null.