Skip to content

Commit

Permalink
fixes NullReferenceException on void returning methods with explicit …
Browse files Browse the repository at this point in the history
…return statements (#264)
  • Loading branch information
adrianoc committed Apr 1, 2024
1 parent 0d511d4 commit f2d5888
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cecilifier.Core.Tests/Tests/Unit/MethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ class Derived : Base
\s+\2Ret\);
"""));
}

[TestCase("void M() { return; }")]
[TestCase("void M(int i) { if(i > 10) return; }")]
[TestCase("class Foo { void M() { return; } }")]
public void VoidReturningMethod_WithExplicitReturn_DoesNotCrash(string code)
{
var result = RunCecilifier(code);

Assert.Pass("Works");
}
}
3 changes: 2 additions & 1 deletion Cecilifier.Core/AST/ExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ internal static bool VisitAndPopIfNotConsumed(IVisitorContext ctx, string ilVar,
public override void VisitReturnStatement(ReturnStatementSyntax node)
{
using var _ = LineInformationTracker.Track(Context, node);
Utils.EnsureNotNull(node.Expression);
if (node.Expression == null)
return;
node.Expression.Accept(this);
InjectRequiredConversions(node.Expression);
}
Expand Down

0 comments on commit f2d5888

Please sign in to comment.