我在 resharper 中有一条规则来查找对 Nullable.HasValue 的调用
T? foo;
//...
if(foo.HasValue)
{}
//And it offers to replace with a comparison directly with null:
if(foo != null)
{}
这很好用,但是当它遇到一个 negate.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
.