Skip to content

Commit 1c36682

Browse files
committed
Use ImmutableArray instead of IReadOnlyList in analyzers
1 parent 4c0b6e1 commit 1c36682

File tree

4 files changed

+73
-73
lines changed

4 files changed

+73
-73
lines changed

src/Microsoft.VisualStudio.Threading.Analyzers/CommonInterest.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,39 @@ public static class CommonInterest
2626
public static readonly Regex FileNamePatternForMethodsThatAssertMainThread = new Regex(@"^vs-threading\.MainThreadAssertingMethods(\..*)?.txt$", FileNamePatternRegexOptions);
2727
public static readonly Regex FileNamePatternForMethodsThatSwitchToMainThread = new Regex(@"^vs-threading\.MainThreadSwitchingMethods(\..*)?.txt$", FileNamePatternRegexOptions);
2828

29-
public static readonly IEnumerable<SyncBlockingMethod> JTFSyncBlockers = new[]
30-
{
29+
public static readonly IEnumerable<SyncBlockingMethod> JTFSyncBlockers =
30+
[
3131
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.MicrosoftVisualStudioThreading, Types.JoinableTaskFactory.TypeName), Types.JoinableTaskFactory.Run), Types.JoinableTaskFactory.RunAsync),
3232
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.MicrosoftVisualStudioThreading, Types.JoinableTask.TypeName), Types.JoinableTask.Join), Types.JoinableTask.JoinAsync),
33-
};
33+
];
3434

35-
public static readonly IEnumerable<SyncBlockingMethod> ProblematicSyncBlockingMethods = new[]
36-
{
35+
public static readonly IEnumerable<SyncBlockingMethod> ProblematicSyncBlockingMethods =
36+
[
3737
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(Task)), nameof(Task.Wait)), null),
3838
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(Task)), nameof(Task.WaitAll)), null),
3939
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(Task)), nameof(Task.WaitAny)), null),
4040
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter)), nameof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult)), null),
4141
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(TaskAwaiter)), nameof(TaskAwaiter.GetResult)), null),
4242
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(ValueTaskAwaiter)), nameof(ValueTaskAwaiter.GetResult)), null),
4343
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter)), nameof(ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter.GetResult)), null),
44-
};
44+
];
4545

46-
public static readonly IEnumerable<SyncBlockingMethod> SyncBlockingMethods = JTFSyncBlockers.Concat(ProblematicSyncBlockingMethods).Concat(new[]
47-
{
46+
public static readonly IEnumerable<SyncBlockingMethod> SyncBlockingMethods = JTFSyncBlockers.Concat(ProblematicSyncBlockingMethods).Concat(
47+
[
4848
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.MicrosoftVisualStudioShellInterop, "IVsTask"), "Wait"), extensionMethodNamespace: Namespaces.MicrosoftVisualStudioShell),
4949
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.MicrosoftVisualStudioShellInterop, "IVsTask"), "GetResult"), extensionMethodNamespace: Namespaces.MicrosoftVisualStudioShell),
50-
});
50+
]);
5151

52-
public static readonly IReadOnlyList<SyncBlockingMethod> SyncBlockingProperties = new[]
53-
{
52+
public static readonly ImmutableArray<SyncBlockingMethod> SyncBlockingProperties =
53+
[
5454
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(Task)), nameof(Task<int>.Result)), null),
5555
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(ValueTask)), nameof(ValueTask<int>.Result)), null),
56-
};
56+
];
5757

58-
public static readonly IEnumerable<QualifiedMember> ThreadAffinityTestingMethods = new[]
59-
{
58+
public static readonly IEnumerable<QualifiedMember> ThreadAffinityTestingMethods =
59+
[
6060
new QualifiedMember(new QualifiedType(Namespaces.MicrosoftVisualStudioShell, Types.ThreadHelper.TypeName), Types.ThreadHelper.CheckAccess),
61-
};
61+
];
6262

