未验证 提交 dd4dbcdb 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #43083 from faso/master

CanAddNullCheck update for nullable reference types
......@@ -3751,7 +3751,7 @@ void M()
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
public async Task TestWitNestedNullability()
public async Task TestWithNestedNullability()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
......
......@@ -1002,6 +1002,84 @@ class Z
optionsCallback: options => options[0].Value = true);
}
[WorkItem(41428, "https://github.com/dotnet/roslyn/issues/41428")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructorFromMembers)]
public async Task TestAddNullChecksWithNullableReferenceType()
{
await TestWithPickMembersDialogAsync(
@"
using System;
using System.Collections.Generic;
#nullable enable
class Z
{
int a;
string b;
string? c;
[||]
}",
@"
using System;
using System.Collections.Generic;
#nullable enable
class Z
{
int a;
string b;
string? c;
public Z(int a, string b, string? c{|Navigation:)|}
{
this.a = a;
this.b = b ?? throw new ArgumentNullException(nameof(b));
this.c = c;
}
}",
chosenSymbols: new string[] { "a", "b", "c" },
optionsCallback: options => options[0].Value = true);
}
[WorkItem(41428, "https://github.com/dotnet/roslyn/issues/41428")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructorFromMembers)]
public async Task TestAddNullChecksWithNullableReferenceTypeForGenerics()
{
await TestWithPickMembersDialogAsync(
@"
using System;
using System.Collections.Generic;
#nullable enable
class Z<T> where T : class
{
int a;
string b;
T? c;
[||]
}",
@"
using System;
using System.Collections.Generic;
#nullable enable
class Z<T> where T : class
{
int a;
string b;
T? c;
public Z(int a, string b, T c{|Navigation:)|}
{
this.a = a;
this.b = b ?? throw new ArgumentNullException(nameof(b));
this.c = c;
}
}",
chosenSymbols: new string[] { "a", "b", "c" },
optionsCallback: options => options[0].Value = true);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructorFromMembers)]
public async Task TestAddNullChecks2()
{
......
......@@ -23,7 +23,19 @@ internal static partial class ITypeSymbolExtensions
private const string DefaultBuiltInParameterName = "v";
public static bool CanAddNullCheck([NotNullWhen(returnValue: true)] this ITypeSymbol? type)
#if CODE_STYLE // TODO: Remove this #if once 'WithNullableAnnotation' and 'NullableAnnotation' are available in CodeStyle layer.
=> type != null && (type.IsReferenceType || type.IsNullable());
#else
{
if (type == null)
return false;
var isNullableValueType = type.IsNullable();
var isNonNullableReferenceType = type.IsReferenceType && type.NullableAnnotation != NullableAnnotation.Annotated;
return isNullableValueType || isNonNullableReferenceType;
}
#endif
public static IList<INamedTypeSymbol> GetAllInterfacesIncludingThis(this ITypeSymbol type)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册