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

Fix issue with 'use explicit cast' and compound assignments #74225

Merged
merged 6 commits into from
Jul 1, 2024

Conversation

CyrusNajmabadi
Copy link
Member

Fixes #56141.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner July 1, 2024 20:16
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 1, 2024
SemanticModel semanticModel, ExpressionSyntax targetNode, ITypeSymbol conversionType)
{
// The compiler is very ambiguous with assignment expressions `(a += b)`. An error on it may be an error on the
// entire expression or on the RHS of the assignment. Have to reverse engineer what it is doing here.
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the main fix.

/// </summary>
private const int MaximumConversionOptions = 3;

protected abstract TExpressionSyntax Cast(TExpressionSyntax expression, ITypeSymbol type);
protected abstract void GetPartsOfCastOrConversionExpression(TExpressionSyntax expression, out SyntaxNode type, out SyntaxNode castedExpression);
protected abstract void GetPartsOfCastOrConversionExpression(TExpressionSyntax expression, out SyntaxNode type, out TExpressionSyntax castedExpression);
Copy link
Member Author

Choose a reason for hiding this comment

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

stronger type.

using var _ = ArrayBuilder<(TExpressionSyntax node, ITypeSymbol type)>.GetInstance(out var candidates);

this.AddPotentialTargetTypes(document, semanticModel, root, diagnosticId, spanNode, candidates, cancellationToken);
candidates.RemoveDuplicates();
Copy link
Member Author

Choose a reason for hiding this comment

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

sharing common code between C# and VB

@CyrusNajmabadi
Copy link
Member Author

@akhera99 ptal :)

this.Cast((TExpressionSyntax)castedExpression, conversionType)
.WithTriviaFrom(targetNode)
.WithAdditionalAnnotations(Simplifier.Annotation));
var firstConversion = semanticFacts.ClassifyConversion(semanticModel, castedExpression, castType);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: could move the def of semanticFacts to here

Copy link
Member Author

Choose a reason for hiding this comment

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

yup. though i'm ok with this.

@CyrusNajmabadi CyrusNajmabadi merged commit eced12e into dotnet:main Jul 1, 2024
25 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the compoundAssignCast branch July 1, 2024 22:40
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jul 1, 2024
@RikkiGibson RikkiGibson modified the milestones: Next, 17.12 P1 Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CS0266 suggests invalid code fix
3 participants