AVRO-4331: [csharp] Fix unreachable code in generated Get()/Put() - #3937
Open
GBNikola wants to merge 1 commit into
Open
AVRO-4331: [csharp] Fix unreachable code in generated Get()/Put()#3937GBNikola wants to merge 1 commit into
GBNikola wants to merge 1 commit into
Conversation
avrogen built the Get()/Put() switch body via CodeSnippetExpression, which CodeDom wraps in a CodeExpressionStatement and always suffixes with a ";". Since every switch arm returns or throws, that trailing ";" is unreachable. Roslyn stays quiet about it, but IDE analyzers with fuller flow analysis (ReSharper/Rider) flag it, forcing consumers to suppress CS0162 for all avrogen output. Use CodeSnippetStatement instead, which emits the block verbatim with no appended semicolon.
GBNikola
force-pushed
the
AVRO-4331-csharp-unreachable-code
branch
from
August 10, 2026 13:06
5c562c4 to
ac1fb34
Compare
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.
What is the purpose of the change
Fixes AVRO-4331. Generated
Get(int fieldPos)/Put(int fieldPos, object)build theirswitch body via
CodeSnippetExpression, whichCodeDomwraps in aCodeExpressionStatementand always suffixes with a;when emitting C#. Since everyswitch arm returns or throws, that trailing
;is an unreachable empty statement.Roslyn/
dotnet buildnever flags it, but IDE analyzers with fuller flow analysis(ReSharper/Rider) do, forcing consumers to add per-file suppressions for all avrogen
output.
This swaps
CodeSnippetExpressionforCodeSnippetStatement, which emits the switchblock verbatim with no appended semicolon. Both types live in
System.CodeDomand areavailable on every target framework the C# SDK already supports
(
netstandard2.0/netstandard2.1for the library,net6.0-net8.0for tests), sothere's no compatibility impact.
Verifying this change
This change added tests and can be verified as follows:
RecordGetAndPutSwitchesShouldNotEmitUnreachableStatementinCodeGenTest.cs,which generates a record and asserts the generated
Get/Putswitch has no stray;after its closing brace. Fails without the fix, passes with it.
Documentation