Skip to content

Update SA1500 and SA1513 for property initializers after accessor lists (C# 14 field-backed properties) - #4118

Merged
AArnott merged 8 commits into
masterfrom
csharp14-field-property-initializer
Oct 7, 2026
Merged

AArnott merged 8 commits into
masterfrom
csharp14-field-property-initializer

Conversation

@AArnott

@AArnott AArnott commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes SA1500 and SA1513 false positives when a property initializer follows the closing brace of a multi-line accessor list. This is common with C# 14 field-backed properties:

public bool Property1
{
    get { return field; }
    set { field = value; }
} = default;

This builds on #4077 by @EdwinEngelen: his original commits are merged in unchanged (same SHAs and authorship). It also folds in @bjornhellander's equivalent fix from his fork (bjornhellander/NewStyleCopAnalyzers 9afd3de13, 40995f2f2), credited with Co-authored-by.

Stacked on #4117 (C# 14 test project). Only the merge of #4077 and the 2 commits after it belong to this PR. Once #4117 merges, this diff shrinks to just those changes.

Commits

  1. Merge of Update for property initializer in C# 14 #4077, which brings in Edwin's original commits as-is:
    • Add test project for c# 14 (b0af9b7ed): Edwin's copy of Björn's commit, which Add test project for C# 14 #4117 already has. The merge resolves it to the Add test project for C# 14 #4117 version of the C# 14 test project.
    • Upgrade NuGet package NuGet.Frameworks from 6.3.3 to 6.3.4 (0d3e41c6c)
    • Update SA1500 for property initializer (e65f52d1f)
    • Update SA1513 for property initializer (9b946abd9)
  2. Tests in C# 6. I confirmed locally that SA1500 and SA1513 report this on plain auto-properties too ({ get; set; } = 1; spread over several lines), which has been valid since C# 6. Per the per-language-version pattern, the regression tests go in the C# 6 test classes (SA1500UnitTests, SA1513UnitTests), so all later versions inherit them. Before the fix these tests fail with 2 diagnostics each; after it they pass.
  3. Tighter SA1500 condition. Update for property initializer in C# 14 #4077 allowed = after any closing brace. This commit limits it to a brace that closes an accessor list (case SyntaxKind.EqualsToken when token.Parent.IsKind(SyntaxKind.AccessorList)), the same check SA1513 uses. It also adds Björn's C# 14 test with a multi-line field accessor body.

The NuGet.Frameworks 6.3.3 → 6.3.4 bump in Test.CSharp8 from #4077 is kept. It's a harmless patch-level bump of a test-only dependency, and keeping it means Edwin's commit doesn't need a revert.

Validation

  • Linux, net6.0: Test.CSharp14 runs 8746 tests (8740 + 6 new) and the 6 new tests pass. The failing set matches the Add test project for C# 14 #4117 baseline exactly (known CRLF/LF artifacts). Windows CI is the real gate.

Part of #4028 (field-backed properties, under #4046). This doesn't close #4028: the rest of that audit still needs a pass over the other rules.
Supersedes #4077.

@AArnott
AArnott force-pushed the csharp14-field-property-initializer branch 2 times, most recently from 06d6065 to 17bcc2c Compare October 7, 2026 03:45
AArnott and others added 3 commits October 6, 2026 21:49
Merge Edwin Engelen's original commits from #4077 (b0af9b7, 0d3e41c,
e65f52d, 9b946ab) on top of the C# 14 test project from #4117.

Conflict resolution:
- b0af9b7 is Edwin's copy of Björn's "Add test project for c# 14"
  (ccb81f4, already in history). Keep the #4117 version of the C# 14 test
  project (csproj and SA1107 tests); its Azure Pipelines edits are dropped
  because master deleted build/build-and-test.yml and build/test.yml (#4105).
- SA1500CSharp14UnitTests.cs, SA1513CSharp14UnitTests.cs: take Edwin's tests
  on top of the empty C# 14 test classes.
- The NuGet.Frameworks 6.3.3 -> 6.3.4 bump (0d3e41c) merges cleanly and is
  kept: it is a patch-level bump of a test-only dependency.
Auto-property initializers after a multi-line accessor list have been valid since C# 6,
and SA1500/SA1513 reported them before this change. Put the regression tests in the C# 6
test classes so every language version runs them.
…eld accessor

Only allow '=' after a closing brace when the brace closes an accessor list, which matches
the SA1513 check. Also add a C# 14 test where a field-backed property has a multi-line
accessor body followed by an initializer.

Adapted from bjornhellander/NewStyleCopAnalyzers 9afd3de13 and 40995f2f2.

Co-authored-by: Björn Hellander <bjorn.hellander@gmail.com>
@AArnott
AArnott force-pushed the csharp14-field-property-initializer branch from 17bcc2c to c01a6ff Compare October 7, 2026 03:49
@AArnott
AArnott marked this pull request as ready for review October 7, 2026 14:13
@AArnott
AArnott merged commit 949aad4 into master Oct 7, 2026
21 checks passed
@AArnott
AArnott deleted the csharp14-field-property-initializer branch October 7, 2026 14:15
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.

Review impact of field-backed properties on existing rules

3 participants