Repository navigation
Reject an expression-bodied property that also has a default value [patch] - #192
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
…atch] PropertyTemplate wrote the expression body and silently dropped DefaultValue, since C# cannot give an expression-bodied property an initialiser. Validate now refuses the combination before anything is written, so a class holding such a property is refused by AddClass up front too. Fixes #190 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW5kkYGVswnWUL9YgPE
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #190
What was wrong
When
ExpressionBodyFactorywas set,PropertyTemplate.WriteTotook the expression-body branch and never looked atDefaultValue.Validatedidn't reject the combination either. SoDefaultValue = "5"plusExpressionBodyFactory = cb => cb.Write("x")renderedint P => x;, and the= 5disappeared without an error. C# has no initialiser on an expression-bodied property, so there was no correct way to write both.Change
PropertyTemplate.Validatenow throwsInvalidOperationException, naming the property, whenExpressionBodyFactoryis set and the template has a default value (HasDefaultValue, which also counts a quoted empty string).ClassTemplate.Validatealready validates every member before writing, soAddClassnow refuses such a class before writing anything.<exception>doc onWriteTo.Tests
AnExpressionBodyWithADefaultValueIsRejectedBeforeAnythingIsWritten: the issue's repro throws, the message names'P', and the CodeBlocker keeps exactly what it held before.AClassWithAnExpressionBodiedPropertyThatHasADefaultValueWritesNothing: inside a class,AddClassthrows and nothing is written.PropertyTemplate.cschange reverted, both new tests fail ("no exception was thrown").Generated by Claude Code