Skip to content

Commit 1fa8625

Browse files
Jim8yshargon
andauthored
Analyze unsupported ArraySegment usage (#1916)
Co-authored-by: Shargon <shargon@gmail.com>
1 parent e8f757a commit 1fa8625

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/Neo.SmartContract.Analyzer/CollectionTypesUsageAnalyzer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public class CollectionTypesUsageAnalyzer : DiagnosticAnalyzer
6060
"System.Collections.BitArray",
6161
"System.Collections.ObjectModel.Collection<T>",
6262
"System.Collections.ObjectModel.ReadOnlyCollection<T>",
63-
"System.Collections.ObjectModel.ReadOnlyObservableCollection<T>"
63+
"System.Collections.ObjectModel.ReadOnlyObservableCollection<T>",
64+
"System.ArraySegment<T>"
6465
};
6566

6667
private static readonly DiagnosticDescriptor Rule = new(
@@ -192,6 +193,9 @@ private void ReportIfUnsupportedCollectionType(SyntaxNodeAnalysisContext context
192193

193194
private static string GetSuggestedType(string originalType)
194195
{
196+
if (originalType == "System.ArraySegment<T>")
197+
return "an array or Span<T>";
198+
195199
return originalType.Contains("Dictionary") ? "Map<TKey, TValue>" : "List<T>";
196200
}
197201
}

tests/Neo.SmartContract.Analyzer.UnitTests/CollectionTypesUsageAnalyzerUnitTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,49 @@ class TestClass
179179
await VerifyCS.VerifyAnalyzerAsync(test);
180180
}
181181

182+
[TestMethod]
183+
public async Task ArraySegmentUsage_ShouldReportDiagnostic()
184+
{
185+
var test = TestNamespace + """
186+
187+
class TestClass
188+
{
189+
public void Run(byte[] buffer)
190+
{
191+
System.ArraySegment<byte> segment = new(buffer, 1, 2);
192+
}
193+
}
194+
""";
195+
196+
var expected = VerifyCS.Diagnostic(CollectionTypesUsageAnalyzer.DiagnosticId)
197+
.WithLocation(18, 9)
198+
.WithArguments("System.ArraySegment<T>", "an array or Span<T>");
199+
200+
await VerifyCS.VerifyAnalyzerAsync(test, expected);
201+
}
202+
203+
[TestMethod]
204+
public async Task NestedArraySegmentSignature_ShouldReportDiagnostics()
205+
{
206+
var test = TestNamespace + """
207+
208+
class TestClass
209+
{
210+
public {|#0:System.Tuple<int, System.ArraySegment<byte>>|} Read(
211+
{|#1:System.ArraySegment<byte>[]|} segments) => null;
212+
}
213+
""";
214+
215+
var returnDiagnostic = VerifyCS.Diagnostic(CollectionTypesUsageAnalyzer.DiagnosticId)
216+
.WithLocation(0)
217+
.WithArguments("System.ArraySegment<T>", "an array or Span<T>");
218+
var parameterDiagnostic = VerifyCS.Diagnostic(CollectionTypesUsageAnalyzer.DiagnosticId)
219+
.WithLocation(1)
220+
.WithArguments("System.ArraySegment<T>", "an array or Span<T>");
221+
222+
await VerifyCS.VerifyAnalyzerAsync(test, returnDiagnostic, parameterDiagnostic);
223+
}
224+
182225
[TestMethod]
183226
public void CollectionDiagnostic_ShouldNotOfferAutomaticCodeFixes()
184227
{

0 commit comments

Comments
 (0)