diff --git a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs index c6b615fd7..6ecd9aa18 100644 --- a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs @@ -630,9 +630,13 @@ namespace ICSharpCode.Decompiler.CSharp AddDefinesForConditionalAttributes(function); var statementBuilder = new StatementBuilder(specializingTypeSystem, decompilationContext, method, function); - entityDecl.AddChild(statementBuilder.ConvertAsBlock(function.Body), Roles.Body); + var body = statementBuilder.ConvertAsBlock(function.Body); + entityDecl.AddChild(body, Roles.Body); if (function.IsIterator) { + if (!body.Descendants.Any(d => d is YieldReturnStatement || d is YieldBreakStatement)) { + body.Add(new YieldBreakStatement()); + } RemoveAttribute(entityDecl, new TopLevelTypeName("System.Runtime.CompilerServices", "IteratorStateMachineAttribute")); } } diff --git a/ICSharpCode.Decompiler/Tests/TestCases/Correctness/YieldReturn.cs b/ICSharpCode.Decompiler/Tests/TestCases/Correctness/YieldReturn.cs index d07c920d7..4b6bc6037 100644 --- a/ICSharpCode.Decompiler/Tests/TestCases/Correctness/YieldReturn.cs +++ b/ICSharpCode.Decompiler/Tests/TestCases/Correctness/YieldReturn.cs @@ -309,6 +309,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness } } + public static IEnumerable YieldBreakOnly() + { + yield break; + } + public static IEnumerable UnconditionalThrowInTryFinally() { // Here, MoveNext() doesn't call the finally methods at all