Possible InvalidOperationException - is this a bug?

Resharper 6.0, VS 2010.

CircleYN is type "bool?"

Code:

if (row.CircleYN.HasValue && row.CircleYN.Value)

row.CircleYN.Value is underlined in blue and Resharper says: Possible System.InvalidOperationException. I may be having brain fade here but if I've checked HasValue how can that be right?

If I take Resharper's suggested action it changes the code to:
if (row.CircleYN != null && (row.CircleYN.HasValue && row.CircleYN.Value))

but there's still a blue line!


PS Off-topic rant, sorry: This WYSIWG editor is horrible and I'm getting script errors in IE9.

6 comments
Comment actions Permalink

Checking that row isn't null doesn't help as row is declared in a foreach statement (
foreach

(var row in query)). If I check that row isn't null Resharper rightly says the expression is always true.
0
Comment actions Permalink

Hello Stephen,

In case 'row.CircleYN' is a property, one cannot reliably tell that it doesn't
return null both times you call it in your 'if' condition. This means that
when you check row.CircleYN.HasValue, it doesn't imply that the next call
to that property (row.CircleYN.Value) will not return null. In order to avoid
this warning and a possible problem, you can extract row.CircleYN to a local
variable:

var circleYN = row.CircleYN;
if(circleYN.HasValue && circleYN.Value)
...

However, ReSharper's suggestion is a bit incorrect in this case and we'll
discuss if this problem can be addressed in the future. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Resharper 6.0, VS 2010.

CircleYN is type "bool?"

Code:

if (row.CircleYN.HasValue && row.CircleYN.Value)

row.CircleYN.Value is underlined in blue and Resharper says: Possible
System.InvalidOperationException. I may be having brain fade here but
if I've checked HasValue how can that be right?

If I take Resharper's suggested action it changes the code to:
if (row.CircleYN != null && (row.CircleYN.HasValue &&
row.CircleYN.Value))
but there's still a blue line!

PS Off-topic rant, sorry: This WYSIWG editor is horrible and I'm
getting script errors in IE9.

---
Original message URL:
http://devnet.jetbrains.net/message/5441906#5441906




0
Comment actions Permalink

Thank you Andrey for the quick reply!

I understand what you're saying (I imagine it's a real edge case though) and have got rid of the blue line.

FYI, CircleYN is indeed a property, from a Linq To Sql row object.

Thanks again and thanks for a great product.

0
Comment actions Permalink

I agree with Stephen in that this is a real edge case.  More often than not, this will never be the case.  If you have a nullable int? property (or DateTime?, or decimal?, you name it), you know that calling the myInt.HasValue it's going to give you a correct answer every time.

I just upgraded from ReSharper 5, where these warning messages were not showing up, to ReSharper 6 where they are now showing up all over the place.  The biggest issue I have with this is that the "Possible 'System.InvalidOperationException'" warning can be a very useful warning in normal cases, but not in this edge case.  So I am forced to either ignore all of the edge-case warnings, or demote the severity to Suggestion, which could potentially cause me to ignore "real" issues.  Creating local temp variables for every single one of my nullable properties is also somewhat obnoxious, especially if I know exactly what those properties are going to look like.

Is there any way to only demote the edge-case variety of this warning, but somehow leave the real issues as warnings?  Even as I type that it sounds somewhat absurd.  I think the better solution would be to have this edge case be a different warning altogether.  Thoughts?

0
Comment actions Permalink

I've been thinking about this some more and I suppose it may show up when multi-threading? I'm thinking that a different error message with perhaps a different severity would indeed be the best resolution.

0
Comment actions Permalink

If you're worrying about multi-threading, any variable that isn't locked, etc could be changed from null to not null or vice versa between check and use.

0

Please sign in to leave a comment.