Skip to content

Exclude CsWinRT-required partial types from MA0204 - #1451

Merged
meziantou merged 3 commits into
mainfrom
copilot/remove-unnecessary-partial-modifier
Sep 11, 2026
Merged

meziantou merged 3 commits into
mainfrom
copilot/remove-unnecessary-partial-modifier

Conversation

Copilot AI commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

MA0204 was flagging partial as unnecessary in multi-targeted MAUI/Windows scenarios where CsWinRT requires the type to remain partial for WinRT AOT/trimming compatibility. This produced contradictory guidance: MA0204 suggested removing partial, while the Windows build required it.

  • Analyzer behavior

    • Keep partial when the type may participate in CsWinRT AOT generation.
    • Detect common WinRT-mapped interfaces (for example IDisposable, collection interfaces, INotifyPropertyChanged, ICommand) and skip MA0204 for those types.
    • Preserve partial on containing types when a nested type requires it.
  • Scope of the exemption

    • Apply the exemption when CsWinRT support is present in the compilation.
    • Fall back to Windows-target detection from analyzer config build properties when CsWinRT types are not directly available.
  • Coverage

    • Add focused tests for:
      • a partial class implementing IDisposable
      • the non-Windows case, which should still report MA0204
      • a containing partial type with a nested CsWinRT-relevant type
  • Documentation

    • Update MA0204 docs to mention the CsWinRT/MAUI Windows exception.

Example:

public partial class CertificateHolder : IDisposable
{
    public void Dispose()
    {
    }
}

With this change, MA0204 no longer suggests removing partial from types like the above when that modifier may be required by CsWinRT on Windows-targeted builds.

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MA0204 by removing unnecessary partial modifier Exclude CsWinRT-required partial types from MA0204 Sep 10, 2026
Copilot AI requested a review from meziantou September 10, 2026 21:52
Comment thread src/Meziantou.Analyzer/Rules/RemoveUnnecessaryPartialModifierAnalyzer.cs Outdated
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI requested a review from meziantou September 11, 2026 05:45
@meziantou
meziantou marked this pull request as ready for review September 11, 2026 13:37
@meziantou
meziantou merged commit 4ad45cb into main Sep 11, 2026
13 checks passed
@meziantou
meziantou deleted the copilot/remove-unnecessary-partial-modifier branch September 11, 2026 13:37
This was referenced Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET MAUI project and the MA0204 "Remove unnecessary partial modifier" rule

2 participants