Skip to content

Review impact of record structs on existing rules #3979

Activity

  1. added theissue type on Nov 29, 2025
  2. sharwell commented on Dec 3, 2025

    @sharwell
    MemberAuthor

    Audit by Deep Research

    StyleCopAnalyzers: C# 10 record struct Support Audit

    C# 10 introduces record struct and readonly record struct types. StyleCopAnalyzers must recognize these as distinct type declarations and apply existing rules consistently.

    This audit lists impacted rules, required changes, and test coverage. Rules not mentioned were assessed as unaffected.


    SA1201: Elements Must Appear in the Correct Order

    Implementation

    • SA1201 enforces the order of top-level elements and type members.
    • RecordStructDeclaration nodes are now mapped to the struct category (same as regular struct).

    This ensures:

    • A record struct is ordered alongside struct declarations, not with classes.
    • Error messages correctly distinguish “record struct” vs “class” when reporting misordering.

    Documentation

    • SA1201 documentation lists the standard ordering (classes, structs, etc.).
    • It may be worth clarifying that “structs” include record structs.

    Tests

    • Ordering scenarios involving record struct and classes, such as:

      • Misordered record struct before/after a class.
      • Messages correctly mentioning “record struct”.

    SA1202: Elements Must Be Ordered by Access

    Implementation

    • SA1202 enforces ordering by access level among adjacent members of the same type.
    • Support was extended to include RecordStructDeclaration in its target syntax kinds.

    Result:

    • Members of a record struct are ordered just like members of a regular struct (e.g., public before internal, etc.).

    Documentation

    • No specific change required, as the concept is the same for all types.

    Tests

    • Extend existing SA1202 tests to include record struct members with differing access modifiers.

    SA1205: Partial Elements Must Declare Access

    Implementation

    • SA1205 requires partial type declarations to specify an access modifier.
    • The analyzer already covers all TypeDeclarationSyntax (class, struct, interface, etc.), and now also includes record class and record struct.

    Result:

    • partial record struct without an access modifier is flagged.
    • Code fix logic understands the new keywords.

    Documentation

    • Clarify that all partial type declarations (including record types) must declare access.
    • Optionally add an example using partial record struct.

    Tests

    • Valid example:

      public partial record struct Foo;

      – no SA1205 diagnostic.

    • Invalid example:

      partial record struct Foo;

      – SA1205 diagnostic expected.


    SA1300: Element Must Begin with Uppercase Letter

    Implementation

    • SA1300 enforces PascalCase for type names (classes, structs, interfaces, etc.).
    • The analyzer now registers for RecordStructDeclaration along with other type declarations.

    Result:

    • record struct exampleRecord is flagged by SA1300.
    • record struct ExampleRecord is accepted.

    Documentation

    • Existing text already applies to “type names”.
    • Optionally mention that record types are included for clarity.

    Tests

    • Add tests ensuring record struct names obey the uppercase-first rule.

    SA1313: Parameter Names Must Begin with Lowercase Letter

    Implementation

    • SA1313 expects parameters to be camelCase, but C# records use PascalCase positional parameters in primary constructors because they become properties.
    • Previously, SA1313 exempted primary constructor parameters on record classes.
    • It now also exempts primary constructor parameters on record structs by checking when a parameter’s parent is a RecordDeclaration or RecordStructDeclaration.

    Result:

    • For:

      public record struct Point(int X, int Y);

      SA1313 does not report diagnostics on X and Y.

    Documentation

    • Documentation should mention that positional record parameters (for both record classes and record structs) are allowed to be PascalCase.

    Tests

    • Regression tests for the issue where SA1313 was incorrectly emitted for record struct parameters.

    • Examples like:

      public record struct Point(int X, int Y);

      must pass without SA1313 diagnostics.


    SA1402: File May Only Contain a Single Type

    Implementation

    • SA1402 can be configured to enforce one type per file for certain categories.

    • Implementation was updated so that:

      • Record classes count as “class”.
      • Record structs count as “struct”.

    This resolves a bug where record types were ignored in the file-type counting logic.

    Behavior

    • Under default settings:

      • A file containing a class and a record struct may be allowed, depending on configuration (structs are often allowed with classes by default).
    • Under stricter settings:

      • A file containing a class and a record struct may trigger SA1402 if configuration requires separate files for those categories.

    Documentation

    • Clarify that:

      • “Classes” include record classes.
      • “Structs” include record structs.

    Tests

    • Dedicated C# 10 tests (e.g., SA1402ForRecordStructCSharp10UnitTests) that:

      • Verify record structs participate in SA1402 checks.
      • Confirm no regressions or crashes.

    SA1642: Constructor Summary Documentation Must Begin with Standard Text

    Implementation

    • SA1642 checks <summary> documentation for constructors, verifying it starts with standard text:

      • For classes: “Initializes a new instance of the … class.”
      • For structs: “Initializes a new instance of the … struct.”
    • Initially, record structs were misidentified as classes, causing SA1642 to suggest “class” in the message.

    • The logic was updated so that a constructor whose containing type is a RecordStructDeclaration is treated as a struct.

    Result:

    • Constructors in record structs are expected to use “struct” in the standard summary text.

    Documentation

    • SA1642 documentation already distinguishes class vs struct phrasing.

    • Consider adding a note:

      Record structs follow the “struct” wording for SA1642.

    Tests

    • Constructors in record structs with correct vs incorrect summary text:

      • Confirm correct struct text is accepted.
      • Confirm incorrect “class” text is flagged.

    SA1649: File Name Must Match Type Name

    Implementation

    • SA1649 enforces that when a file contains a single type, the file name must match the type name.

    • Previously, encountering a record struct caused an exception:

      • “Unhandled declaration kind: RecordStructDeclaration”.
    • The helper that extracts the type name was extended to handle RecordStructDeclaration.

    Result:

    • Record structs are treated like any other top-level type for SA1649.
    • record struct MyRecordStruct should live in MyRecordStruct.cs (under default configuration).

    Documentation

    • Clarify that SA1649 applies to:

      • Classes
      • Structs
      • Interfaces
      • Record classes
      • Record structs

    Tests

    • Files containing a single record struct:

      • Matching file name → no SA1649 diagnostic.
      • Non-matching file name → SA1649 diagnostic.
      • Ensure no exceptions are thrown when analyzing record structs.

    Summary

    All of the above rules have been updated to recognize and properly handle record struct and readonly record struct declarations. StyleCopAnalyzers now:

    • Applies ordering rules consistently,
    • Enforces naming and file naming expectations,
    • Handles documentation text correctly,
    • Avoids crashes on record struct syntax.

    Documentation should be reviewed for any references to “classes” or “structs” and updated where necessary to explicitly include record types.

    Sources

  3. self-assigned this
    on Dec 3, 2025
  4. moved this from Backlog to In progress in StyleCopAnalyzerson Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions