Skip to content

Fix cast simplification issues#57066

Merged
CyrusNajmabadi merged 22 commits intodotnet:mainfrom
CyrusNajmabadi:castSimplifierWork
Oct 11, 2021
Merged

Fix cast simplification issues#57066
CyrusNajmabadi merged 22 commits intodotnet:mainfrom
CyrusNajmabadi:castSimplifierWork

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Oct 9, 2021

Fixes #57065
Fixes #57064
Fixes #57063

Also fixes several cases in #56938

@ghost ghost added the Area-IDE label Oct 9, 2021
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 10, 2021 00:40
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task TestUnintendedReferenceComparison1()
{
await VerifyCS.VerifyCodeFixAsync(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are things that we do not want to report diagnostics on removing the cast will cause a warning from teh compiler.

public async Task DoNotRemoveEqualityWarningSilencingCast1()
{
var source =
@"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests are similar to what we already have, except these test against interfaces, not just reference types.

@CyrusNajmabadi CyrusNajmabadi changed the title Add tests Fix cast simplification issues Oct 10, 2021
public async Task DoNotRemoveCastFromStringTypeToObjectInReferenceEquality()
{
await TestInRegularAndScriptAsync(
await TestMissingInRegularAndScriptAsync(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing this cast causes a warning. so we need to preserve it.

return false;

if (CastRemovalWouldCauseUnintendedReferenceComparisonWarning(rewrittenExpression, rewrittenSemanticModel, cancellationToken))
return false;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes issue where removing the cast has no semantic/IL impact, but would cause a warning.


// If the types of the expressions are the same, then we can remove safely.
if (Equals(originalConvertedType, rewrittenConvertedType))
if (originalConvertedType.Equals(rewrittenConvertedType, SymbolEqualityComparer.IncludeNullability))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes issue where we'd remove a necessary NRT cast.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@davidwengier ptal. thanks!

@davidwengier
Copy link
Copy Markdown
Member

@davidwengier ptal. thanks!

Do I get a quid for my pro quo?

@CyrusNajmabadi CyrusNajmabadi merged commit e4458a6 into dotnet:main Oct 11, 2021
@ghost ghost added this to the Next milestone Oct 11, 2021
@CyrusNajmabadi CyrusNajmabadi deleted the castSimplifierWork branch October 11, 2021 06:32
@RikkiGibson RikkiGibson modified the milestones: Next, 17.1.P1 Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

3 participants