Repository: dotnet/maui
File: src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs (C#)

Code before:
		Assert.Contains("global::System.Action<global::Test.TestPage, string?>", generated, StringComparison.Ordinal);
		
		// The pattern "if (value is null)" followed by "return;" should NOT appear together
		// This would indicate an early return that we don't want for nullable target properties
		var lines = generated.Split('\n');
		bool hasEarlyReturn = false;
		for (int i = 0; i < lines.Length - 1; i++)
		{
			if (lines[i].Contains("if (value is null)", StringComparison.Ordinal))
			{
				// Check if the next few lines contain a return statement
				for (int j = i + 1; j < Math.Min(i + 5, lines.Length); j++)
				{
					if (lines[j].Trim() == "return;")
					{
						hasEarlyReturn = true;
						break;
					}
					// If we hit the closing brace or another statement, stop looking
					if (lines[j].Contains("}", StringComparison.Ordinal) || lines[j].Contains("if (", StringComparison.Ordinal))
					{
						break;
					}
				}
			}
		}
		Assert.False(hasEarlyReturn, "Setter should NOT have early return for nullable target property");
	}

	[Fact]
	public void NullableValueTypeAtEndOfConditionalAccessPath()
	{
		// Test nullable value type property (int?) at the end of a binding path with conditional access
		// The setter should NOT have an early return since the target property accepts null
		var xaml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
	xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
	xmlns:test="clr-namespace:Test"
	x:Class="Test.TestPage"
	x:DataType="test:TestPage">
	<Label Text="{Binding Container.OptionalCount}"/>
</ContentPage>
""";

		var code =
"""
#nullable enable
using System;

Review comment (suggestion):
@copilot Isn't there some Assert which would test a match for a simple regex? Please use that if available. If not, calculate `hasEarlyReturn` via regex matching instead. Apply this to other tests which use the same pattern.

Code after:
		Assert.Contains("global::System.Action<global::Test.TestPage, string?>", generated, StringComparison.Ordinal);
		
		// Verify the pattern "if (value is null)" followed by "return;" does NOT appear
		// This would indicate an early return that we don't want for nullable target properties
		var earlyReturnPattern = @"if\s*\(\s*value\s+is\s+null\s*\)\s*\{\s*return\s*;";
		Assert.DoesNotMatch(earlyReturnPattern, generated);
	}

	[Fact]
	public void NullableValueTypeAtEndOfConditionalAccessPath()
	{
		// Test nullable value type property (int?) at the end of a binding path with conditional access
		// The setter should NOT have an early return since the target property accepts null
		var xaml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
	xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
	xmlns:test="clr-namespace:Test"
	x:Class="Test.TestPage"
	x:DataType="test:TestPage">
	<Label Text="{Binding Container.OptionalCount}"/>
</ContentPage>
""";

		var code =
"""
#nullable enable
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Test;

[XamlProcessing(XamlInflator.SourceGen)]
public partial class TestPage : ContentPage
{
	public Container? Container { get; set; }
}

public class Container
{
	public int? OptionalCount { get; set; }  // Nullable int
}
""";

		var (result, generated) = RunGenerator(xaml, code);
		
		Assert.False(result.Diagnostics.Any(d => d.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error));
		Assert.NotNull(generated);