Skip to content

Commit

Permalink
Check reachability when looking at uninitialized non-null fields (#34316
Browse files Browse the repository at this point in the history
)

If the end of a constructor is unreachable we shouldn't warn about
fields which are uninitialized at the end.

Fixes #25529
  • Loading branch information
agocke authored Apr 2, 2019
1 parent 0354852 commit ebe36fa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void ReportUninitializedNonNullableReferenceTypeFields()
{
var thisParameter = MethodThisParameter;
int thisSlot = VariableSlot(thisParameter);
if (thisSlot == -1)
if (thisSlot == -1 || !State.Reachable)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,5 +789,60 @@ void L(object o)
// C()
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "C").WithArguments("field", "G").WithLocation(6, 5));
}

[Fact]
[WorkItem(25529, "https://github.com/dotnet/roslyn/issues/25529")]
public void UnassignedNonNullFieldsUnreachable()
{
var comp = CreateCompilation(@"
using System;
class C
{
private object _f;
internal C()
{
throw new NotImplementedException();
}
internal C(object o) { }
internal C(string s)
{
return;
throw new NotImplementedException();
}
internal C(int x)
{
if (x == 0)
{
return;
}
throw new NotImplementedException();
}
internal C(char c)
{
return;
}
}
", options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (5,20): warning CS0169: The field 'C._f' is never used
// private object _f;
Diagnostic(ErrorCode.WRN_UnreferencedField, "_f").WithArguments("C._f").WithLocation(5, 20),
// (11,14): warning CS8618: Non-nullable field '_f' is uninitialized.
// internal C(object o) { }
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "C").WithArguments("field", "_f").WithLocation(11, 14),
// (13,14): warning CS8618: Non-nullable field '_f' is uninitialized.
// internal C(string s)
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "C").WithArguments("field", "_f").WithLocation(13, 14),
// (19,14): warning CS8618: Non-nullable field '_f' is uninitialized.
// internal C(int x)
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "C").WithArguments("field", "_f").WithLocation(19, 14),
// (28,14): warning CS8618: Non-nullable field '_f' is uninitialized.
// internal C(char c)
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "C").WithArguments("field", "_f").WithLocation(28, 14));
}
}
}

0 comments on commit ebe36fa

Please sign in to comment.