Skip to content

Fix issue with remove-unnecessary-cast and necessary explicit casts.#60293

Merged
CyrusNajmabadi merged 2 commits intodotnet:mainfrom
CyrusNajmabadi:implicitCast
Mar 21, 2022
Merged

Fix issue with remove-unnecessary-cast and necessary explicit casts.#60293
CyrusNajmabadi merged 2 commits intodotnet:mainfrom
CyrusNajmabadi:implicitCast

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Mar 21, 2022

Fixes #60292

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner March 21, 2022 17:06
@ghost ghost added the Area-IDE label Mar 21, 2022
// if the new code would have the same conversion as well.
if (originalConversionOperation.Parent is IConversionOperation { IsImplicit: true, Conversion: { IsUserDefined: true } } originalParentImplicitConversion)
if (originalConversionOperation.Parent is IConversionOperation { Conversion.IsUserDefined: true } originalParentConversion &&
originalParentConversion.GetConversion().IsImplicit)
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.

sigh. boneheaded mistake on my part. IConversionOperation.IsImplicit doesn't mean it's an implciit conversion. THis is .IsImplicit inherited from IOperation (where it means the operation was not written in source).

if (rewrittenConditionalOperation.Parent is IConversionOperation { IsImplicit: true } implicitConversion &&
castType.Equals(implicitConversion.Type, SymbolEqualityComparer.IncludeNullability))
if (rewrittenConditionalOperation.Parent is IConversionOperation conditionalParentConversion &&
conditionalParentConversion.GetConversion().IsImplicit &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about defining an extension method “IsImplicitConversion” on IConversionOperation? This will likely prevent someone else from making the same mistake when they see IsImplicit and IsImplicitConversion available on it and read through the doc comments on both to choose the appropriate member to invoke.

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.

possibly. though it doesn't help in this case because in a pattern you only see properties :'(

@CyrusNajmabadi CyrusNajmabadi merged commit f22ba81 into dotnet:main Mar 21, 2022
@ghost ghost added this to the Next milestone Mar 21, 2022
@CyrusNajmabadi CyrusNajmabadi deleted the implicitCast branch March 27, 2022 04:23
@allisonchou allisonchou modified the milestones: Next, 17.2.P3 Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IDE0004 incorrectly reported when casting from nullable enum to nullable int; applying codefix results in non-compilable code

3 participants