-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reporting "field is uninitialized" for constructor that throws an exception #25529
Comments
In unreachable code every variable is definitely not null. |
Yes, but in this case, the call to this default constructor will not create a class with an uninitialized field either, since the constructor call will fail! The use case would be a class with a second constructor, e.g. |
@AlexanderTaeschner Agreed. That is why the rule I stated works for this case (the end of the constructor is not reachable). |
This issue still exists in recent 16.1 branch: [Fact]
public void Test()
{
var comp = CreateCompilation(@"
using System;
class C
{
private object _f;
internal C()
{
throw new NotImplementedException();
}
}", 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),
// (6,14): warning CS8618: Non-nullable field '_f' is uninitialized.
// internal C()
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "C").WithArguments("field", "_f").WithLocation(6, 14)
);
} |
) If the end of a constructor is unreachable we shouldn't warn about fields which are uninitialized at the end. Fixes #25529
Reports:
(5,14): warning CS8618: Non-nullable field '_f' is uninitialized.
The text was updated successfully, but these errors were encountered: