Nikola G. created AVRO-4331:
-------------------------------
Summary: [C#] Generated Get()/Put() switch emits unreachable
statement after exhaustive switc
Key: AVRO-4331
URL: https://issues.apache.org/jira/browse/AVRO-4331
Project: Apache Avro
Issue Type: Improvement
Components: csharp
Affects Versions: 1.12.1
Reporter: Nikola G.
Avrogen generates Get(int fieldPos) / Put(int fieldPos, object fieldValue) on
every SpecificRecordBase-derived record. The switch body is built as a raw
string in CodeGen.cs and added to the method via CodeSnippetExpression:
var cseGet = new CodeSnippetExpression(getFieldStmt.ToString());
cmmGet.Statements.Add(cseGet);
CodeStatementCollection.Add(CodeExpression) wraps the expression in a
CodeExpressionStatement, and the C# CodeDom provider always appends a trailing
; after an expression statement even though the snippet text is already a
complete switch (fieldPos) \{ ... } block. The generated code looks like
this:
public virtual object Get(int fieldPos)
{
switch (fieldPos)
{
case 0: return this.Value;
case 1: return this.Quality;
default: throw new global::Avro.AvroRuntimeException("Bad index " +
fieldPos + " in Get()");
}; // <- stray empty statement, unreachable
}
Since every branch of the switch returns or throws, that trailing ; is
unreachable. Roslyn's own compiler analysis does not flag it (dotnet build
stays clean), but IDE analyzers with more thorough flow analysis notably
JetBrains
ReSharper/Rider — do flag it as unreachable code, forcing consumers to add
per-file .editorconfig suppressions for every directory of avrogen output just
to keep their IDE quiet.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)
