Skip to content

Commit 2d39e58

Browse files
committed
remove unnecessary Unsafe enum casting optimization that causes compiler warning
a simple cast produces the same JIT optimized assembly
1 parent e05a359 commit 2d39e58

2 files changed

Lines changed: 2 additions & 8 deletions

File tree

Core/Generators/CSharp/CSharpGenerator.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ private string CompileEncodeField(TypeBase type, string target, int depth = 0, i
10741074
_ => throw new ArgumentOutOfRangeException()
10751075
},
10761076
DefinedType dt when Schema.Definitions[dt.Name] is EnumDefinition ed =>
1077-
CompileEncodeField(ed.ScalarType, $"{As(PrefixNamespace(dt.Name.ToPascalCase()), TypeName(ed.ScalarType), target)}", depth, indentDepth),
1077+
CompileEncodeField(ed.ScalarType, $"({TypeName(ed.ScalarType)}){target}", depth, indentDepth),
10781078
DefinedType dt =>
10791079
$"{PrefixNamespace(dt.Name.ToPascalCase())}.__EncodeInto({target}, ref writer);",
10801080
_ => throw new InvalidOperationException($"CompileEncodeField: {type}")
@@ -1134,7 +1134,7 @@ private string CompileDecodeField(TypeBase type, string target, int depth = 0)
11341134
"}",
11351135
ScalarType st => $"{target} = {ReadBaseType(st.BaseType)};",
11361136
DefinedType dt when Schema.Definitions[dt.Name] is EnumDefinition ed =>
1137-
$"{target} = {As(TypeName(ed.ScalarType), PrefixNamespace(dt.Name.ToPascalCase()), ReadBaseType(ed.BaseType))};",
1137+
$"{target} = ({PrefixNamespace(dt.Name.ToPascalCase())}){ReadBaseType(ed.BaseType)};",
11381138
DefinedType dt =>
11391139
$"{target} = {PrefixNamespace(dt.Name.ToPascalCase())}.__DecodeFrom(ref reader);",
11401140
_ => throw new InvalidOperationException($"CompileDecodeField: {type}")
@@ -1352,11 +1352,6 @@ private string TypeName(in TypeBase type, string arraySizeVar = "")
13521352
};
13531353
}
13541354

1355-
private static string As(string from, string to, string value)
1356-
{
1357-
return $"System.Runtime.CompilerServices.Unsafe.As<{from}, {to}>(ref System.Runtime.CompilerServices.Unsafe.AsRef({value}))";
1358-
}
1359-
13601355
public override string Alias { get => "cs"; set => throw new NotImplementedException(); }
13611356
public override string Name { get => "C#"; set => throw new NotImplementedException(); }
13621357

Laboratory/Integration/IntegrationTesting.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<LangVersion>preview</LangVersion>
77
<TargetFramework>net8.0</TargetFramework>
88
<CheckEolTargetFramework>false</CheckEolTargetFramework>
9-
<NoWarn>$(NoWarn);CS9193</NoWarn>
109
</PropertyGroup>
1110

1211
<ItemGroup>

0 commit comments

Comments
 (0)