Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect Resharper Suggestion - "Merge Conditional Expression"

Given an oracle query which is returning a single string value, to get the value from the query I use the following two lines:

var result = cmd.ExecuteOracleScalar();
return result != null ? ((OracleString)result).Value : null;

The "!= null" in this statement is underlines with the suggestion to "Merge Conditional Expression". If I accept the suggestion it changes it to:

return ((OracleString)result).Value;

Which throws an exception because the value returned will be null for a number of executions.

Is there anyway to use the ternary operator but not have this warning?

Note that if I change the code to:

var result = cmd.ExecuteOracleScalar();
if (result == null)
    return null;
return ((OracleString)result).Value;

Resharper then first suggests that I "Convert to Return Statement" which just changes it back to use the ternary operator.

Any Suggestions?

like image 730
Wrightboy Avatar asked Oct 19 '22 14:10

Wrightboy


1 Answers

This looks like exactly the bug identified in RSRP-434610:

  • given original code that checks an object reference for nullity, and accesses a property of the object if the object reference is not null
  • R# proposes a refactoring that always accesses the property, and will therefore fail when the object reference is null

The issue has a fix version of 9.1, which was released just a few days ago, although watch out trying to upgrade from within VS.

like image 160
AakashM Avatar answered Oct 22 '22 19:10

AakashM