6363
public static readonly ImmutableArray<QualifiedMember> TaskConfigureAwait = ImmutableArray.Create(
6464
new QualifiedMember(new QualifiedType(Types.Task.Namespace, Types.Task.TypeName), nameof(Task.ConfigureAwait)),
@@ -77,7 +77,7 @@ public static class CommonInterest
7777
/// <summary>
7878
/// An array with '.' as its only element.
7979
/// </summary>
80-
private static readonly char[] QualifiedIdentifierSeparators = new[] { '.' };
80+
private static readonly char[] QualifiedIdentifierSeparators = ['.'];
8181

8282
public static IEnumerable<QualifiedMember> ReadMethods(AnalyzerOptions analyzerOptions, Regex fileNamePattern, CancellationToken cancellationToken)
8383
{
@@ -421,7 +421,7 @@ public TypeMatchSpec(QualifiedType type, QualifiedMember member, bool inverted)
421421
public QualifiedMember Member { get; }
422422

423423
/// <summary>
424-
/// Gets a value indicating whether a member match is reuqired.
424+
/// Gets a value indicating whether a member match is required.
425425
/// </summary>
426426
public bool IsMember => this.Member.Name is object;
427427

@@ -433,7 +433,7 @@ public TypeMatchSpec(QualifiedType type, QualifiedMember member, bool inverted)
433433
/// <summary>
434434
/// Gets a value indicating whether this is an uninitialized (default) instance.
435435
/// </summary>
436-
public bool IsEmpty => this.Type.Namespace is null;
436+
public bool IsEmpty => this.Type.Name is null;
437437

438438
/// <summary>
439439
/// Tests whether a given symbol matches the description of a type (independent of its <see cref="InvertedLogic"/> property).
@@ -466,13 +466,13 @@ public bool IsMatch([NotNullWhen(true)] ITypeSymbol? typeSymbol, ISymbol? member
466466

467467
public readonly struct QualifiedType
468468
{
469-
public QualifiedType(IReadOnlyList<string> containingTypeNamespace, string typeName)
469+
public QualifiedType(ImmutableArray<string> containingTypeNamespace, string typeName)
470470
{
471471
this.Namespace = containingTypeNamespace;
472472
this.Name = typeName;
473473
}
474474

475-
public IReadOnlyList<string> Namespace { get; }
475+
public ImmutableArray<string> Namespace { get; }
476476

477477
public string Name { get; }
478478

@@ -482,7 +482,7 @@ public bool IsMatch(ISymbol symbol)
482482
&& symbol.BelongsToNamespace(this.Namespace);
483483
}
484484

485-
public override string ToString() => string.Join(".", this.Namespace.Concat(new[] { this.Name }));
485+
public override string ToString() => string.Join(".", this.Namespace.Concat([this.Name]));
486486
}
487487

488488
public readonly struct QualifiedMember
@@ -509,7 +509,7 @@ public bool IsMatch(ISymbol? symbol)
509509
[DebuggerDisplay("{" + nameof(Method) + "} -> {" + nameof(AsyncAlternativeMethodName) + "}")]
510510
public readonly struct SyncBlockingMethod
511511
{
512-
public SyncBlockingMethod(QualifiedMember method, string? asyncAlternativeMethodName = null, IReadOnlyList<string>? extensionMethodNamespace = null)
512+
public SyncBlockingMethod(QualifiedMember method, string? asyncAlternativeMethodName = null, ImmutableArray<string>? extensionMethodNamespace = null)
513513
{
514514
this.Method = method;
515515
this.AsyncAlternativeMethodName = asyncAlternativeMethodName;
@@ -520,7 +520,7 @@ public SyncBlockingMethod(QualifiedMember method, string? asyncAlternativeMethod
520520

521521
public string? AsyncAlternativeMethodName { get; }
522522

523-
public IReadOnlyList<string>? ExtensionMethodNamespace { get; }
523+
public ImmutableArray<string>? ExtensionMethodNamespace { get; }
524524
}
525525

526526
public class AwaitableTypeTester
Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,81 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Collections.Generic;
5-
64
namespace Microsoft.VisualStudio.Threading.Analyzers;
75

86
public static class Namespaces
97
{
10-
public static readonly IReadOnlyList<string> System = new[]
11-
{
8+
public static readonly ImmutableArray<string> System =
9+
[
1210
nameof(System),
13-
};
11+
];
1412

15-
public static readonly IReadOnlyList<string> SystemCollectionsGeneric = new[]
16-
{
13+
public static readonly ImmutableArray<string> SystemCollectionsGeneric =
14+
[
1715
nameof(System),
1816
nameof(global::System.Collections),
1917
nameof(global::System.Collections.Generic),
20-
};
18+
];
2119

22-
public static readonly IReadOnlyList<string> SystemThreading = new[]
23-
{
20+
public static readonly ImmutableArray<string> SystemThreading =
21+
[
2422
nameof(System),
2523
nameof(global::System.Threading),
26-
};
24+
];
2725

28-
public static readonly IReadOnlyList<string> SystemDiagnostics = new[]
29-
{
26+
public static readonly ImmutableArray<string> SystemDiagnostics =
27+
[
3028
nameof(System),
3129
nameof(global::System.Diagnostics),
32-
};
30+
];
3331

34-
public static readonly IReadOnlyList<string> SystemThreadingTasks = new[]
35-
{
32+
public static readonly ImmutableArray<string> SystemThreadingTasks =
33+
[
3634
nameof(System),
3735
nameof(global::System.Threading),
3836
nameof(global::System.Threading.Tasks),
39-
};
37+
];
4038

41-
public static readonly IReadOnlyList<string> SystemRuntimeCompilerServices = new[]
42-
{
39+
public static readonly ImmutableArray<string> SystemRuntimeCompilerServices =
40+
[
4341
nameof(System),
4442
nameof(global::System.Runtime),
4543
nameof(global::System.Runtime.CompilerServices),
46-
};
44+
];
4745

48-
public static readonly IReadOnlyList<string> SystemRuntimeInteropServices = new[]
49-
{
46+
public static readonly ImmutableArray<string> SystemRuntimeInteropServices =
47+
[
5048
nameof(System),
5149
nameof(global::System.Runtime),
5250
nameof(global::System.Runtime.InteropServices),
53-
};
51+
];
5452

55-
public static readonly IReadOnlyList<string> SystemWindowsThreading = new[]
56-
{
53+
public static readonly ImmutableArray<string> SystemWindowsThreading =
54+
[
5755
nameof(System),
5856
nameof(global::System.Windows),
5957
"Threading",
60-
};
58+
];
6159

62-
public static readonly IReadOnlyList<string> MicrosoftVisualStudioThreading = new[]
63-
{
60+
public static readonly ImmutableArray<string> MicrosoftVisualStudioThreading =
61+
[
6462
"Microsoft",
6563
"VisualStudio",
6664
"Threading",
67-
};
65+
];
6866

69-
public static readonly IReadOnlyList<string> MicrosoftVisualStudioShell = new[]
70-
{
67+
public static readonly ImmutableArray<string> MicrosoftVisualStudioShell =
68+
[
7169
"Microsoft",
7270
"VisualStudio",
7371
"Shell",
74-
};
72+
];
7573

76-
public static readonly IReadOnlyList<string> MicrosoftVisualStudioShellInterop = new[]
77-
{
74+
public static readonly ImmutableArray<string> MicrosoftVisualStudioShellInterop =
75+
[
7876
"Microsoft",
7977
"VisualStudio",
8078
"Shell",
8179
"Interop",
82-
};
80+
];
8381
}

src/Microsoft.VisualStudio.Threading.Analyzers/Types.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Collections.Generic;
5-
64
namespace Microsoft.VisualStudio.Threading.Analyzers;
75

86
/// <summary>
@@ -37,7 +35,7 @@ public static class AwaitExtensions
3735
/// </summary>
3836
public const string ConfigureAwaitRunInline = "ConfigureAwaitRunInline";
3937

40-
public static readonly IReadOnlyList<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
38+
public static readonly ImmutableArray<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
4139
}
4240

4341
/// <summary>
@@ -75,7 +73,7 @@ public static class TplExtensions
7573
/// </summary>
7674
public const string FalseTask = "FalseTask";
7775

78-
public static readonly IReadOnlyList<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
76+
public static readonly ImmutableArray<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
7977
}
8078

8179
/// <summary>
@@ -88,14 +86,14 @@ public static class AsyncEventHandler
8886
/// </summary>
8987
public const string TypeName = "AsyncEventHandler";
9088

91-
public static readonly IReadOnlyList<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
89+
public static readonly ImmutableArray<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
9290
}
9391

9492
public static class AsyncMethodBuilderAttribute
9593
{
9694
public const string TypeName = nameof(System.Runtime.CompilerServices.AsyncMethodBuilderAttribute);
9795

98-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemRuntimeCompilerServices;
96+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemRuntimeCompilerServices;
9997
}
10098

10199
/// <summary>
@@ -116,7 +114,7 @@ public static class JoinableTaskFactory
116114

117115
public const string RunAsync = "RunAsync";
118116

119-
public static readonly IReadOnlyList<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
117+
public static readonly ImmutableArray<string> Namespace = Namespaces.MicrosoftVisualStudioThreading;
120118
}
121119

