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

Don't remove null-casts in switch expressions #43701

Merged
merged 1 commit into from
Apr 27, 2020
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 @@ -6957,5 +6957,217 @@ public static void Main()

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveNullCastInSwitch1()
{
var source =
@"class Program
{
static void Main()
{
switch ((object)null)
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveNullCastInSwitch2()
{
var source =
@"class Program
{
static void Main()
{
switch ((object)(null))
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveNullCastInSwitch3()
{
var source =
@"class Program
{
static void Main()
{
switch ((bool?)null)
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveNullCastInSwitch4()
{
var source =
@"class Program
{
static void Main()
{
switch ((bool?)(null))
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveNullCastInSwitch5()
{
var source =
@"class Program
{
static void Main()
{
switch (((object)null))
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveDefaultCastInSwitch1()
{
var source =
@"class Program
{
static void Main()
{
switch ((object)default)
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveDefaultCastInSwitch2()
{
var source =
@"class Program
{
static void Main()
{
switch ((object)(default))
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveDefaultCastInSwitch3()
{
var source =
@"class Program
{
static void Main()
{
switch ((bool?)default)
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveDefaultCastInSwitch4()
{
var source =
@"class Program
{
static void Main()
{
switch ((bool?)(default))
{
case bool _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, source);
}

[WorkItem(20211, "https://github.com/dotnet/roslyn/issues/20211")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task RemoveDoubleNullCastInSwitch1()
{
var source =
@"class Program
{
static void Main()
{
switch ([|(object)|][|(string)|]null)
{
case var _:
break;
}
}
}";
var fixedCode =
@"class Program
{
static void Main()
{
switch ((string)null)
{
case var _:
break;
}
}
}";

await VerifyCS.VerifyCodeFixAsync(source, fixedCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ protected override bool ReplacementChangesSemanticsForNodeLanguageSpecific(Synta
// expression type are not broken.

var newSwitchStatement = (SwitchStatementSyntax)currentReplacedNode;
var previousReplacedExpression = (ExpressionSyntax)previousReplacedNode;

// it is never legal to use `default/null` in a switch statement's expression.
if (previousReplacedExpression.WalkDownParentheses().IsKind(SyntaxKind.NullLiteralExpression, SyntaxKind.DefaultLiteralExpression))
return true;

var originalSwitchLabels = originalSwitchStatement.Sections.SelectMany(section => section.Labels).ToArray();
var newSwitchLabels = newSwitchStatement.Sections.SelectMany(section => section.Labels).ToArray();
Expand Down