|
14 | 14 | using Microsoft.CodeAnalysis.Diagnostics; |
15 | 15 | using Microsoft.CodeAnalysis.Operations; |
16 | 16 | using System.Collections.Immutable; |
| 17 | +using System.Collections.Generic; |
17 | 18 | using System.Linq; |
18 | 19 | using Microsoft.CodeAnalysis.CSharp; |
19 | 20 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
@@ -132,10 +133,61 @@ private void ReportIfUnsupportedCollectionType(SyntaxNodeAnalysisContext context |
132 | 133 |
|
133 | 134 | private string? GetUnsupportedCollectionType(ITypeSymbol? type) |
134 | 135 | { |
| 136 | + return GetUnsupportedCollectionType( |
| 137 | + type, |
| 138 | + new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default)); |
| 139 | + } |
| 140 | + |
| 141 | + private string? GetUnsupportedCollectionType( |
| 142 | + ITypeSymbol? type, |
| 143 | + HashSet<ITypeSymbol> visitedTypes) |
| 144 | + { |
| 145 | + if (type is null || !visitedTypes.Add(type)) return null; |
| 146 | + |
| 147 | + if (type is IArrayTypeSymbol arrayType) |
| 148 | + return GetUnsupportedCollectionType(arrayType.ElementType, visitedTypes); |
| 149 | + |
| 150 | + if (type is IPointerTypeSymbol pointerType) |
| 151 | + return GetUnsupportedCollectionType(pointerType.PointedAtType, visitedTypes); |
| 152 | + |
| 153 | + if (type is IFunctionPointerTypeSymbol functionPointerType) |
| 154 | + { |
| 155 | + var returnType = GetUnsupportedCollectionType( |
| 156 | + functionPointerType.Signature.ReturnType, |
| 157 | + visitedTypes); |
| 158 | + if (returnType is not null) return returnType; |
| 159 | + |
| 160 | + foreach (var parameter in functionPointerType.Signature.Parameters) |
| 161 | + { |
| 162 | + var parameterType = GetUnsupportedCollectionType(parameter.Type, visitedTypes); |
| 163 | + if (parameterType is not null) return parameterType; |
| 164 | + } |
| 165 | + |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
135 | 169 | if (type is not INamedTypeSymbol namedType) return null; |
136 | 170 |
|
137 | | - var originalType = namedType.OriginalDefinition.ToString() ?? throw new ArgumentNullException("originalType is null"); |
138 | | - return _unsupportedCollectionTypes.Contains(originalType) ? originalType : null; |
| 171 | + var originalType = namedType.OriginalDefinition.ToString() ?? |
| 172 | + throw new ArgumentNullException(nameof(type)); |
| 173 | + if (_unsupportedCollectionTypes.Contains(originalType)) |
| 174 | + return originalType; |
| 175 | + |
| 176 | + if (namedType.ContainingType is not null) |
| 177 | + { |
| 178 | + var containingType = GetUnsupportedCollectionType( |
| 179 | + namedType.ContainingType, |
| 180 | + visitedTypes); |
| 181 | + if (containingType is not null) return containingType; |
| 182 | + } |
| 183 | + |
| 184 | + foreach (var typeArgument in namedType.TypeArguments) |
| 185 | + { |
| 186 | + var unsupportedType = GetUnsupportedCollectionType(typeArgument, visitedTypes); |
| 187 | + if (unsupportedType is not null) return unsupportedType; |
| 188 | + } |
| 189 | + |
| 190 | + return null; |
139 | 191 | } |
140 | 192 |
|
141 | 193 | private static string GetSuggestedType(string originalType) |
|
0 commit comments