Fix MA0048 only_validate_first_type when the first type is a delegate

Merged
ME
2mo ago
+27-3

What

With MA0048.only_validate_first_type = true, a delegate is not counted as the first type of a file, so the next type is validated as well:

// Test0.cs
delegate void Test0();
class Bar {}
  • Expected: no diagnostic. The first type, Test0, matches the file name.
  • Actual: MA0048 on Bar.

The first-type check used a local IsTypeDeclaration function that only matched BaseTypeDeclarationSyntax. It hid the static helper of the class, which also matches DelegateDeclarationSyntax and is already used to list the top-level types for the prefix options.

Fix

Removed the local function, so the first-type check uses the shared IsTypeDeclaration helper and treats delegates as type declarations.

Tests

  • Added MatchOnlyFirstType_Delegate and MatchOnlyFirstType_DelegateNotMatchingFileName (a delegate that doesn't match is still reported, and the class after it isn't). Both failed before the fix.
  • FileNameMustMatchTypeNameAnalyzerTests: 68/68 passed on roslyn5.9 and roslyn4.8.