{"record":{"id":"739acd5404b0b8b2","repo":"dotnet/BenchmarkDotNet","slug":"extend-a-an-expression-with-a-valid-body","errorCode":null,"errorMessage":"Extend a an Expression with a valid Body","messagePattern":"Extend a an Expression with a valid Body","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Extensions/ReportExtensions.cs","lineNumber":12,"sourceCode":"using BenchmarkDotNet.Mathematics;\nusing BenchmarkDotNet.Reports;\nusing System.Linq.Expressions;\n\nnamespace BenchmarkDotNet.Extensions\n{\n    public static class ReportExtensions\n    {\n        public static BenchmarkReport GetReportFor<T>(this Summary summary, Expression<Action<T>> actionExp)\n        {\n            if (actionExp.Body == null)\n                throw new ArgumentException(\"Extend a an Expression with a valid Body\", nameof(actionExp));\n\n            if (!(actionExp.Body is MethodCallExpression methodExp))\n                throw new ArgumentException(\"Extend a MethodCallExpression, but got a \" + actionExp.Body.GetType().Name, nameof(actionExp));\n\n            return summary.Reports.First(r => r.BenchmarkCase.Descriptor.WorkloadMethod == methodExp.Method);\n        }\n\n        public static IList<Measurement> GetRunsFor<T>(this Summary summary, Expression<Action<T>> actionExp)\n        {\n            return summary.GetReportFor(actionExp).GetResultRuns().ToList();\n        }\n\n        public static Statistics GetStatistics(this IReadOnlyCollection<Measurement> runs)\n        {\n            if (runs.IsEmpty())\n                throw new InvalidOperationException(\"List of measurements contains no elements\");\n            return new Statistics(runs.Select(r => r.GetAverageTime().Nanoseconds));\n        }","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Extensions/ReportExtensions.cs#L1-L30","documentation":"ReportExtensions.GetReportFor<T>(Summary, Expression<Action<T>>) requires a lambda expression tree whose Body is non-null. The exception fires when actionExp.Body == null, which is abnormal for a compiler-generated expression and usually indicates a malformed or default-valued expression was passed.","triggerScenarios":"Calling summary.GetReportFor(x => ...) with a manually constructed or default(Expression<Action<T>>) whose Body is null, or an expression tree built via the Expression API that was never assigned a Body.","commonSituations":"Passing default(Expression<Action<T>>) by mistake, building expression trees by hand incorrectly, or a deserialization/reflection scenario producing a degenerate Expression.","solutions":["Pass a normal lambda that calls a benchmark method, e.g. summary.GetReportFor(b => b.MyBenchmark()).","If constructing the expression programmatically, ensure the lambda's Body is a real Expression node before passing it in.","Guard with a null check on actionExp.Body before calling GetReportFor."],"exampleFix":"// before\nvar report = summary.GetReportFor(default(Expression<Action<MyBench>>));\n\n// after\nvar report = summary.GetReportFor(b => b.Run());","handlingStrategy":"validation","validationCode":"Expression<Action<T>> expr = b => b.Run();\nif (expr?.Body == null) throw new InvalidOperationException(\"expression must have a body\");\nvar report = summary.GetReportFor(expr);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a compiler-generated lambda, never default(Expression<...>) or hand-built trees.","Treat GetReportFor as taking a method-call lambda only.","Verify expr.Body != null before calling when expressions come from external sources."],"tags":["expression-tree","validation","reports"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}