Skip to content

Commit

Permalink
Merge pull request #74018 from DoctorKrolic/switch-null-and-underlyin…
Browse files Browse the repository at this point in the history
…g-value

Do not offer to add default switch case when it handles `null` and an underlying value
  • Loading branch information
CyrusNajmabadi authored Jun 16, 2024
2 parents ad9ff58 + e1a461a commit 0d03b2e
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.PopulateSwitch;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -1716,4 +1715,126 @@ public string Method(bool? boolean)
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("int")]
[InlineData("int i")]
public async Task NullableValueTypeWithNullAndUnderlyingValueArms1(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(int? x)
{
return x [||]switch
{
null => -1,
{{underlyingTypePattern}} => 0,
};
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("int")]
[InlineData("int i")]
public async Task NullableValueTypeWithNullAndUnderlyingValueArms2(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(int? x)
{
return x [||]switch
{
{{underlyingTypePattern}} => 0,
null => -1,
};
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("int")]
[InlineData("int i")]
public async Task NullableValueTypeWithNullAndUnderlyingValueArms3(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(int? x)
{
return x [||]switch
{
null => -1,
0 => 0,
{{underlyingTypePattern}} => 1,
};
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("string")]
[InlineData("string s")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueArms1(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(string? x)
{
return x [||]switch
{
null => -1,
{{underlyingTypePattern}} => 0,
};
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("string")]
[InlineData("string s")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueArms2(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(string? x)
{
return x [||]switch
{
{{underlyingTypePattern}} => 0,
null => -1,
};
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("string")]
[InlineData("string s")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueArms3(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(string? x)
{
return x [||]switch
{
null => -1,
"" => 0,
{{underlyingTypePattern}} => 1,
};
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1758,4 +1758,216 @@ void Method()
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("int")]
[InlineData("int i")]
public async Task NullableValueTypeWithNullAndUnderlyingValueCases1(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
void M(int? x)
{
[||]switch (x)
{
case null:
break;
case {{underlyingTypePattern}}:
break;
}
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("int")]
[InlineData("int i")]
public async Task NullableValueTypeWithNullAndUnderlyingValueCases2(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(int? x)
{
[||]switch (x)
{
case {{underlyingTypePattern}}:
break;
case null:
break;
}
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("int")]
[InlineData("int i")]
public async Task NullableValueTypeWithNullAndUnderlyingValueCases3(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
void M(int? x)
{
[||]switch (x)
{
case null:
break;
case 0:
break;
case {{underlyingTypePattern}}:
break;
}
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
public async Task NullableValueTypeWithNullAndUnderlyingValueCases4()
{
await TestMissingInRegularAndScriptAsync("""
class C
{
int M(int? x)
{
[||]switch (x)
{
case null:
case int:
break;
}
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
public async Task NullableValueTypeWithNullAndUnderlyingValueCases5()
{
await TestMissingInRegularAndScriptAsync("""
class C
{
int M(int? x)
{
[||]switch (x)
{
case int:
case null:
break;
}
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("string")]
[InlineData("string s")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases1(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
void M(string? x)
{
[||]switch (x)
{
case null:
break;
case {{underlyingTypePattern}}:
break;
}
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("string")]
[InlineData("string s")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases2(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
int M(string? x)
{
[||]switch (x)
{
case {{underlyingTypePattern}}:
break;
case null:
break;
}
}
}
""");
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
[InlineData("string")]
[InlineData("string s")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases3(string underlyingTypePattern)
{
await TestMissingInRegularAndScriptAsync($$"""
class C
{
void M(string? x)
{
[||]switch (x)
{
case null:
break;
case "":
break;
case {{underlyingTypePattern}}:
break;
}
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases4()
{
await TestMissingInRegularAndScriptAsync("""
class C
{
int M(string? x)
{
[||]switch (x)
{
case null:
case string:
break;
}
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases5()
{
await TestMissingInRegularAndScriptAsync("""
class C
{
int M(string? x)
{
[||]switch (x)
{
case string:
case null:
break;
}
}
}
""");
}
}
Loading

0 comments on commit 0d03b2e

Please sign in to comment.