Skip to content

C# 14: add field keyword tests (field-backed properties) - #4165

Merged
AArnott merged 7 commits into
masterfrom
csharp14-field-keyword-tests
Oct 8, 2026
Merged

AArnott merged 7 commits into
masterfrom
csharp14-field-keyword-tests

Conversation

@AArnott

@AArnott AArnott commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Part of #4046 (C# 14 audit), checklist item #4028 (field-backed properties). Tests only. Independent master-based PR.

The product side of #4028 already landed in #4118: SA1500/SA1513 allow } = value; after an accessor list. That superseded @bjornhellander's 9afd3de13 and 40995f2f2. This PR adds the remaining piece: his field keyword test commit a351a7ef8 (bjornhellander/NewStyleCopAnalyzers#92), cherry-picked with -x and his authorship kept. It covers field used in expressions across 14 rules:

  • SA1000, SA1003, SA1008, SA1009
  • SA1010, SA1011
  • SA1019, SA1020, SA1021, SA1022, SA1023, SA1024
  • SA1101: field doesn't need a this. prefix
  • SA1119

The test additions merged into the existing C# 14 test classes with no other changes.

I also probed field-backed properties against master with SA1101, SA1201, SA1500, SA1502, SA1513, SA1516, SA1600, SA1609 and SA1623 (expression- and block-bodied accessors, ??= on field, @field referring to a real field named field, and initializers after accessor lists). No unexpected diagnostics.

Local: all 16 added tests pass on C# 14 and C# 15.

From bjornhellander/NewStyleCopAnalyzers#32.

Test changes only.

(cherry picked from commit a351a7ef8645e75ee2894e23c0aad1e8fac8d26a)
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.50%. Comparing base (33b604e) to head (dd5f2c1).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4165      +/-   ##
==========================================
+ Coverage   97.46%   97.50%   +0.03%     
==========================================
  Files        1172     1224      +52     
  Lines      127299   129248    +1949     
  Branches     4206     4209       +3     
==========================================
+ Hits       124073   126024    +1951     
+ Misses       2114     2112       -2     
  Partials     1112     1112              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AArnott
AArnott marked this pull request as ready for review October 8, 2026 06:09
# Conflicts:
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp14/SpacingRules/SA1000CSharp14UnitTests.cs
# Conflicts:
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp14/SpacingRules/SA1000CSharp14UnitTests.cs
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp14/SpacingRules/SA1008CSharp14UnitTests.cs
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp14/SpacingRules/SA1009CSharp14UnitTests.cs
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp14/SpacingRules/SA1019CSharp14UnitTests.cs
# Conflicts:
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp14/SpacingRules/SA1008CSharp14UnitTests.cs
#	StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp14/SpacingRules/SA1009CSharp14UnitTests.cs
public int Prop
{
get => field;
set =>field= value;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why it would be desirable to ignore spacing rules around the field keyword. Whether field is a keyword in later C# versions or interpreted as a local variable, it ought to be spaced properly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the old test name was misleading. Nothing ignores spacing around field. SA1000 only checks the space after specific keywords like new, return or checked. field is used like an identifier, so SA1000 doesn't require a space after it, the same as for any name. The bad spacing in that snippet (=>field= value) is operator spacing, which is SA1003's job. That test only ran SA1000, so it never showed SA1003 reporting.

In 5dc9cd6:

  • I renamed the SA1000 test to TestFieldKeywordIsSpacedLikeAnIdentifierAsync and gave it properly spaced code with field in identifier positions (return field;, field++, field.CompareTo(0), (field)).
  • I added SA1003CSharp14UnitTests.TestFieldKeywordAssignmentAsync. It's a theory over field and this.storage, and both report and fix => and = the same way.

The other spacing rules already had tests in this PR showing they report next to field: SA1008/SA1009 (field (9)), SA1010/SA1011 (field [0]), SA1019 (field .X), SA1020 (field ++), SA1021/SA1022 and SA1023/SA1024. None of the analyzers needed a change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the point of the original test was to check that SA1000 does NOT concern itself with spacing around the field keyword. So it contained a number of scenarios that COULD HAVE triggered if the analyzer was implemented incorrectly. Not a very strong test though. I think a better way that could be tried later on is to add tests for incorrect screnarios which run ALL analyzers at once. Much easier in such a case to see that there's no overlap between rules.


[Fact]
[WorkItem(4028, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/4028")]
public async Task TestFieldKeywordDoesNotRequireThisPrefixAsync()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if field is an explicitly declared field in user code? If C# binds against it then we should flag it as requiring this. I expect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I added tests for this in dd5f2c1, and the analyzer already handles every case correctly, so it didn't need a change:

  • C# 14, field inside an accessor, with a member named field: binds to the synthesized backing field. The compiler warns CS9258 ("the 'field' keyword binds to a synthesized backing field… use 'this.field' or '@field'"). SA1101 doesn't report. The test now enables compiler warnings, so it asserts CS9258 at both uses.
  • Same type, field outside the accessor: binds to the member. SA1101 reports, and the fix gives this.field.
  • C# 14, @field inside the accessor: binds to the member with no CS9258. SA1101 reports, and the fix gives this.@field.
  • C# 13, field inside the accessor: binds to the member with no compiler warning. SA1101 reports both uses and fixes them.

This works because SA1101 decides from semantic binding. At C# 14, the field keyword parses as a FieldExpression node, not a SimpleName, so SA1101 never sees it. Every other spelling is a SimpleName that binds to the member, so it gets the normal this. check. The tests pass in the C# 14 and C# 15 test projects.

SA1000 only governs the spacing after specific keywords. The contextual
'field' keyword is used like an identifier, so SA1000 neither requires nor
forbids a space after it. Replace the misleading "is ignored" test (which
contained bad operator spacing that SA1000 does not own) with properly
spaced code using 'field' in identifier positions.

Add an SA1003 test showing that bad spacing around '=>' and '=' next to
'field' is reported and fixed exactly as it is next to an ordinary member
access.
- C# 14: 'field' inside an accessor binds to the synthesized backing field
  (CS9258 when a member named 'field' exists), so SA1101 does not report it.
  Outside accessors 'field' binds to the member and SA1101 reports and fixes.
- C# 14: '@field' inside an accessor binds to the member; SA1101 reports and
  the fix produces 'this.@field'.
- C# 13: 'field' inside an accessor binds to the member; SA1101 reports.

The analyzer already decides from semantic binding (the C# 14 keyword is a
FieldExpression, not a SimpleName), so no product change is needed.
@AArnott
AArnott enabled auto-merge October 8, 2026 18:44
@AArnott
AArnott merged commit ed25530 into master Oct 8, 2026
25 checks passed
@AArnott
AArnott deleted the csharp14-field-keyword-tests branch October 8, 2026 18:47
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.

2 participants