未验证 提交 920ad824 编写于 作者: A Andy Gocke 提交者: GitHub

Report nullable warnings for receiver of pattern indexers (#37216)

Fixes #36241
上级 ede3ecf9
......@@ -6036,7 +6036,12 @@ public override BoundNode VisitIndexerAccess(BoundIndexerAccess node)
public override BoundNode VisitIndexOrRangePatternIndexerAccess(BoundIndexOrRangePatternIndexerAccess node)
{
var receiverType = VisitRvalueWithState(node.Receiver);
BoundExpression receiver = node.Receiver;
var receiverType = VisitRvalueWithState(receiver);
// https://github.com/dotnet/roslyn/issues/30598: Mark receiver as not null
// after indices have been visited, and only if the receiver has not changed.
_ = CheckPossibleNullReceiver(receiver);
VisitRvalue(node.Argument);
var patternSymbol = node.PatternSymbol;
if (!receiverType.HasNullType)
......
......@@ -101387,6 +101387,33 @@ static void F3(int? z, int? w)
comp.VerifyDiagnostics();
}
[Fact]
public void PatternIndexer()
{
var src = @"
#nullable enable
class C
{
static void M1(string? s)
{
_ = s[^1];
}
static void M2(string? s)
{
_ = s[1..10];
}
}";
var comp = CreateCompilationWithIndexAndRange(src, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (7,9): warning CS8602: Dereference of a possibly null reference.
// _ = s[^1];
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(7, 9),
// (12,9): warning CS8602: Dereference of a possibly null reference.
// _ = s[1..10];
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(12, 9));
}
[WorkItem(31770, "https://github.com/dotnet/roslyn/issues/31770")]
[Fact]
public void UserDefinedConversion_NestedNullability_01()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册