[英]resharper custom pattern replacement with negative equality expression
我在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
並replace with != null
。
如果不是真的強制要求,你可以放棄這條規則,因為根據這篇文章 :
“編譯器將空比較替換為對HasValue的調用,因此沒有真正的區別。只要做一個更具可讀性/對你和你的同事更有意義的事情。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.