提交 9516be26 编写于 作者: V vsadov

Report nullness warnings for nullable collections in foreach.

Fixes:#31503
上级 c4c3e1ab
......@@ -2593,6 +2593,14 @@ private TypeWithState VisitCallReceiver(BoundCall node)
{
checkNullableValueType = true;
}
else if (method.OriginalDefinition == compilation.GetSpecialTypeMember(SpecialMember.System_Nullable_T_get_Value))
{
// call to get_Value may not occur directly in source, but may be inserted as a result of premature lowering.
// One example where we do it is foreach with nullables.
// The reason is Dev10 compatibility (see: UnwrapCollectionExpressionIfNullable in ForEachLoopBinder.cs)
// Regardless of the reasons, we know that the method does not tolerate nulls.
checkNullableValueType = true;
}
// https://github.com/dotnet/roslyn/issues/30598: Mark receiver as not null
// after arguments have been visited, and only if the receiver has not changed.
......
......@@ -79781,8 +79781,15 @@ static void F1(S? s)
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
// https://github.com/dotnet/roslyn/issues/31503: Report warning for `.Value.GetEnumerator()` calls.
comp.VerifyDiagnostics();
comp.VerifyDiagnostics(
// (11,27): warning CS8629: Nullable value type may be null.
// foreach (var i in s) // 1
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "s").WithLocation(11, 27),
// (18,27): warning CS8629: Nullable value type may be null.
// foreach (var i in t) // 2
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "t").WithLocation(18, 27)
);
}
[Fact]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册