Repository navigation
C# 14: add field keyword tests (field-backed properties) - #4165
Conversation
From bjornhellander/NewStyleCopAnalyzers#32. Test changes only. (cherry picked from commit a351a7ef8645e75ee2894e23c0aad1e8fac8d26a)
Codecov Report✅ All modified and coverable lines are covered by tests. 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:
|
# 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
TestFieldKeywordIsSpacedLikeAnIdentifierAsyncand gave it properly spaced code withfieldin identifier positions (return field;,field++,field.CompareTo(0),(field)). - I added
SA1003CSharp14UnitTests.TestFieldKeywordAssignmentAsync. It's a theory overfieldandthis.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.
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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,
fieldinside an accessor, with a member namedfield: 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,
fieldoutside the accessor: binds to the member. SA1101 reports, and the fix givesthis.field. - C# 14,
@fieldinside the accessor: binds to the member with no CS9258. SA1101 reports, and the fix givesthis.@field. - C# 13,
fieldinside 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.
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: hisfieldkeyword test commit a351a7ef8 (bjornhellander/NewStyleCopAnalyzers#92), cherry-picked with-xand his authorship kept. It coversfieldused in expressions across 14 rules:fielddoesn't need athis.prefixThe 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,
??=onfield,@fieldreferring to a real field namedfield, and initializers after accessor lists). No unexpected diagnostics.Local: all 16 added tests pass on C# 14 and C# 15.