未验证 提交 ac8a56e6 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #43167 from ryzngard/issues/41869_uninitialized_property_nullable

Allow the DeclareAsNullable code fix to work for CS8618
......@@ -950,5 +950,24 @@ void M()
int this[string x] { get { throw null!; } set { throw null!; } }
}", parameters: s_nullableFeature);
}
[Fact]
public async Task FixPropertyDeclaration_Unassigned()
{
await TestInRegularAndScript1Async(
@"#nullable enable
class C
{
string [|S|] { get; }
}",
@"#nullable enable
class C
{
string? S { get; }
}",
parameters: s_nullableFeature);
}
}
}
......@@ -43,7 +43,8 @@ public CSharpDeclareAsNullableCodeFixProvider()
// warning CS8603: Possible null reference return.
// warning CS8600: Converting null literal or possible null value to non-nullable type.
// warning CS8625: Cannot convert null literal to non-nullable reference type.
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create("CS8603", "CS8600", "CS8625");
// warning CS8618: Non-nullable property is uninitialized
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create("CS8603", "CS8600", "CS8625", "CS8618");
internal sealed override CodeFixCategory CodeFixCategory => CodeFixCategory.Compile;
......@@ -272,6 +273,13 @@ private static void MakeDeclarationNullable(SyntaxEditor editor, SemanticModel m
return propertyDeclaration.Type;
}
// string x { get; }
// Unassigned value that's not marked as null
if (node is PropertyDeclarationSyntax propertyDeclarationSyntax)
{
return propertyDeclarationSyntax.Type;
}
// void M(string x = null) { }
if (node.Parent.IsParentKind(SyntaxKind.Parameter, out ParameterSyntax? optionalParameter))
{
......@@ -342,7 +350,8 @@ private static bool IsExpressionSupported(SyntaxNode node)
SyntaxKind.DefaultExpression,
SyntaxKind.DefaultLiteralExpression,
SyntaxKind.ConditionalExpression,
SyntaxKind.ConditionalAccessExpression);
SyntaxKind.ConditionalAccessExpression,
SyntaxKind.PropertyDeclaration);
}
private class MyCodeAction : CodeAction.DocumentChangeAction
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册