-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
52624ec
Fix issue with 'use explicit cast' and compound assignments
CyrusNajmabadi 267a19b
Simplify
CyrusNajmabadi 9e2c539
Fix
CyrusNajmabadi 48c225e
Simplify
CyrusNajmabadi 5615c2c
Simplify
CyrusNajmabadi ab08bdf
Improve cases with extra parens
CyrusNajmabadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Simplify
- Loading branch information
commit 267a19b0c47916afe0b0ceaddc3b76d2850a64ac
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
@@ -15,7 +14,6 @@ | |
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Microsoft.CodeAnalysis.Shared.Collections; | ||
using Microsoft.CodeAnalysis.Shared.Extensions; | ||
using Microsoft.CodeAnalysis.Simplification; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.CodeFixes.AddExplicitCast; | ||
|
@@ -30,7 +28,7 @@ internal abstract partial class AbstractAddExplicitCastCodeFixProvider<TExpressi | |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stronger type. |
||
|
||
/// <summary> | ||
/// Output the current type information of the target node and the conversion type(s) that the target node is | ||
|
@@ -91,8 +89,12 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | |
|
||
actions.Add(CodeAction.Create( | ||
title, | ||
cancellationToken => Task.FromResult(document.WithSyntaxRoot( | ||
ApplyFix(document, semanticModel, root, targetNode, conversionType, cancellationToken))), | ||
cancellationToken => | ||
{ | ||
var (finalTarget, replacement) = ApplyFix(document, semanticModel, targetNode, conversionType, cancellationToken); | ||
|
||
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(finalTarget, replacement))); | ||
}, | ||
title)); | ||
} | ||
|
||
|
@@ -101,10 +103,9 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | |
context.Diagnostics); | ||
} | ||
|
||
private SyntaxNode ApplyFix( | ||
private (SyntaxNode finalTarget, SyntaxNode finalReplacement) ApplyFix( | ||
Document document, | ||
SemanticModel semanticModel, | ||
SyntaxNode currentRoot, | ||
TExpressionSyntax targetNode, | ||
ITypeSymbol conversionType, | ||
CancellationToken cancellationToken) | ||
|
@@ -126,28 +127,22 @@ private SyntaxNode ApplyFix( | |
if (firstConversion is { IsImplicit: false, IsReference: true } or { IsIdentity: true } && | ||
secondConversion is { IsImplicit: false, IsReference: true }) | ||
{ | ||
return currentRoot.ReplaceNode( | ||
targetNode, | ||
this.Cast((TExpressionSyntax)castedExpression, conversionType) | ||
.WithTriviaFrom(targetNode)); | ||
return (targetNode, this.Cast(castedExpression, conversionType).WithTriviaFrom(targetNode)); | ||
} | ||
} | ||
} | ||
else if (TryLanguageSpecificFix(semanticModel, currentRoot, targetNode, cancellationToken, out var replacement)) | ||
{ | ||
return replacement; | ||
} | ||
|
||
return currentRoot.ReplaceNode( | ||
targetNode, | ||
this.Cast(targetNode, conversionType)); | ||
return Cast(semanticModel, targetNode, conversionType); | ||
} | ||
|
||
protected virtual (SyntaxNode finalTarget, SyntaxNode finalReplacement) Cast(SemanticModel semanticModel, TExpressionSyntax targetNode, ITypeSymbol conversionType) | ||
=> (targetNode, this.Cast(targetNode, conversionType)); | ||
|
||
protected virtual bool TryLanguageSpecificFix( | ||
SemanticModel semanticModel, SyntaxNode currentRoot, TExpressionSyntax targetNode, CancellationToken cancellationToken, | ||
[NotNullWhen(true)] out SyntaxNode? replacement) | ||
out (SyntaxNode finalTarget, SyntaxNode replacement) result) | ||
{ | ||
replacement = null; | ||
result = default; | ||
return false; | ||
} | ||
|
||
|
@@ -223,7 +218,7 @@ await editor.ApplyExpressionLevelSemanticEditsAsync( | |
if (TryGetTargetTypeInfo(document, semanticModel, root, diagnostics[0].Id, spanNode, cancellationToken, out var potentialConversionTypes) && | ||
potentialConversionTypes.Length == 1) | ||
{ | ||
return ApplyFix(document, semanticModel, root, potentialConversionTypes[0].node, potentialConversionTypes[0].type, cancellationToken); | ||
var (newTarget, newReplacement) = ApplyFix(document, semanticModel, root, potentialConversionTypes[0].node, potentialConversionTypes[0].type, cancellationToken); | ||
} | ||
|
||
return root; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.