我在resharper中有一个规则来查找对Nullable.HasValue的调用
T? foo;
//...
if(foo.HasValue)
{}
//And it offers to replace with a comparison directly with null:
if(foo != null)
{}
这很有效,但当它遇到否定的.HasValue
时,结果有点奇怪。
if(!foo.HasValue) {}
//is replaced with
if(!(foo != null)) {}
然后resharper希望我将语句简化为if(foo == null)
//ideally it would automatically get to this without the extra step:
if(foo == null) {}
规则定义为:
type: System.ValueType or derived
nullable: expression of type System.Nullable<$type$>
search pattern:
$nullable$.HasValue
replace pattern:
$nullable$ != null
('替换后'格式'和'缩短参考'都被选中)
有没有办法可以编写此规则,以便ReSharper智能地处理它?我尝试为!$nullable$.HasValue
制定第二条规则,但这会导致两条规则匹配,这使得工具提示建议看起来很混乱:replace with == null
和replace with != null
。
答案 0 :(得分:0)
如果不是真的强制要求,您可以放弃此规则,因为根据此post:
“编译器将空比较替换为对HasValue的调用,因此没有真正的区别。只要做一个更具可读性/对你和你的同事更有意义的事情。”