Skip to content

Commit

Permalink
Revert "Fix converted nullability on extract method (dotnet#39134)"
Browse files Browse the repository at this point in the history
This reverts commit ba47b2f.
  • Loading branch information
allisonchou committed Oct 31, 2019
1 parent 855509a commit 73b18ad
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2815,313 +2815,5 @@ private void NewMethod()
}
}");
}


[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNullableObjectWithExplicitCast()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class C
{
void M()
{
object? o = null;
var s = (string?)[|o|];
Console.WriteLine(s);
}
}",
@"#nullable enable
using System;
class C
{
void M()
{
object? o = null;
var s = (string?){|Rename:GetO|}(o);
Console.WriteLine(s);
}
private static object? GetO(object? o)
{
return o;
}
}");

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNotNullableObjectWithExplicitCast()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class C
{
void M()
{
object? o = new object();
var s = (string)[|o|];
Console.WriteLine(s);
}
}",
@"#nullable enable
using System;
class C
{
void M()
{
object? o = new object();
var s = (string){|Rename:GetO|}(o);
Console.WriteLine(s);
}
private static object GetO(object o)
{
return o;
}
}");

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNotNullableWithExplicitCast()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class A
{
}
class B : A
{
}
class C
{
void M()
{
B? b = new B();
var s = (A)[|b|];
}
}",
@"#nullable enable
using System;
class A
{
}
class B : A
{
}
class C
{
void M()
{
B? b = new B();
var s = (A){|Rename:GetB|}(b);
}
private static B GetB(B b)
{
return b;
}
}");

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNullableWithExplicitCast()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class A
{
}
class B : A
{
}
class C
{
void M()
{
B? b = null;
var s = (A)[|b|];
}
}",
@"#nullable enable
using System;
class A
{
}
class B : A
{
}
class C
{
void M()
{
B? b = null;
var s = (A){|Rename:GetB|}(b);
}
private static B? GetB(B? b)
{
return b;
}
}");

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNotNullableWithExplicitCastSelected()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class C
{
void M()
{
object? o = new object();
var s = [|(string)o|];
Console.WriteLine(s);
}
}",
@"#nullable enable
using System;
class C
{
void M()
{
object? o = new object();
var s = {|Rename:GetS|}(o);
Console.WriteLine(s);
}
private static string GetS(object o)
{
return (string)o;
}
}");

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNullableWithExplicitCastSelected()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class C
{
void M()
{
object? o = null;
var s = [|(string?)o|];
Console.WriteLine(s);
}
}",
@"#nullable enable
using System;
class C
{
void M()
{
object? o = null;
var s = {|Rename:GetS|}(o);
Console.WriteLine(s);
}
private static string? GetS(object? o)
{
return (string?)o;
}
}");
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNullableNonNullFlowWithExplicitCastSelected()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class C
{
void M()
{
object? o = new object();
var s = [|(string?)o|];
Console.WriteLine(s);
}
}",
@"#nullable enable
using System;
class C
{
void M()
{
object? o = new object();
var s = {|Rename:GetS|}(o);
Console.WriteLine(s);
}
private static string? GetS(object o)
{
return (string?)o;
}
}");

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public Task TestExtractNullableToNonNullableWithExplicitCastSelected()
=> TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class C
{
void M()
{
object? o = null;
var s = [|(string)o|];
Console.WriteLine(s);
}
}",
@"#nullable enable
using System;
class C
{
void M()
{
object? o = null;
var s = {|Rename:GetS|}(o);
Console.WriteLine(s);
}
private static string? GetS(object? o)
{
return (string)o;
}
}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public override ITypeSymbol GetContainingScopeType()
// 2. if it doesn't, even if the cast itself wasn't included in the selection, we will treat it
// as it was in the selection
var regularType = GetRegularExpressionType(model, node);
if (regularType != null)
if (regularType != null && !regularType.IsObjectType())
{
return regularType;
}

if (node.Parent is CastExpressionSyntax castExpression)
{
return model.GetTypeInfo(castExpression).GetTypeWithFlowNullability();
return model.GetTypeInfo(castExpression.Type).GetTypeWithAnnotatedNullability();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public static T WithoutNullability<T>(this T typeSymbol) where T : INamespaceOrT
}

public static ITypeSymbol GetConvertedTypeWithFlowNullability(this TypeInfo typeInfo)
=> typeInfo.ConvertedType?.WithNullability(typeInfo.ConvertedNullability.FlowState);
=> typeInfo.ConvertedType?.WithNullability(typeInfo.Nullability.FlowState);

public static ITypeSymbol GetConvertedTypeWithAnnotatedNullability(this TypeInfo typeInfo)
=> typeInfo.ConvertedType?.WithNullability(typeInfo.ConvertedNullability.Annotation);
=> typeInfo.ConvertedType?.WithNullability(typeInfo.Nullability.Annotation);

public static ITypeSymbol GetTypeWithFlowNullability(this TypeInfo typeInfo)
=> typeInfo.Type?.WithNullability(typeInfo.Nullability.FlowState);
Expand Down

0 comments on commit 73b18ad

Please sign in to comment.