未验证 提交 45c0b662 编写于 作者: C Charles Stoner 提交者: GitHub

Analyze implicit constructors (#33093)

上级 08cf66a0
......@@ -193,7 +193,7 @@ protected override ImmutableArray<PendingBranch> Scan(ref bool badRegion)
DiagnosticBag diagnostics,
Action<BoundExpression, TypeSymbolWithAnnotations> callbackOpt = null)
{
if (method.IsImplicitlyDeclared)
if (method.IsImplicitlyDeclared && (!method.IsImplicitConstructor || method.ContainingType.IsImplicitlyDeclared))
{
return;
}
......
......@@ -8303,6 +8303,9 @@ public class Class<T> : Base<T>
// (7,25): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
// public virtual List<T?> P { get; set; } = default;
Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(7, 25),
// (7,47): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// public virtual List<T?> P { get; set; } = default;
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(7, 47),
// (12,26): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
// public override List<T?> P { get; set; } = default;
Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(12, 26),
......@@ -8331,6 +8334,9 @@ public class Class<T> : Base<T> where T : class
";
var comp = CreateCompilation(new[] { source });
comp.VerifyDiagnostics(
// (7,47): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// public virtual List<T?> P { get; set; } = default;
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(7, 47),
// (12,27): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// public override List<T?> P { get; set; } = default;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(12, 27)
......@@ -8354,7 +8360,10 @@ public class Class<T> : Base<T> where T : struct
}
";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics();
comp.VerifyDiagnostics(
// (6,47): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// public virtual List<T?> P { get; set; } = default;
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(6, 47));
}
[Fact]
......@@ -47759,6 +47768,9 @@ static void F(C<T> a, bool c)
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (4,24): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// internal T field = default;
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(4, 24),
// (8,16): warning CS8602: Possible dereference of a null reference.
// if (c) a.field.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a.field").WithLocation(8, 16),
......@@ -78377,5 +78389,39 @@ static void F()
// (object? x, object? y, object? z) = new C();
Diagnostic(ErrorCode.ERR_MissingDeconstruct, "new C()").WithArguments("C", "3").WithLocation(9, 45));
}
[Fact]
[WorkItem(26628, "https://github.com/dotnet/roslyn/issues/26628")]
public void ImplicitConstructor_01()
{
var source =
@"#pragma warning disable 414
class Program
{
object F = null; // warning
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (4,16): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// object F = null; // warning
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(4, 16));
}
[Fact]
[WorkItem(26628, "https://github.com/dotnet/roslyn/issues/26628")]
public void ImplicitConstructor_02()
{
var source =
@"#pragma warning disable 414
class Program
{
static object F = null; // warning
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (4,23): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// static object F = null; // warning
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(4, 23));
}
}
}
......@@ -34,7 +34,6 @@ public void UnconstrainedGenericType()
internal T F3 = default;
internal T F4 = default(T);
}";
// https://github.com/dotnet/roslyn/issues/29849 Missing warnings for possible null-assignment to F3 and F4
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (1,16): warning CS8618: Non-nullable field 'F1' is uninitialized.
......@@ -42,8 +41,13 @@ public void UnconstrainedGenericType()
Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "C").WithArguments("field", "F1").WithLocation(1, 16),
// (3,16): warning CS0649: Field 'C<T>.F1' is never assigned to, and will always have its default value
// internal T F1;
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "F1").WithArguments("C<T>.F1", "").WithLocation(3, 16)
);
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "F1").WithArguments("C<T>.F1", "").WithLocation(3, 16),
// (5,21): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// internal T F3 = default;
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(5, 21),
// (6,21): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// internal T F4 = default(T);
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default(T)").WithLocation(6, 21));
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册