Skip to content

Commit 6afb1ee

Browse files
authored
Enforce DoesNotReturn on return; (#46659)
1 parent ff854c6 commit 6afb1ee

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,7 @@ private static TypeWithState GetParameterState(TypeWithAnnotations parameterType
19571957
var expr = node.ExpressionOpt;
19581958
if (expr == null)
19591959
{
1960+
EnforceDoesNotReturn(node.Syntax);
19601961
PendingBranches.Add(new PendingBranch(node, this.State, label: null));
19611962
SetUnreachable();
19621963
return null;

src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30461,6 +30461,29 @@ void boom()
3046130461
comp.VerifyDiagnostics();
3046230462
}
3046330463

30464+
[Fact, WorkItem(45791, "https://github.com/dotnet/roslyn/issues/45791")]
30465+
public void DoesNotReturn_VoidReturningMethod()
30466+
{
30467+
string source = @"
30468+
using System.Diagnostics.CodeAnalysis;
30469+
30470+
public class C
30471+
{
30472+
[DoesNotReturn]
30473+
public void M()
30474+
{
30475+
return;
30476+
}
30477+
}
30478+
";
30479+
var comp = CreateNullableCompilation(new[] { source, DoesNotReturnAttributeDefinition });
30480+
comp.VerifyDiagnostics(
30481+
// (9,9): warning CS8763: A method marked [DoesNotReturn] should not return.
30482+
// return;
30483+
Diagnostic(ErrorCode.WRN_ShouldNotReturn, "return;").WithLocation(9, 9)
30484+
);
30485+
}
30486+
3046430487
[Fact]
3046530488
public void DoesNotReturn_Operator()
3046630489
{

0 commit comments

Comments
 (0)