I have a rule in resharper to find calls to Nullable.HasValue
T? foo;
//...
if(foo.HasValue)
{}
//And it offers to replace with a comparison directly with null:
if(foo != null)
{}
This works great, but when it encounters a negated .HasValue
, the result is a little strange.
if(!foo.HasValue) {}
//is replaced with
if(!(foo != null)) {}
Then resharper wants me to simplify the statement to just if(foo == null)
//ideally it would automatically get to this without the extra step:
if(foo == null) {}
The rule is defined as:
type: System.ValueType or derived
nullable: expression of type System.Nullable<$type$>
search pattern:
$nullable$.HasValue
replace pattern:
$nullable$ != null
('Format after replace' and 'Shorten references' are both checked)
Is there a way I can write this rule so ReSharper handles it intelligently? I tried making a second rule for !$nullable$.HasValue
, but that causes both rules to match which makes tool-tip suggestions look confusing: replace with == null
and replace with != null
.