Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct types with custom equality operators doesn't report CS0019 error when compared with null. #21659

Closed
zlatanov opened this issue Aug 22, 2017 · 5 comments

Comments

@zlatanov
Copy link
Contributor

Version Used:
Visual Studio 15.3.1

Steps to Reproduce:

  1. Create a dummy struct Value such as:
struct Value
{
    public static Boolean operator ==( Value left, Value right ) => true;
    public static Boolean operator !=( Value left, Value right ) => false;
}
  1. Then the following Main procedure:
static void Main()
{
    var value = new Value();

    // No Error CS0019  Operator '!=' cannot be applied to operands of type 'Value' and '<null>'
    if ( value == null )
    {
        // This line seems OK to the analyzer, but even the compiler is removing it.
        // Setting up break point here is removed to the next line when debugging.
        Console.WriteLine( "This is not even compiled." );
     }
}

Expected Behavior:
The compiler or analyzer should issue CS0019 error.

Actual Behavior:
The code is built without any error and the code block inside the if is omitted.

This really stung me in one of my projects where I had to make what once was a class to a struct and I expected that the compiler will warn me of places where I compared it to null or not null.

If the value type doesn't have the custom operators == and != the code doesn't compile and the expected error CS0019 is given.

@zlatanov zlatanov changed the title Struct type with custom operator == and Struct types with custom equality operators no longer report CS0019 error when compared with null. Aug 22, 2017
@jcouv jcouv added this to the 15.5 milestone Aug 24, 2017
@jcouv jcouv self-assigned this Aug 24, 2017
@jcouv
Copy link
Member

jcouv commented Aug 27, 2017

@zlatanov Just to confirm, you don't think this is a regression, correct?

I tried this on csc.exe version 1.3.2 (which shipped in the latest update of Visual Studio 2015) and also the previous/native csc.exe compiler and I get the same behavior.

@jcouv jcouv modified the milestones: 15.later, 15.5 Aug 27, 2017
@zlatanov
Copy link
Contributor Author

@jcouv No, I don't think this is a regression. Just never had the case before where something was class that I needed it to be a struct ( with custom operators ) to notice it.

@jcouv jcouv changed the title Struct types with custom equality operators no longer report CS0019 error when compared with null. Struct types with custom equality operators doesn't report CS0019 error when compared with null. Aug 28, 2017
@jcouv
Copy link
Member

jcouv commented Aug 28, 2017

Thanks for the clarification. Updated the title.
Adding a new warning is a breaking change, and so needs to be considered as part of warning waves (see #1580 for broader workitem).

@jcouv jcouv modified the milestones: 15.later, Unknown Oct 19, 2017
@jcouv jcouv added the New Feature - Warning Waves Warning Waves label Aug 10, 2020
@jcouv jcouv modified the milestones: Backlog, 16.8 Aug 10, 2020
@jcouv
Copy link
Member

jcouv commented Aug 11, 2020

Now that we have warning waves, we should be able to warn for this. I'll check with Neal when he's back.

@jcouv jcouv modified the milestones: 16.8, Compiler.Next Aug 27, 2020
@gafter
Copy link
Member

gafter commented Sep 3, 2020

This is already implemented in C# 9 at warning level 5. See CS8073 in https://github.com/dotnet/roslyn/blob/master/docs/compilers/CSharp/Warnversion%20Warning%20Waves.md

@gafter gafter closed this as completed Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

4 participants