-
-
Notifications
You must be signed in to change notification settings - Fork 244
/
UseElementAccessAnalysis.cs
123 lines (92 loc) · 4.8 KB
/
UseElementAccessAnalysis.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CSharp.Syntax;
using static Roslynator.SymbolUtility;
namespace Roslynator.CSharp.Analysis;
internal static class UseElementAccessAnalysis
{
public static bool IsFixableElementAt(
in SimpleMemberInvocationExpressionInfo invocationInfo,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
InvocationExpressionSyntax invocationExpression = invocationInfo.InvocationExpression;
if (invocationExpression.IsParentKind(SyntaxKind.ExpressionStatement))
return false;
IMethodSymbol methodSymbol = semanticModel.GetReducedExtensionMethodInfo(invocationExpression, cancellationToken).Symbol;
if (methodSymbol is null)
return false;
if (!IsLinqElementAt(methodSymbol, allowImmutableArrayExtension: true))
return false;
ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken);
if (!HasAccessibleIndexer(typeSymbol, semanticModel, invocationExpression.SpanStart))
return false;
ElementAccessExpressionSyntax elementAccess = SyntaxFactory.ElementAccessExpression(
invocationInfo.Expression,
CSharpFactory.BracketedArgumentList(invocationInfo.Arguments[0]));
SymbolInfo symbolInfo = semanticModel.GetSpeculativeSymbolInfo(invocationExpression.SpanStart, elementAccess, SpeculativeBindingOption.BindAsExpression);
return symbolInfo.Symbol is not IPropertySymbol propertySymbol
|| !propertySymbol.IsIndexer
|| CheckInfiniteRecursion(propertySymbol, invocationExpression.SpanStart, semanticModel, cancellationToken);
}
public static bool IsFixableFirst(
in SimpleMemberInvocationExpressionInfo invocationInfo,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (invocationInfo.InvocationExpression.IsParentKind(SyntaxKind.ExpressionStatement))
return false;
IMethodSymbol methodSymbol = semanticModel.GetReducedExtensionMethodInfo(invocationInfo.InvocationExpression, cancellationToken).Symbol;
if (methodSymbol is null)
return false;
if (!IsLinqExtensionOfIEnumerableOfTWithoutParameters(methodSymbol, "First", allowImmutableArrayExtension: true))
return false;
ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken);
return HasAccessibleIndexer(typeSymbol, semanticModel, invocationInfo.InvocationExpression.SpanStart);
}
public static bool IsFixableLast(
in SimpleMemberInvocationExpressionInfo invocationInfo,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (invocationInfo.InvocationExpression.IsParentKind(SyntaxKind.ExpressionStatement))
return false;
if (((CSharpCompilation)semanticModel.Compilation).LanguageVersion < LanguageVersion.CSharp8)
return false;
IMethodSymbol methodSymbol = semanticModel.GetReducedExtensionMethodInfo(invocationInfo.InvocationExpression, cancellationToken).Symbol;
if (methodSymbol is null)
return false;
if (!IsLinqExtensionOfIEnumerableOfTWithoutParameters(methodSymbol, "Last", allowImmutableArrayExtension: true))
return false;
ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken);
return HasAccessibleIndexer(typeSymbol, semanticModel, invocationInfo.InvocationExpression.SpanStart);
}
private static bool CheckInfiniteRecursion(
IPropertySymbol indexerSymbol,
int position,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
ISymbol symbol = semanticModel.GetEnclosingSymbol(position, cancellationToken);
if (symbol is not null)
{
IPropertySymbol propertySymbol = null;
if (symbol.Kind == SymbolKind.Property)
{
propertySymbol = (IPropertySymbol)symbol;
}
else if (symbol.Kind == SymbolKind.Method)
{
var methodSymbol = (IMethodSymbol)symbol;
if (methodSymbol.MethodKind.Is(MethodKind.PropertyGet, MethodKind.PropertySet))
propertySymbol = methodSymbol.AssociatedSymbol as IPropertySymbol;
}
if (SymbolEqualityComparer.Default.Equals(propertySymbol, indexerSymbol))
return false;
}
return true;
}
}