Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not offer to add default switch case when it handles null and an underlying value #74018

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading