{"record":{"id":"62887dc851dbd2f7","repo":"dotnet/efcore","slug":"empty-switch-statement","errorCode":null,"errorMessage":"Empty switch statement","messagePattern":"Empty switch statement","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2385,"sourceCode":"            }\n\n            default:\n                throw new ArgumentOutOfRangeException();\n        }\n\n        static ConditionalExpression RewriteSwitchToConditionals(SwitchExpression node)\n        {\n            if (node.Type == typeof(void))\n            {\n                return (ConditionalExpression)(node.Cases\n                        .SelectMany(c => c.TestValues, (c, tv) => new { c.Body, Label = tv })\n                        .Reverse()\n                        .Aggregate(\n                            node.DefaultBody,\n                            (expression, arm) => expression is null\n                                ? Expression.IfThen(Expression.Equal(node.SwitchValue, arm.Label), arm.Body)\n                                : Expression.IfThenElse(Expression.Equal(node.SwitchValue, arm.Label), arm.Body, expression))\n                    ?? throw new NotImplementedException(\"Empty switch statement\"));\n            }\n\n            Check.DebugAssert(node.DefaultBody is not null, \"Switch expression with non-void return type but no default body\");\n\n            return (ConditionalExpression)node.Cases\n                .SelectMany(c => c.TestValues, (c, tv) => new { c.Body, Label = tv })\n                .Reverse()\n                .Aggregate(\n                    node.DefaultBody,\n                    (expression, arm) => Expression.Condition(\n                        Expression.Equal(node.SwitchValue, arm.Label),\n                        arm.Body,\n                        expression));\n        }\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitTry(TryExpression tryNode)","sourceCodeStart":2367,"sourceCodeEnd":2403,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2367-L2403","documentation":"When a void switch is rewritten to conditionals (RewriteSwitchToConditionals), the aggregate over an empty case list with a null default body yields null, and the translator throws NotImplementedException(\"Empty switch statement\"). A switch with zero arms and no default is degenerate.","triggerScenarios":"A SwitchExpression of void type constructed with no cases and a null default body, e.g. Expression.Switch(value).","commonSituations":"Programmatically constructed degenerate switches; expression rewriting that removes all arms leaving an empty switch.","solutions":["Add at least one case or a default body before the switch is translated.","Remove the empty switch entirely — it has no effect.","Validate that a switch has arms before constructing it."],"exampleFix":"// before\nvar sw = Expression.Switch(value); // no cases, no default -> throws\n// after\nvar sw = Expression.Switch(value, Expression.Empty(), new[] { case1 });","handlingStrategy":"validation","validationCode":"protected override Expression VisitSwitch(SwitchExpression s) {\n    if (s.Cases.Count == 0 && s.DefaultBody is null) Found = true;\n    return s;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never construct a switch with zero arms and no default.","Drop degenerate switches entirely — they are no-ops.","Validate arm count before building a switch."],"tags":["ef-core","linq","expression-tree","switch","control-flow"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}