Fix MA0204 false positive on types with partial members
What
The documentation of MA0204 says it reports a partial type "when the type has a single declaration and no partial members", but the analyzer never checked the members. Removing partial from such a type produces an error (CS0751: a partial member must be declared in a partial type):
partial class Sample // MA0204 reported, the fix breaks the build
{
public partial int P { get; }
public partial int P { get => 1; }
}
partial class Sample // MA0204 reported, the fix breaks the build
{
partial void M();
}
Expected: no diagnostic. Actual: MA0204 is reported.
Fix
The analyzer doesn't report the type when one of its members (method, property, indexer, event, constructor) has the partial modifier. The nested types are ignored, as a nested partial type doesn't require the containing type to be partial (the existing PartialClass_WithNestedPartialType_ReportsDiagnostic test still passes).
The check is done on the declaration syntax, which the analyzer already uses to find the partial keyword: the type has a single declaration, so all its members are in this node. This works the same way for every supported Roslyn version, whereas the symbol APIs to detect a partial property, event or constructor don't exist in the older Roslyn versions.
The existing test PartialClass_WithPartialMethod_NoDiagnostic expected the diagnostic despite its name (and didn't check the fix, which would have produced CS0751). It is updated to expect no diagnostic.
The documentation now shows a compliant partial type with a partial member.
Tests
- Updated
PartialClass_WithPartialMethod_NoDiagnostic. - Added
PartialStruct_WithPartialMethodImplementation_NoDiagnostic,PartialClass_WithPartialProperty_NoDiagnosticandPartialClass_WithPartialIndexer_NoDiagnostic(C# 13),PartialClass_WithPartialEvent_NoDiagnosticandPartialClass_WithPartialConstructor_NoDiagnostic(C# 14). - The new tests failed before the fix.
RemoveUnnecessaryPartialModifierAnalyzerTestspasses on roslyn5.9 (22 tests), roslyn4.14 (20 tests) and roslyn4.8 (18 tests).