Skip to content

Commit 0d03b2e

Browse files
Merge pull request #74018 from DoctorKrolic/switch-null-and-underlying-value
Do not offer to add default switch case when it handles `null` and an underlying value
2 parents ad9ff58 + e1a461a commit 0d03b2e

File tree

8 files changed

+472
-55
lines changed

8 files changed

+472
-55
lines changed

src/Analyzers/CSharp/Tests/PopulateSwitch/PopulateSwitchExpressionTests.cs

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System.Threading.Tasks;
66
using Microsoft.CodeAnalysis.CodeFixes;
7-
using Microsoft.CodeAnalysis.CSharp;
87
using Microsoft.CodeAnalysis.CSharp.PopulateSwitch;
98
using Microsoft.CodeAnalysis.Diagnostics;
109
using Microsoft.CodeAnalysis.Test.Utilities;
@@ -1716,4 +1715,126 @@ public string Method(bool? boolean)
17161715
}
17171716
""");
17181717
}
1718+
1719+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1720+
[InlineData("int")]
1721+
[InlineData("int i")]
1722+
public async Task NullableValueTypeWithNullAndUnderlyingValueArms1(string underlyingTypePattern)
1723+
{
1724+
await TestMissingInRegularAndScriptAsync($$"""
1725+
class C
1726+
{
1727+
int M(int? x)
1728+
{
1729+
return x [||]switch
1730+
{
1731+
null => -1,
1732+
{{underlyingTypePattern}} => 0,
1733+
};
1734+
}
1735+
}
1736+
""");
1737+
}
1738+
1739+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1740+
[InlineData("int")]
1741+
[InlineData("int i")]
1742+
public async Task NullableValueTypeWithNullAndUnderlyingValueArms2(string underlyingTypePattern)
1743+
{
1744+
await TestMissingInRegularAndScriptAsync($$"""
1745+
class C
1746+
{
1747+
int M(int? x)
1748+
{
1749+
return x [||]switch
1750+
{
1751+
{{underlyingTypePattern}} => 0,
1752+
null => -1,
1753+
};
1754+
}
1755+
}
1756+
""");
1757+
}
1758+
1759+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1760+
[InlineData("int")]
1761+
[InlineData("int i")]
1762+
public async Task NullableValueTypeWithNullAndUnderlyingValueArms3(string underlyingTypePattern)
1763+
{
1764+
await TestMissingInRegularAndScriptAsync($$"""
1765+
class C
1766+
{
1767+
int M(int? x)
1768+
{
1769+
return x [||]switch
1770+
{
1771+
null => -1,
1772+
0 => 0,
1773+
{{underlyingTypePattern}} => 1,
1774+
};
1775+
}
1776+
}
1777+
""");
1778+
}
1779+
1780+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1781+
[InlineData("string")]
1782+
[InlineData("string s")]
1783+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueArms1(string underlyingTypePattern)
1784+
{
1785+
await TestMissingInRegularAndScriptAsync($$"""
1786+
class C
1787+
{
1788+
int M(string? x)
1789+
{
1790+
return x [||]switch
1791+
{
1792+
null => -1,
1793+
{{underlyingTypePattern}} => 0,
1794+
};
1795+
}
1796+
}
1797+
""");
1798+
}
1799+
1800+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1801+
[InlineData("string")]
1802+
[InlineData("string s")]
1803+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueArms2(string underlyingTypePattern)
1804+
{
1805+
await TestMissingInRegularAndScriptAsync($$"""
1806+
class C
1807+
{
1808+
int M(string? x)
1809+
{
1810+
return x [||]switch
1811+
{
1812+
{{underlyingTypePattern}} => 0,
1813+
null => -1,
1814+
};
1815+
}
1816+
}
1817+
""");
1818+
}
1819+
1820+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1821+
[InlineData("string")]
1822+
[InlineData("string s")]
1823+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueArms3(string underlyingTypePattern)
1824+
{
1825+
await TestMissingInRegularAndScriptAsync($$"""
1826+
class C
1827+
{
1828+
int M(string? x)
1829+
{
1830+
return x [||]switch
1831+
{
1832+
null => -1,
1833+
"" => 0,
1834+
{{underlyingTypePattern}} => 1,
1835+
};
1836+
}
1837+
}
1838+
""");
1839+
}
17191840
}

src/Analyzers/CSharp/Tests/PopulateSwitch/PopulateSwitchStatementTests.cs

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,4 +1758,216 @@ void Method()
17581758
}
17591759
""");
17601760
}
1761+
1762+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1763+
[InlineData("int")]
1764+
[InlineData("int i")]
1765+
public async Task NullableValueTypeWithNullAndUnderlyingValueCases1(string underlyingTypePattern)
1766+
{
1767+
await TestMissingInRegularAndScriptAsync($$"""
1768+
class C
1769+
{
1770+
void M(int? x)
1771+
{
1772+
[||]switch (x)
1773+
{
1774+
case null:
1775+
break;
1776+
case {{underlyingTypePattern}}:
1777+
break;
1778+
}
1779+
}
1780+
}
1781+
""");
1782+
}
1783+
1784+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1785+
[InlineData("int")]
1786+
[InlineData("int i")]
1787+
public async Task NullableValueTypeWithNullAndUnderlyingValueCases2(string underlyingTypePattern)
1788+
{
1789+
await TestMissingInRegularAndScriptAsync($$"""
1790+
class C
1791+
{
1792+
int M(int? x)
1793+
{
1794+
[||]switch (x)
1795+
{
1796+
case {{underlyingTypePattern}}:
1797+
break;
1798+
case null:
1799+
break;
1800+
}
1801+
}
1802+
}
1803+
""");
1804+
}
1805+
1806+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1807+
[InlineData("int")]
1808+
[InlineData("int i")]
1809+
public async Task NullableValueTypeWithNullAndUnderlyingValueCases3(string underlyingTypePattern)
1810+
{
1811+
await TestMissingInRegularAndScriptAsync($$"""
1812+
class C
1813+
{
1814+
void M(int? x)
1815+
{
1816+
[||]switch (x)
1817+
{
1818+
case null:
1819+
break;
1820+
case 0:
1821+
break;
1822+
case {{underlyingTypePattern}}:
1823+
break;
1824+
}
1825+
}
1826+
}
1827+
""");
1828+
}
1829+
1830+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1831+
public async Task NullableValueTypeWithNullAndUnderlyingValueCases4()
1832+
{
1833+
await TestMissingInRegularAndScriptAsync("""
1834+
class C
1835+
{
1836+
int M(int? x)
1837+
{
1838+
[||]switch (x)
1839+
{
1840+
case null:
1841+
case int:
1842+
break;
1843+
}
1844+
}
1845+
}
1846+
""");
1847+
}
1848+
1849+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1850+
public async Task NullableValueTypeWithNullAndUnderlyingValueCases5()
1851+
{
1852+
await TestMissingInRegularAndScriptAsync("""
1853+
class C
1854+
{
1855+
int M(int? x)
1856+
{
1857+
[||]switch (x)
1858+
{
1859+
case int:
1860+
case null:
1861+
break;
1862+
}
1863+
}
1864+
}
1865+
""");
1866+
}
1867+
1868+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1869+
[InlineData("string")]
1870+
[InlineData("string s")]
1871+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases1(string underlyingTypePattern)
1872+
{
1873+
await TestMissingInRegularAndScriptAsync($$"""
1874+
class C
1875+
{
1876+
void M(string? x)
1877+
{
1878+
[||]switch (x)
1879+
{
1880+
case null:
1881+
break;
1882+
case {{underlyingTypePattern}}:
1883+
break;
1884+
}
1885+
}
1886+
}
1887+
""");
1888+
}
1889+
1890+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1891+
[InlineData("string")]
1892+
[InlineData("string s")]
1893+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases2(string underlyingTypePattern)
1894+
{
1895+
await TestMissingInRegularAndScriptAsync($$"""
1896+
class C
1897+
{
1898+
int M(string? x)
1899+
{
1900+
[||]switch (x)
1901+
{
1902+
case {{underlyingTypePattern}}:
1903+
break;
1904+
case null:
1905+
break;
1906+
}
1907+
}
1908+
}
1909+
""");
1910+
}
1911+
1912+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1913+
[InlineData("string")]
1914+
[InlineData("string s")]
1915+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases3(string underlyingTypePattern)
1916+
{
1917+
await TestMissingInRegularAndScriptAsync($$"""
1918+
class C
1919+
{
1920+
void M(string? x)
1921+
{
1922+
[||]switch (x)
1923+
{
1924+
case null:
1925+
break;
1926+
case "":
1927+
break;
1928+
case {{underlyingTypePattern}}:
1929+
break;
1930+
}
1931+
}
1932+
}
1933+
""");
1934+
}
1935+
1936+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1937+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases4()
1938+
{
1939+
await TestMissingInRegularAndScriptAsync("""
1940+
class C
1941+
{
1942+
int M(string? x)
1943+
{
1944+
[||]switch (x)
1945+
{
1946+
case null:
1947+
case string:
1948+
break;
1949+
}
1950+
}
1951+
}
1952+
""");
1953+
}
1954+
1955+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/50983")]
1956+
public async Task NullableReferenceTypeWithNullAndUnderlyingValueCases5()
1957+
{
1958+
await TestMissingInRegularAndScriptAsync("""
1959+
class C
1960+
{
1961+
int M(string? x)
1962+
{
1963+
[||]switch (x)
1964+
{
1965+
case string:
1966+
case null:
1967+
break;
1968+
}
1969+
}
1970+
}
1971+
""");
1972+
}
17611973
}

0 commit comments

Comments
 (0)