Skip to content

'left' and 'right' in generated equality operators should be nullable. #40053

@ronnygunawan

Description

@ronnygunawan

Version Used: 16.4.0 Preview 6.0

Steps to Reproduce:

  1. Paste following code:
#nullable enable
using System;

namespace N {
    public class C {
        public int X;
    }
}
  1. Run Generate Equals and GetHashCode... on C, leave Generate operators checked.

Expected Behavior:
Generated operators:

public static bool operator ==(C? left, C? right) => EqualityComparer<C?>.Default.Equals(left, right);
public static bool operator !=(C? left, C? right) => !(left == right);

Actual Behavior:
Generated operators:

public static bool operator ==(C left, C right) => global::System.Collections.Generic.EqualityComparer<global::N.C>.Default.Equals(left, right);
public static bool operator !=(C left, C right) => !(left == right);

Which then cause this code to produce warning:

C? c = null;
if (c == null) { // Possible null reference argument for parameter 'left' in 'bool C.operator ==(C left, C right)'.
}

Metadata

Metadata

Assignees

Labels

Area-IDEBugFeature - Nullable Reference TypesNullable Reference TypesResolution-Not ReproducibleThe described behavior could not be reproduced by developershelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions