未验证 提交 b3049770 编写于 作者: F Fred Silberberg 提交者: GitHub

Merge pull request #34926 from 333fred/foreach-loop-variable-mismatch

Correctly report mismatches in foreach types
......@@ -5877,4 +5877,10 @@ 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>
</root>
\ No newline at end of file
......@@ -1634,7 +1634,8 @@ internal enum ErrorCode
WRN_NullReferenceReturn = 8603,
WRN_NullReferenceArgument = 8604,
WRN_UnboxPossibleNull = 8605,
// Unused 8606-8607
WRN_NullReferenceIterationVariable = 8606,
// Unused 8607
WRN_NullabilityMismatchInTypeOnOverride = 8608,
WRN_NullabilityMismatchInReturnTypeOnOverride = 8609,
WRN_NullabilityMismatchInParameterTypeOnOverride = 8610,
......
......@@ -25,6 +25,7 @@ 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));
......@@ -370,6 +371,7 @@ 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:
......
......@@ -194,6 +194,7 @@ 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_NullabilityMismatchInTypeOnOverride:
case ErrorCode.WRN_NullabilityMismatchInReturnTypeOnOverride:
case ErrorCode.WRN_NullabilityMismatchInParameterTypeOnOverride:
......
......@@ -1422,6 +1422,16 @@
<target state="translated">Může jít o přiřazení s odkazem null.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Může jít o vyhodnocení odkazu null.</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">Mögliche Nullverweiszuweisung.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Mögliche Dereferenzierung eines Nullverweises.</target>
......
......@@ -1424,6 +1424,16 @@
<target state="translated">Posible asignación de referencia nula</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Posible desreferencia de una referencia nula.</target>
......
......@@ -1423,6 +1423,16 @@
<target state="translated">Existence possible d'une assignation de référence null.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Déréférencement possible d'une référence null.</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">Possibile assegnazione di riferimento Null.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Possibile dereferenziamento di un riferimento Null.</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">Null 参照割り当ての可能性があります。</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Null 参照の逆参照である可能性があります。</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">가능한 null 참조 할당입니다.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">가능한 null 참조의 역참조입니다.</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">Możliwe przypisanie odwołania o wartości null.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Możliwe wyłuskanie odwołania o wartości null.</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">Possível atribuição de referência nula.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Possível desreferência de uma referência nula.</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">Возможно, присваивание-ссылка, допускающее значение NULL.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Возможно, разыменование ссылки, допускающей значение NULL.</target>
......
......@@ -1423,6 +1423,16 @@
<target state="translated">Olası null başvuru ataması.</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">Null başvuruya yönelik olası başvuru.</target>
......
......@@ -1463,6 +1463,16 @@
<target state="translated">可能的 null 引用赋值。</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">null 引用可能的取消引用。</target>
......
......@@ -1422,6 +1422,16 @@
<target state="translated">可能有 Null 參考指派。</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceIterationVariable_Title">
<source>Possible null reference assignment to iteration variable</source>
<target state="new">Possible null reference assignment to iteration variable</target>
<note />
</trans-unit>
<trans-unit id="WRN_NullReferenceReceiver">
<source>Dereference of a possibly null reference.</source>
<target state="needs-review-translation">可能有 Null 參考的取值 (Dereference)。</target>
......
......@@ -33753,8 +33753,15 @@ static void G()
}
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
// https://github.com/dotnet/roslyn/issues/33011: Deconstruction should infer `string?` for `var y`.
comp.VerifyDiagnostics();
// https://github.com/dotnet/roslyn/issues/33011: Deconstruction should infer `string?` for `var y`. Shouldn't report nullability
// mismatches
comp.VerifyDiagnostics(
// (8,18): warning CS8619: Nullability of reference types in value of type '(string, string?)' doesn't match target type 'string'.
// foreach ((var x, var y) in F())
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "(var x, var y)").WithArguments("(string, string?)", "string").WithLocation(8, 18),
// (8,18): warning CS8619: Nullability of reference types in value of type '(string, string?)' doesn't match target type 'string'.
// foreach ((var x, var y) in F())
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "(var x, var y)").WithArguments("(string, string?)", "string").WithLocation(8, 18));
//// (11,13): warning CS8602: Dereference of a possibly null reference.
//// y.ToString();
//Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(11, 13));
......@@ -50456,11 +50463,14 @@ static void F(Enumerable e)
foreach (string y in e)
y.ToString();
foreach (string? z in e)
z.ToString();
z.ToString(); // 1
}
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics();
comp.VerifyDiagnostics(
// (19,13): warning CS8602: Dereference of a possibly null reference.
// z.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(19, 13));
}
[Fact]
......@@ -50493,9 +50503,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,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (16,25): warning CS8605: Possible null reference assignment to iteration variable
// foreach (object y in e)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "object").WithLocation(16, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "y").WithLocation(16, 25),
// (17,13): warning CS8602: Dereference of a possibly null reference.
// y.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(17, 13),
......@@ -50574,18 +50584,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,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (51,25): warning CS8605: Possible null reference assignment to iteration variable
// foreach (object y in e)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "object").WithLocation(51, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "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,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (58,25): warning CS8605: Possible null reference assignment to iteration variable
// foreach (object w in e)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "object").WithLocation(58, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "w").WithLocation(58, 25),
// (59,13): warning CS8602: Dereference of a possibly null reference.
// w.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "w").WithLocation(59, 13));
......@@ -50674,15 +50684,15 @@ class P
// (15,13): warning CS8602: Dereference of a possibly null reference.
// y.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(15, 13),
// (16,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (16,20): warning CS8605: Possible null reference assignment to iteration variable
// foreach (T z in c)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "T").WithLocation(16, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "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,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (18,25): warning CS8605: Possible null reference assignment to iteration variable
// foreach (object w in c)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "object").WithLocation(18, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "w").WithLocation(18, 25),
// (19,13): warning CS8602: Dereference of a possibly null reference.
// w.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "w").WithLocation(19, 13));
......@@ -50734,9 +50744,9 @@ class P
// (26,13): warning CS8602: Dereference of a possibly null reference.
// x2.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x2").WithLocation(26, 13),
// (27,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (27,20): warning CS8605: Possible null reference assignment to iteration variable
// foreach (T y2 in new S<T?>())
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "T").WithLocation(27, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "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),
......@@ -50791,12 +50801,24 @@ static void F3(IEnumerable<IOut<object>> x3, IEnumerable<IOut<object?>> y3)
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (9,29): warning CS8619: Nullability of reference types in value of type 'I<object>' doesn't match target type 'I<object?>'.
// foreach (I<object?> a1 in x1)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "a1").WithArguments("I<object>", "I<object?>").WithLocation(9, 29),
// (10,13): warning CS8602: Dereference of a possibly null reference.
// a1.P.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a1.P").WithLocation(10, 13),
// (11,28): warning CS8619: Nullability of reference types in value of type 'I<object?>' doesn't match target type 'I<object>'.
// foreach (I<object> b1 in y1)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "b1").WithArguments("I<object?>", "I<object>").WithLocation(11, 28),
// (16,31): warning CS8619: Nullability of reference types in value of type 'IIn<object>' doesn't match target type 'IIn<object?>'.
// foreach (IIn<object?> a2 in x2)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "a2").WithArguments("IIn<object>", "IIn<object?>").WithLocation(16, 31),
// (24,13): warning CS8602: Dereference of a possibly null reference.
// a3.P.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a3.P").WithLocation(24, 13));
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a3.P").WithLocation(24, 13),
// (25,31): warning CS8619: Nullability of reference types in value of type 'IOut<object?>' doesn't match target type 'IOut<object>'.
// foreach (IOut<object> b3 in y3)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "b3").WithArguments("IOut<object?>", "IOut<object>").WithLocation(25, 31));
}
[Fact]
......@@ -50829,18 +50851,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,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (11,20): warning CS8605: Possible null reference assignment to iteration variable
// foreach (A a3 in c)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "A").WithLocation(11, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "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,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (15,20): warning CS8605: Possible null reference assignment to iteration variable
// foreach (B b2 in c)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "B").WithLocation(15, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "b2").WithLocation(15, 20),
// (16,13): warning CS8602: Dereference of a possibly null reference.
// b2.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "b2").WithLocation(16, 13));
......@@ -50872,8 +50894,6 @@ static void F(A<object?>[] c)
b1.ToString();
}
}";
// https://github.com/dotnet/roslyn/issues/29971: Should report WRN_NullabilityMismatchInAssignment
// for `A<object> a3 in c` and `B b1 in c`.
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (13,13): warning CS8602: Dereference of a possibly null reference.
......@@ -50881,10 +50901,17 @@ static void F(A<object?>[] c)
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a1.F").WithLocation(13, 13),
// (15,13): warning CS8602: Dereference of a possibly null reference.
// a2.F.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a2.F").WithLocation(15, 13));
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a2.F").WithLocation(15, 13),
// (16,28): warning CS8619: Nullability of reference types in value of type 'A<object?>' doesn't match target type 'A<object>'.
// foreach (A<object> a3 in c)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "a3").WithArguments("A<object?>", "A<object>").WithLocation(16, 28),
// (18,20): warning CS8619: Nullability of reference types in value of type 'A<object?>' doesn't match target type 'B'.
// foreach (B b1 in c)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "b1").WithArguments("A<object?>", "B").WithLocation(18, 20));
}
[Fact]
[WorkItem(29971, "https://github.com/dotnet/roslyn/issues/29971")]
public void ForEach_11()
{
var source =
......@@ -50912,18 +50939,16 @@ static void F(IEnumerable<A> e)
}
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
// https://github.com/dotnet/roslyn/issues/29971: Location of WRN_ConvertingNullableToNonNullable should be `y` rather than `B`.
comp.VerifyDiagnostics(
// (15,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (15,20): warning CS8605: Possible null reference assignment to iteration variable
// foreach (B y in e)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "B").WithLocation(15, 18),
Diagnostic(ErrorCode.WRN_NullReferenceIterationVariable, "y").WithLocation(15, 20),
// (16,13): warning CS8602: Dereference of a possibly null reference.
// y.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(16, 13),
// (19,13): warning CS8602: Dereference of a possibly null reference.
// z.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(19, 13)
);
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(19, 13));
}
[WorkItem(23493, "https://github.com/dotnet/roslyn/issues/23493")]
......@@ -85636,10 +85661,46 @@ static void F1<T>(IEnumerable<(T, T)> e1)
z3.ToString(); // 8
}
}
static void F4((object?, object?)[] arr)
{
foreach ((object, object) el in arr) // 9
{
el.Item1.ToString();
el.Item2.ToString();
}
foreach ((object i1, object i2) in arr) // 10, 11
{
i1.ToString(); // 12
i2.ToString(); // 13
}
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
// https://github.com/dotnet/roslyn/issues/33017: Report warnings.
comp.VerifyDiagnostics();
// https://github.com/dotnet/roslyn/issues/33017: Report warnings for null dereferences,
// stop reporting warnings for the type mismatches on var. 10 and 11 should probably be W warnings?
comp.VerifyDiagnostics(
// (6,18): warning CS8619: Nullability of reference types in value of type '(T, T)' doesn't match target type 'T'.
// foreach (var (x1, y1) in e1)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "var (x1, y1)").WithArguments("(T, T)", "T").WithLocation(6, 18),
// (6,18): warning CS8619: Nullability of reference types in value of type '(T, T)' doesn't match target type 'T'.
// foreach (var (x1, y1) in e1)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "var (x1, y1)").WithArguments("(T, T)", "T").WithLocation(6, 18),
// (19,18): warning CS8619: Nullability of reference types in value of type '(T, T?)' doesn't match target type 'T'.
// foreach (var (x2, y2) in e2)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "var (x2, y2)").WithArguments("(T, T?)", "T").WithLocation(19, 18),
// (19,18): warning CS8619: Nullability of reference types in value of type '(T, T?)' doesn't match target type 'T'.
// foreach (var (x2, y2) in e2)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "var (x2, y2)").WithArguments("(T, T?)", "T").WithLocation(19, 18),
// (32,18): warning CS8619: Nullability of reference types in value of type '(T, T?)' doesn't match target type 'T'.
// foreach (var (x3, y3) in e3)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "var (x3, y3)").WithArguments("(T, T?)", "T").WithLocation(32, 18),
// (32,18): warning CS8619: Nullability of reference types in value of type '(T, T?)' doesn't match target type 'T?'.
// foreach (var (x3, y3) in e3)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "var (x3, y3)").WithArguments("(T, T?)", "T?").WithLocation(32, 18),
// (45,35): warning CS8619: Nullability of reference types in value of type '(object?, object?)' doesn't match target type '(object, object)'.
// foreach ((object, object) el in arr) // 9
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "el").WithArguments("(object?, object?)", "(object, object)").WithLocation(45, 35));
}
[Fact]
......@@ -269,6 +269,7 @@ public void WarningLevel_2()
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:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册