122120
/// <summary>
@@ -191,7 +189,7 @@ public static class Task
191189

192190
public const string WhenAll = "WhenAll";
193191

194-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemThreadingTasks;
192+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemThreadingTasks;
195193
}
196194

197195
public static class ConfiguredTaskAwaitable
@@ -200,7 +198,7 @@ public static class ConfiguredTaskAwaitable
200198

201199
public const string FullName = "System.Runtime.CompilerServices." + TypeName;
202200

203-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemRuntimeCompilerServices;
201+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemRuntimeCompilerServices;
204202
}
205203

206204
public static class ValueTask
@@ -209,7 +207,7 @@ public static class ValueTask
209207

210208
public const string FullName = "System.Threading.Tasks." + TypeName;
211209

212-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemThreadingTasks;
210+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemThreadingTasks;
213211
}
214212

215213
public static class ConfiguredValueTaskAwaitable
@@ -218,34 +216,34 @@ public static class ConfiguredValueTaskAwaitable
218216

219217
public const string FullName = "System.Runtime.CompilerServices." + TypeName;
220218

221-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemRuntimeCompilerServices;
219+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemRuntimeCompilerServices;
222220
}
223221

224222
public static class CoClassAttribute
225223
{
226224
public const string TypeName = nameof(System.Runtime.InteropServices.CoClassAttribute);
227225

228-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemRuntimeInteropServices;
226+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemRuntimeInteropServices;
229227
}
230228

231229
public static class ComImportAttribute
232230
{
233231
public const string TypeName = nameof(System.Runtime.InteropServices.ComImportAttribute);
234232

235-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemRuntimeInteropServices;
233+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemRuntimeInteropServices;
236234
}
237235

238236
public static class InterfaceTypeAttribute
239237
{
240238
public const string TypeName = nameof(System.Runtime.InteropServices.InterfaceTypeAttribute);
241239

242-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemRuntimeInteropServices;
240+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemRuntimeInteropServices;
243241
}
244242

245243
public static class TypeLibTypeAttribute
246244
{
247245
public const string TypeName = "TypeLibTypeAttribute";
248246

249-
public static readonly IReadOnlyList<string> Namespace = Namespaces.SystemRuntimeInteropServices;
247+
public static readonly ImmutableArray<string> Namespace = Namespaces.SystemRuntimeInteropServices;
250248
}
251249
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
global using System.Collections.Immutable;

0 commit comments

Comments
 (0)