Exclude CsWinRT-required partial types from MA0204

Merged
CO
2mo ago
+183-2

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.