|
| 1 | +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Composition; |
| 5 | +using Analyzer.Utilities; |
| 6 | +using Analyzer.Utilities.Extensions; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CodeActions; |
| 9 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 10 | +using Microsoft.CodeAnalysis.Editing; |
| 11 | + |
| 12 | +namespace Microsoft.NetCore.Analyzers.Performance |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// CA1517: Use ReadOnlySpan<T> or ReadOnlyMemory<T> instead of Span<T> or Memory<T> |
| 16 | + /// </summary> |
| 17 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PreferReadOnlySpanOverSpanFixer))] |
| 18 | + [Shared] |
| 19 | + public sealed class PreferReadOnlySpanOverSpanFixer : CodeFixProvider |
| 20 | + { |
| 21 | + public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = |
| 22 | + ImmutableArray.Create(PreferReadOnlySpanOverSpanAnalyzer.RuleId); |
| 23 | + |
| 24 | + public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; |
| 25 | + |
| 26 | + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 27 | + { |
| 28 | + var root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 29 | + var node = root.FindNode(context.Span, getInnermostNodeForTie: true); |
| 30 | + var semanticModel = await context.Document.GetRequiredSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); |
| 31 | + |
| 32 | + if (semanticModel.GetDeclaredSymbol(node, context.CancellationToken) is IParameterSymbol parameterSymbol && |
| 33 | + GetReadOnlyTypeName(parameterSymbol.Type) is { } targetTypeName) |
| 34 | + { |
| 35 | + var title = string.Format(MicrosoftNetCoreAnalyzersResources.PreferReadOnlySpanOverSpanCodeFixTitle, targetTypeName); |
| 36 | + |
| 37 | + context.RegisterCodeFix( |
| 38 | + CodeAction.Create( |
| 39 | + title: title, |
| 40 | + createChangedDocument: c => ChangeParameterTypeAsync(context.Document, node, c), |
| 41 | + equivalenceKey: title), |
| 42 | + context.Diagnostics[0]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static string? GetReadOnlyTypeName(ITypeSymbol typeSymbol) => |
| 47 | + typeSymbol is INamedTypeSymbol namedType && namedType.OriginalDefinition.Name is "Span" or "Memory" ? |
| 48 | + $"ReadOnly{typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}" : |
| 49 | + null; |
| 50 | + |
| 51 | + private static async Task<Document> ChangeParameterTypeAsync( |
| 52 | + Document document, |
| 53 | + SyntaxNode node, |
| 54 | + CancellationToken cancellationToken) |
| 55 | + { |
| 56 | + var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); |
| 57 | + var generator = editor.Generator; |
| 58 | + var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); |
| 59 | + |
| 60 | + // Get the parameter symbol to construct the correct type |
| 61 | + var parameterSymbol = semanticModel.GetDeclaredSymbol(node, cancellationToken) as IParameterSymbol; |
| 62 | + if (parameterSymbol?.Type is INamedTypeSymbol namedType && namedType.TypeArguments.Length == 1) |
| 63 | + { |
| 64 | + // Get the compilation to find the readonly types |
| 65 | + var compilation = semanticModel.Compilation; |
| 66 | + var typeName = namedType.OriginalDefinition.Name; |
| 67 | + |
| 68 | + INamedTypeSymbol? readOnlyType = |
| 69 | + typeName is "Span" ? compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReadOnlySpan1) : |
| 70 | + typeName is "Memory" ? compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReadOnlyMemory1) : |
| 71 | + null; |
| 72 | + |
| 73 | + if (readOnlyType is not null) |
| 74 | + { |
| 75 | + // Construct the generic type with the same type argument |
| 76 | + var newTypeNode = generator.TypeExpression( |
| 77 | + readOnlyType.Construct(namedType.TypeArguments[0])); |
| 78 | + |
| 79 | + // Replace the parameter's type |
| 80 | + editor.ReplaceNode(node, (currentNode, gen) => gen.WithType(currentNode, newTypeNode)); |
| 81 | + |
| 82 | + return editor.GetChangedDocument(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return document; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments