未验证 提交 bbe84f5b 编写于 作者: J Julien Couvreur 提交者: GitHub

Nullable warning for iteration variables is W-warning (#40207)

上级 8c0b494d
......@@ -15763,24 +15763,6 @@ internal class CSharpResources {
}
}
/// <summary>
/// Looks up a localized string similar to Possible null reference assignment to iteration variable.
/// </summary>
internal static string WRN_NullReferenceIterationVariable {
get {
return ResourceManager.GetString("WRN_NullReferenceIterationVariable", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Possible null reference assignment to iteration variable.
/// </summary>
internal static string WRN_NullReferenceIterationVariable_Title {
get {
return ResourceManager.GetString("WRN_NullReferenceIterationVariable_Title", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Dereference of a possibly null reference..
/// </summary>
......
......@@ -5861,12 +5861,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="IDS_FeatureObsoleteOnPropertyAccessor" xml:space="preserve">
<value>obsolete on property accessor</value>
</data>
<data name="WRN_NullReferenceIterationVariable" xml:space="preserve">
<value>Possible null reference assignment to iteration variable</value>
</data>
<data name="WRN_NullReferenceIterationVariable_Title" xml:space="preserve">
<value>Possible null reference assignment to iteration variable</value>
</data>
<data name="WRN_UnconsumedEnumeratorCancellationAttributeUsage" xml:space="preserve">
<value>The EnumeratorCancellationAttribute applied to parameter '{0}' will have no effect. The attribute is only effective on a parameter of type CancellationToken in an async-iterator method returning IAsyncEnumerable</value>
</data>
......
......@@ -1641,7 +1641,7 @@ internal enum ErrorCode
WRN_NullReferenceReturn = 8603,
WRN_NullReferenceArgument = 8604,
WRN_UnboxPossibleNull = 8605,
WRN_NullReferenceIterationVariable = 8606,
// WRN_NullReferenceIterationVariable = 8606 (unavailable, may be used in warning suppressions in early C# 8.0 code)
WRN_DisallowNullAttributeForbidsMaybeNullAssignment = 8607,
WRN_NullabilityMismatchInTypeOnOverride = 8608,
WRN_NullabilityMismatchInReturnTypeOnOverride = 8609,
......
......@@ -25,7 +25,6 @@ static ErrorFacts()
builder.Add(getId(ErrorCode.WRN_NullReferenceReceiver));
builder.Add(getId(ErrorCode.WRN_NullReferenceReturn));
builder.Add(getId(ErrorCode.WRN_NullReferenceArgument));
builder.Add(getId(ErrorCode.WRN_NullReferenceIterationVariable));
builder.Add(getId(ErrorCode.WRN_UninitializedNonNullableField));
builder.Add(getId(ErrorCode.WRN_NullabilityMismatchInAssignment));
builder.Add(getId(ErrorCode.WRN_NullabilityMismatchInArgument));
......@@ -382,7 +381,6 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_NullReferenceReceiver:
case ErrorCode.WRN_NullReferenceReturn:
case ErrorCode.WRN_NullReferenceArgument:
case ErrorCode.WRN_NullReferenceIterationVariable:
case ErrorCode.WRN_NullabilityMismatchInTypeOnOverride:
case ErrorCode.WRN_NullabilityMismatchInReturnTypeOnOverride:
case ErrorCode.WRN_NullabilityMismatchInParameterTypeOnOverride:
......
......@@ -1083,25 +1083,11 @@ private enum AssignmentKind
return;
}
if (useLegacyWarnings && isMaybeDefaultValue(valueType))
{
// No W warning reported assigning or casting [MaybeNull]T value to T
// because there is no syntax for declaring the target type as [MaybeNull]T.
return;
}
if (value.ConstantValue?.IsNull == true)
if (value.ConstantValue?.IsNull == true && !useLegacyWarnings)
{
// Report warning converting null literal to non-nullable reference type.
// target (e.g.: `object x = null;` or calling `void F(object y)` with `F(null)`).
if (useLegacyWarnings)
{
ReportNonSafetyDiagnostic(location);
}
else
{
ReportDiagnostic(assignmentKind == AssignmentKind.Return ? ErrorCode.WRN_NullReferenceReturn : ErrorCode.WRN_NullAsNonNullable, location);
}
ReportDiagnostic(assignmentKind == AssignmentKind.Return ? ErrorCode.WRN_NullReferenceReturn : ErrorCode.WRN_NullAsNonNullable, location);
}
else if (assignmentKind == AssignmentKind.Argument)
{
......@@ -1111,17 +1097,17 @@ private enum AssignmentKind
}
else if (useLegacyWarnings)
{
if (isMaybeDefaultValue(valueType))
{
// No W warning reported assigning or casting [MaybeNull]T value to T
// because there is no syntax for declaring the target type as [MaybeNull]T.
return;
}
ReportNonSafetyDiagnostic(location);
}
else if (assignmentKind == AssignmentKind.ForEachIterationVariable && isMaybeDefaultValue(valueType))
{
// No warning reported assigning [MaybeNull]T value to foreach iteration variable
// because there is no syntax for declaring the variable as [MaybeNull]T.
return;
}
else
{
ReportDiagnostic(assignmentKind switch { AssignmentKind.Return => ErrorCode.WRN_NullReferenceReturn, AssignmentKind.ForEachIterationVariable => ErrorCode.WRN_NullReferenceIterationVariable, _ => ErrorCode.WRN_NullReferenceAssignment }, location);
ReportDiagnostic(assignmentKind == AssignmentKind.Return ? ErrorCode.WRN_NullReferenceReturn : ErrorCode.WRN_NullReferenceAssignment, location);
}
static bool isMaybeDefaultValue(TypeWithState valueType)
......@@ -6997,7 +6983,7 @@ public override void VisitForEachIterationVariables(BoundForEachStatement node)
sourceState,
checkConversion: true,
fromExplicitCast: !conversion.IsImplicit,
useLegacyWarnings: false,
useLegacyWarnings: true,
AssignmentKind.ForEachIterationVariable,
reportTopLevelWarnings: true,
reportRemainingWarnings: true,
......
......@@ -194,7 +194,6 @@ public static bool IsWarning(ErrorCode code)
case ErrorCode.WRN_NullReferenceReturn:
case ErrorCode.WRN_NullReferenceArgument:
case ErrorCode.WRN_UnboxPossibleNull:
case ErrorCode.WRN_NullReferenceIterationVariable:
case ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment:
case ErrorCode.WRN_NullabilityMismatchInTypeOnOverride:
case ErrorCode.WRN_NullabilityMismatchInReturnTypeOnOverride:
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Možné přiřazení odkazu s hodnotou null k proměnné iterace</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Možné přiřazení odkazu s hodnotou null k proměnné iterace</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Přístup přes ukazatel k možnému odkazu s hodnotou null</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Mögliche Zuweisung eines Nullverweises zu Iterationsvariable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Mögliche Zuweisung eines Nullverweises zu Iterationsvariable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Dereferenzierung eines möglichen Nullverweises.</target>
......
......@@ -1480,16 +1480,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Posible asignación de referencia de tipo NULL a variable de iteración</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Posible asignación de referencia de tipo NULL a variable de iteración</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Desreferencia de una referencia posiblemente NULL.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Affectation d'une éventuelle référence null à la variable d'itération</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Affectation d'une éventuelle référence null à la variable d'itération</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Déréférencement d'une éventuelle référence null.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Possibile assegnazione di riferimento Null alla variabile di iterazione</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Possibile assegnazione di riferimento Null alla variabile di iterazione</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Dereferenziamento di un possibile riferimento Null.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">繰り返し変数に対する null 参照代入の可能性があります</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">繰り返し変数に対する null 参照代入の可能性があります</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">null 参照の可能性があるものの逆参照です。</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">반복 변수에 대한 가능한 null 참조 할당</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">반복 변수에 대한 가능한 null 참조 할당</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">가능한 null 참조의 역참조입니다.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Możliwe przypisanie odwołania o wartości null do zmiennej iteracji</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Możliwe przypisanie odwołania o wartości null do zmiennej iteracji</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Wyłuskanie odwołania, które może mieć wartość null.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Possível atribuição de referência nula à variável de iteração</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Possível atribuição de referência nula à variável de iteração</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Desreferência de uma referência possivelmente nula.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Назначение вероятной пустой ссылки переменной итерации</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Назначение вероятной пустой ссылки переменной итерации</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Разыменование вероятной пустой ссылки.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Yineleme değişkenine olası null başvuru ataması</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">Yineleme değişkenine olası null başvuru ataması</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">Olası bir null başvurunun başvurma işlemi.</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">迭代变量的可能的空引用赋值</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">迭代变量的可能的空引用赋值</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">取消引用可能出现的空引用。</target>
......
......@@ -1479,16 +1479,6 @@
<target state="new">Object or collection initializer implicitly dereferences possibly null member.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">反覆運算變數的可能 null 參考指派</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="translated">反覆運算變數的可能 null 參考指派</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="translated">可能 null 參考的取值 (dereference)。</target>
......
......@@ -64048,7 +64048,7 @@ static void F4(A4? x4, A4 y4)
// b = ((B)x4)/*T:B!*/;
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x4").WithArguments("a", "A4.implicit operator B(A4 a)").WithLocation(47, 17),
// (48,18): warning CS8604: Possible null reference argument for parameter 'a' in 'A4.implicit operator B(A4 a)'.
// b = ((B?)x4)/*T:B!*/;
// b = ((B?)x4)/*T:B?*/;
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x4").WithArguments("a", "A4.implicit operator B(A4 a)").WithLocation(48, 18));
comp.VerifyTypes();
}
......@@ -64200,7 +64200,7 @@ static void F(Enumerable e)
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(19, 13));
}
[Fact]
[Fact, WorkItem(40164, "https://github.com/dotnet/roslyn/issues/40164")]
public void ForEach_02()
{
var source =
......@@ -64230,9 +64230,9 @@ static void F(Enumerable e)
// (15,13): warning CS8602: Dereference of a possibly null reference.
// x.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(15, 13),
// (16,25): warning CS8605: Possible null reference assignment to iteration variable
// (16,25): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (object y in e)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "y").WithLocation(16, 25),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "y").WithLocation(16, 25),
// (17,13): warning CS8602: Dereference of a possibly null reference.
// y.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(17, 13),
......@@ -64241,7 +64241,7 @@ static void F(Enumerable e)
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(19, 13));
}
[Fact]
[Fact, WorkItem(40164, "https://github.com/dotnet/roslyn/issues/40164")]
public void ForEach_03()
{
var source =
......@@ -64311,18 +64311,18 @@ static void G(IEnumerable e)
// (50,13): warning CS8602: Dereference of a possibly null reference.
// x.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(50, 13),
// (51,25): warning CS8605: Possible null reference assignment to iteration variable
// (51,25): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (object y in e)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "y").WithLocation(51, 25),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "y").WithLocation(51, 25),
// (52,13): warning CS8602: Dereference of a possibly null reference.
// y.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(52, 13),
// (57,13): warning CS8602: Dereference of a possibly null reference.
// z.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(57, 13),
// (58,25): warning CS8605: Possible null reference assignment to iteration variable
// (58,25): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (object w in e)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "w").WithLocation(58, 25),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "w").WithLocation(58, 25),
// (59,13): warning CS8602: Dereference of a possibly null reference.
// w.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "w").WithLocation(59, 13));
......@@ -64411,15 +64411,15 @@ class P
// (15,13): warning CS8602: Dereference of a possibly null reference.
// y.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(15, 13),
// (16,20): warning CS8605: Possible null reference assignment to iteration variable
// (16,20): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (T z in c)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "z").WithLocation(16, 20),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "z").WithLocation(16, 20),
// (17,13): warning CS8602: Dereference of a possibly null reference.
// z.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(17, 13),
// (18,25): warning CS8605: Possible null reference assignment to iteration variable
// (18,25): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (object w in c)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "w").WithLocation(18, 25),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "w").WithLocation(18, 25),
// (19,13): warning CS8602: Dereference of a possibly null reference.
// w.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "w").WithLocation(19, 13));
......@@ -64471,9 +64471,9 @@ class P
// (26,13): warning CS8602: Dereference of a possibly null reference.
// x2.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x2").WithLocation(26, 13),
// (27,20): warning CS8605: Possible null reference assignment to iteration variable
// (27,20): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (T y2 in new S<T?>())
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "y2").WithLocation(27, 20),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "y2").WithLocation(27, 20),
// (27,32): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'S<T>'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// foreach (T y2 in new S<T?>())
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "T?").WithArguments("S<T>", "T", "T?").WithLocation(27, 32),
......@@ -64578,18 +64578,18 @@ static void F(A?[] c)
// (10,13): warning CS8602: Dereference of a possibly null reference.
// a2.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a2").WithLocation(10, 13),
// (11,20): warning CS8605: Possible null reference assignment to iteration variable
// (11,20): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (A a3 in c)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "a3").WithLocation(11, 20),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "a3").WithLocation(11, 20),
// (12,13): warning CS8602: Dereference of a possibly null reference.
// a3.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a3").WithLocation(12, 13),
// (14,13): warning CS8602: Dereference of a possibly null reference.
// b1.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "b1").WithLocation(14, 13),
// (15,20): warning CS8605: Possible null reference assignment to iteration variable
// (15,20): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (B b2 in c)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "b2").WithLocation(15, 20),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "b2").WithLocation(15, 20),
// (16,13): warning CS8602: Dereference of a possibly null reference.
// b2.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "b2").WithLocation(16, 13));
......@@ -64667,9 +64667,9 @@ static void F(IEnumerable<A> e)
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (15,20): warning CS8605: Possible null reference assignment to iteration variable
// (15,20): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (B y in e)
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "y").WithLocation(15, 20),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "y").WithLocation(15, 20),
// (16,13): warning CS8602: Dereference of a possibly null reference.
// y.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(16, 13),
......@@ -65400,9 +65400,9 @@ static object F2<T>()
// (19,20): warning CS8603: Possible null reference return.
// return t2; // 2
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t2").WithLocation(19, 20),
// (24,25): warning CS8606: Possible null reference assignment to iteration variable
// (24,25): warning CS8600: Converting null literal or possible null value to non-nullable type.
// foreach (object o1 in new S<T>()) // 3
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "o1").WithLocation(24, 25),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "o1").WithLocation(24, 25),
// (25,20): warning CS8603: Possible null reference return.
// return o1; // 4
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "o1").WithLocation(25, 20),
......@@ -65456,9 +65456,9 @@ await foreach (object? o2 in new S<T>())
// (21,20): warning CS8603: Possible null reference return.
// return t2; // 2
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t2").WithLocation(21, 20),
// (26,31): warning CS8606: Possible null reference assignment to iteration variable
// (26,31): warning CS8600: Converting null literal or possible null value to non-nullable type.
// await foreach (object o1 in new S<T>()) // 3
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "o1").WithLocation(26, 31),
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "o1").WithLocation(26, 31),
// (27,20): warning CS8603: Possible null reference return.
// return o1; // 4
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "o1").WithLocation(27, 20),
......@@ -2208,13 +2208,7 @@ void M(object o1, object? o2)
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (10,25): warning CS8606: Possible null reference assignment to iteration variable
// foreach (object o in GetList(o2)) {}
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "o").WithLocation(10, 25),
// (12,25): warning CS8606: Possible null reference assignment to iteration variable
// foreach (object o in GetList(o1)) {}
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "o").WithLocation(12, 25));
comp.VerifyDiagnostics();
var syntaxTree = comp.SyntaxTrees[0];
var root = syntaxTree.GetRoot();
......
......@@ -270,7 +270,6 @@ public void WarningLevel_2()
case ErrorCode.WRN_NullReferenceReturn:
case ErrorCode.WRN_NullReferenceArgument:
case ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment:
case ErrorCode.WRN_NullReferenceIterationVariable:
case ErrorCode.WRN_NullabilityMismatchInTypeOnOverride:
case ErrorCode.WRN_NullabilityMismatchInReturnTypeOnOverride:
case ErrorCode.WRN_NullabilityMismatchInParameterTypeOnOverride:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册