Skip to content

Commit 5b469e1

Browse files
committed
Fixed null comparison.
1 parent 579d67f commit 5b469e1

4 files changed

+43
-7
lines changed

Diff for: src/LightResults.Extensions.GeneratedIdentifier/GeneratedIdentifierSourceGenerator.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,13 @@ public override int GetHashCode()
382382
/// <returns><c>true</c> if the instances are equal; otherwise, <c>false</c>.</returns>
383383
public static bool operator ==({{symbolName}}? left, {{symbolName}}? right)
384384
{
385-
return left is not null && left.Equals(right);
385+
if (left is null && right is null)
386+
return true;
387+
if (left is null || right is null)
388+
return false;
389+
if (ReferenceEquals(left, right))
390+
return true;
391+
return left.Equals(right);
386392
}
387393
388394
"""
@@ -409,7 +415,13 @@ public override int GetHashCode()
409415
/// <returns><c>true</c> if the instances are not equal; otherwise, <c>false</c>.</returns>
410416
public static bool operator !=({{symbolName}}? left, {{symbolName}}? right)
411417
{
412-
return left is null || !left.Equals(right);
418+
if (left is null && right is null)
419+
return false;
420+
if (left is null || right is null)
421+
return true;
422+
if (ReferenceEquals(left, right))
423+
return false;
424+
return !left.Equals(right);
413425
}
414426
415427
"""

Diff for: src/LightResults.Extensions.GeneratedIdentifier/LightResults.Extensions.GeneratedIdentifier.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<!-- Output -->
3737
<PropertyGroup>
3838
<AssemblyName>LightResults.Extensions.GeneratedIdentifier</AssemblyName>
39-
<Version>9.0.0-preview.9</Version>
39+
<Version>9.0.0-preview.10</Version>
4040
<AssemblyVersion>9.0.0.0</AssemblyVersion>
4141
<FileVersion>9.0.0.0</FileVersion>
4242
<NeutralLanguage>en-US</NeutralLanguage>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithNamespace.verified.txt

+14-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ public sealed partial class TestStringId :
9191
/// <returns><c>true</c> if the instances are equal; otherwise, <c>false</c>.</returns>
9292
public static bool operator ==(TestStringId? left, TestStringId? right)
9393
{
94-
return left is not null && left.Equals(right);
94+
if (left is null && right is null)
95+
return true;
96+
if (left is null || right is null)
97+
return false;
98+
if (ReferenceEquals(left, right))
99+
return true;
100+
return left.Equals(right);
95101
}
96102

97103
/// <summary>Determines whether two instances of <see cref="TestStringId" /> are not equal.</summary>
@@ -100,7 +106,13 @@ public sealed partial class TestStringId :
100106
/// <returns><c>true</c> if the instances are not equal; otherwise, <c>false</c>.</returns>
101107
public static bool operator !=(TestStringId? left, TestStringId? right)
102108
{
103-
return left is null || !left.Equals(right);
109+
if (left is null && right is null)
110+
return false;
111+
if (left is null || right is null)
112+
return true;
113+
if (ReferenceEquals(left, right))
114+
return false;
115+
return !left.Equals(right);
104116
}
105117

106118
/// <inheritdoc />

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithoutNamespace.verified.txt

+14-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ public sealed partial class TestStringId :
8989
/// <returns><c>true</c> if the instances are equal; otherwise, <c>false</c>.</returns>
9090
public static bool operator ==(TestStringId? left, TestStringId? right)
9191
{
92-
return left is not null && left.Equals(right);
92+
if (left is null && right is null)
93+
return true;
94+
if (left is null || right is null)
95+
return false;
96+
if (ReferenceEquals(left, right))
97+
return true;
98+
return left.Equals(right);
9399
}
94100

95101
/// <summary>Determines whether two instances of <see cref="TestStringId" /> are not equal.</summary>
@@ -98,7 +104,13 @@ public sealed partial class TestStringId :
98104
/// <returns><c>true</c> if the instances are not equal; otherwise, <c>false</c>.</returns>
99105
public static bool operator !=(TestStringId? left, TestStringId? right)
100106
{
101-
return left is null || !left.Equals(right);
107+
if (left is null && right is null)
108+
return false;
109+
if (left is null || right is null)
110+
return true;
111+
if (ReferenceEquals(left, right))
112+
return false;
113+
return !left.Equals(right);
102114
}
103115

104116
/// <inheritdoc />

0 commit comments

Comments
 (0)