resharper自定义模式替换与否定等式表达式

dan*_*dan 6 c# resharper

我在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 == nullreplace with != null.

lex*_*x87 0

如果不是真正强制性的,您可以放弃此规则,因为根据这篇文章

“编译器将空比较替换为对 HasValue 的调用,因此没有真正的区别。只需执行对您和您的同事来说更具可读性/更有意义的操作即可。”