Skip to content

AVRO-4331: [csharp] Fix unreachable code in generated Get()/Put() - #3937

Open
GBNikola wants to merge 1 commit into
apache:mainfrom
GBNikola:AVRO-4331-csharp-unreachable-code
Open

AVRO-4331: [csharp] Fix unreachable code in generated Get()/Put()#3937
GBNikola wants to merge 1 commit into
apache:mainfrom
GBNikola:AVRO-4331-csharp-unreachable-code

Conversation

@GBNikola

Copy link
Copy Markdown

What is the purpose of the change

Fixes AVRO-4331. Generated Get(int fieldPos) / Put(int fieldPos, object) build their
switch body via CodeSnippetExpression, which CodeDom wraps in a
CodeExpressionStatement and always suffixes with a ; when emitting C#. Since every
switch arm returns or throws, that trailing ; is an unreachable empty statement.
Roslyn/dotnet build never 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 CodeSnippetExpression for CodeSnippetStatement, which emits the switch
block verbatim with no appended semicolon. Both types live in System.CodeDom and are
available on every target framework the C# SDK already supports
(netstandard2.0/netstandard2.1 for the library, net6.0-net8.0 for tests), so
there's no compatibility impact.

Verifying this change

This change added tests and can be verified as follows:

  • Added RecordGetAndPutSwitchesShouldNotEmitUnreachableStatement in CodeGenTest.cs,
    which generates a record and asserts the generated Get/Put switch has no stray ;
    after its closing brace. Fails without the fix, passes with it.

Documentation

  • Does this pull request introduce a new feature? no

@github-actions github-actions Bot added the C# label Aug 10, 2026
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
GBNikola force-pushed the AVRO-4331-csharp-unreachable-code branch from 5c562c4 to ac1fb34 Compare August 10, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant