Fix CS8602 nullable dereference in ToolbarBadgePage#34963
Fix CS8602 nullable dereference in ToolbarBadgePage#34963jfversluis merged 2 commits intonet11.0from
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 34963Or
iex "& { $(irm https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 34963" |
There was a problem hiding this comment.
Pull request overview
Fixes CS8602 nullable dereference warnings in ToolbarBadgePage by ensuring _statusLabel is initialized before it’s captured by toolbar item click handlers, restoring warning-free builds with TreatWarningsAsErrors=true.
Changes:
- Move
_statusLabelinitialization earlier in the constructor so lambda closures can safely reference it. - Remove the later duplicate
_statusLabelinitialization block.
|
/azp run maui-pr-uitests, maui-pr-devicetests |
|
Azure Pipelines successfully started running 2 pipeline(s). |
CI Failure AnalysisAll CI failures on this PR are pre-existing on Failures Fixed by This PR
Validation: Full solution build ( Pre-existing CI Failures (Not Related to This PR)1. WinUI UITests — All categories, 0/2500+ tests passing (app crash)
2. macOS Debug Build —
3. AOT macOS —
4. Windows Debug Build — File locking flake
|
|
/rebase |
Move _statusLabel initialization before the lambda closures that capture it, so the compiler can prove non-null at the dereference sites (lines 33, 46, 60). Fixes Samples windows and Samples macOS integration test failures introduced by #34669. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- UINSWindow.iOS.cs: Add null checks for PerformSelector and GetItem returns that are now nullable in macios SDK 26.2.11587 - AssertionExtensions.iOS.cs: Guard CGContext null from GetCurrentContext, use null-conditional on UIImage.Size - MauiTestApplicationDelegate.cs: Fix lambda sender parameter nullability to match updated MarshalManagedExceptionHandler/MarshalObjectiveCExceptionHandler delegate signatures Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
5ee1db7 to
6746c83
Compare
This reverts commit 4e6b863.
## Problem PR #34669 (ToolbarItem Badge Support) added Issue8305_Toolbar.cs with `[Issue(IssueTracker.Github, 8305, ...)]`, but Issue8305.cs (Shell Badge, from PR #34659) already uses issue number 8305. Both generated the same `DisplayName = "G8305"`, causing `VerifyNoDuplicates()` in `TestCases.cs` to throw `NotSupportedException` at app startup, crashing the **entire HostApp** and failing **ALL** Windows UI tests (not just toolbar tests). This crash has been present since build [1371858](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1371858) (PR #34669 merge). The follow-up PR #34963 fixed nullable warnings but did not address this crash. ## Root Cause `TestCases.cs:VerifyNoDuplicates()` (line 161-175) uses a HashSet to check for duplicate `DisplayName` values. When two `[Issue]` attributes share the same tracker and number with default `issueTestNumber=0`, they produce identical display names, triggering the crash. ## Fix 1. **Add `issueTestNumber: 1`** to `Issue8305_Toolbar`'s `[Issue]` attribute, producing unique `DisplayName = "G8305 (1)"` instead of duplicate `"G8305"` 2. **Replace non-existent icon images** (`envelope.png`, `bell.png`, `cart.png`) with images that exist in HostApp resources (`bank.png`, `calculator.png`, `shopping_cart.png`) ## Verification - Both `Controls.TestCases.HostApp` (Windows TFM) and `Controls.TestCases.Shared.Tests` build successfully - The `issueTestNumber` pattern is well-established (used by Issue11769, Issue13126_2, Issue1583_1, Issue28986_Border, and others) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Fix nullable dereference warnings (CS8602) in
ToolbarBadgePage.csthat break Samples integration tests on both Windows and macOS.Root Cause
PR #34669 ("Add BadgeText, BadgeColor, and BadgeTextColor support to ToolbarItem") introduced
ToolbarBadgePage.cswhere_statusLabelis initialized on line 68 but captured in lambda closures on lines 33, 46, and 60 — before the assignment. The compiler cannot prove non-null at the capture points, emitting CS8602 on all 3 lines across all TFMs.Fix
Move
_statusLabelinitialization before the lambda closures that reference it. No behavioral change — the label object is the same, just created earlier in the constructor.Validation
dotnet build Maui.Controls.Sample.csproj -f net11.0 -p:TreatWarningsAsErrors=true— 0 warnings, 0 errors