From 95df4acd945e35dbd3c0ef22e0bc5a7c0d801baf Mon Sep 17 00:00:00 2001
From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com>
Date: Wed, 29 Oct 2025 14:27:51 +0000
Subject: [PATCH 1/3] fix: resolve CS8620 and CS8619 warnings for
Task.FromResult with nullable and non-nullable types
---
TUnit.Assertions.Tests/Bugs/Issue3580Tests.cs | 122 ++++++++++++++++++
.../Conditions/StringEqualsAssertion.cs | 16 ++-
TUnit.Assertions/Extensions/Assert.cs | 9 +-
.../Extensions/AssertionExtensions.cs | 15 ---
...Has_No_API_Changes.DotNet10_0.verified.txt | 6 +-
..._Has_No_API_Changes.DotNet8_0.verified.txt | 5 +-
..._Has_No_API_Changes.DotNet9_0.verified.txt | 6 +-
...ary_Has_No_API_Changes.Net4_7.verified.txt | 5 +-
8 files changed, 156 insertions(+), 28 deletions(-)
create mode 100644 TUnit.Assertions.Tests/Bugs/Issue3580Tests.cs
diff --git a/TUnit.Assertions.Tests/Bugs/Issue3580Tests.cs b/TUnit.Assertions.Tests/Bugs/Issue3580Tests.cs
new file mode 100644
index 0000000000..69238daf1b
--- /dev/null
+++ b/TUnit.Assertions.Tests/Bugs/Issue3580Tests.cs
@@ -0,0 +1,122 @@
+namespace TUnit.Assertions.Tests.Bugs;
+
+///
+/// Tests for issue #3580: More warnings CS8620 and CS8619 after update to >= 0.72
+/// https://github.com/thomhurst/TUnit/issues/3580
+///
+public class Issue3580Tests
+{
+ [Test]
+ public async Task Task_FromResult_With_NonNullable_Value_And_Nullable_Expected_Should_Not_Cause_Warnings()
+ {
+ // Scenario 1: Both parameters nullable (from issue)
+ object? value = "test";
+ object? expected = "test";
+
+ // This should not produce CS8620 or CS8619 warnings
+ await Assert.That(Task.FromResult(value)).IsEqualTo(expected);
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_NonNullable_Value_And_NonNullable_Expected_Should_Not_Cause_Warnings()
+ {
+ // Scenario 2: Non-nullable value with nullable expectation (from issue)
+ object value = "test";
+ object? expected = "test";
+
+ // This should not produce CS8620 or CS8619 warnings
+ await Assert.That(Task.FromResult(value)).IsEqualTo(expected);
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_Both_NonNullable_Should_Not_Cause_Warnings()
+ {
+ // Scenario 3: Both non-nullable (from issue)
+ object value = "test";
+ object expected = "test";
+
+ // This should not produce CS8619 warnings
+ await Assert.That(Task.FromResult(value)).IsEqualTo(expected);
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_String_Should_Not_Cause_Warnings()
+ {
+ // Test with string type (reference type)
+ var value = "hello";
+ var expected = "hello";
+
+ await Assert.That(Task.FromResult(value)).IsEqualTo(expected);
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_ValueType_Should_Not_Cause_Warnings()
+ {
+ // Test with value type (int)
+ var value = 42;
+ var expected = 42;
+
+ await Assert.That(Task.FromResult(value)).IsEqualTo(expected);
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_Null_Value_Should_Work()
+ {
+ // Test with null value
+ object? value = null;
+
+ var result = await Task.FromResult(value);
+ await Assert.That(result).IsNull();
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_Custom_Type_Should_Not_Cause_Warnings()
+ {
+ // Test with custom type
+ var value = new TestData { Value = "test" };
+ var expected = value;
+
+ var result = await Task.FromResult(value);
+ await Assert.That(result).IsSameReferenceAs(expected);
+ }
+
+ [Test]
+ public async Task Async_Method_Returning_NonNullable_Task_Should_Not_Cause_Warnings()
+ {
+ // Test with async method that returns non-nullable task
+ // Just verify we can await it and get a non-null result
+ var result = await GetNonNullableValueAsync();
+
+ await Assert.That(result).IsNotNull();
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_IsNotNull_Should_Not_Cause_Warnings()
+ {
+ // Test IsNotNull assertion
+ object value = "test";
+
+ var result = await Task.FromResult(value);
+ await Assert.That(result).IsNotNull();
+ }
+
+ [Test]
+ public async Task Task_FromResult_With_IsGreaterThan_Should_Not_Cause_Warnings()
+ {
+ // Test numeric comparison
+ var value = 10;
+
+ await Assert.That(Task.FromResult(value)).IsGreaterThan(5);
+ }
+
+ private static async Task