Wrong compiler warning when comparing a struct type (not simple type) to null - by JeppeSN1

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 768940 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 10/28/2012 1:21:25 AM
Access Restriction Public


When a struct overloads the > (greater then) operator (user-defined operator), then comparing a value of the struct to null using the "lifted" > operator produces a wrong warning (warning message is all wrong).

Less importantly, when the struct overloads the == (equality) operator, no warning is produced when comparing a value to ==.

See also http://stackoverflow.com/q/13046115/1336654 
Sign in to post a comment.
Posted by Anthony [MSFT] on 2/14/2013 at 2:56 AM
Hi Jeppe,

I'm Anthony. I work on a future version of the C# compiler we're developing as part of the "Roslyn" project. We have not announced any schedule for release of "Roslyn" however I will say that this issue has been fixed in this version of the C# compiler. The code in the linked StackOverflow post now produces:

warning CS0464: Comparing with null of type 'System.DateTime?' always produces 'false'

We also now report a similar warning whenever a non-nullable value type is compared to 'null' using == or !=. You'd be surprised how many people check to see if CancellationTokens are null when writing asynchronous methods now not realizing that CancellationToken is a struct and hence will never be null - a lot of people. You can actually observe this a little if you go to http://msdn.com/roslyn and download the latest "Roslyn" CTP. If you use the compiler APIs to create a C# compilation you'll see this warning is now reported.


-Anthony D. Green, Program Manager, Visual Basic & C# Languages Team
Posted by Microsoft on 10/28/2012 at 7:00 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Microsoft Visual Studio Connect Support Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Macy [MSFT] on 10/28/2012 at 1:52 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)