Skip to content

Commit 02ba9f2

Browse files
authored
Merge pull request #1460 from microsoft/dev/andarno/analyzers-immutablearray
Use `ImmutableArray` instead of `IReadOnlyList` in analyzers
2 parents 629a068 + 1c36682 commit 02ba9f2

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
@@ -27,39 +27,39 @@ public static class CommonInterest
2727
public static readonly Regex FileNamePatternForMethodsThatSwitchToMainThread = new Regex(@"^vs-threading\.MainThreadSwitchingMethods(\..*)?.txt$", FileNamePatternRegexOptions);
2828
public static readonly Regex FileNamePatternForSyncMethodsToExcludeFromVSTHRD103 = new Regex(@"^vs-threading\.SyncMethodsToExcludeFromVSTHRD103(\..*)?.txt$", FileNamePatternRegexOptions);
2929

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

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

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

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

59-
public static readonly IEnumerable<QualifiedMember> ThreadAffinityTestingMethods = new[]
60-
{
59+
public static readonly IEnumerable<QualifiedMember> ThreadAffinityTestingMethods =
60+
[
6161
new QualifiedMember(new QualifiedType(Namespaces.MicrosoftVisualStudioShell, Types.ThreadHelper.TypeName), Types.ThreadHelper.CheckAccess),
62-
};
62+
];
6363

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

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

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

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

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

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

476-
public IReadOnlyList<string> Namespace { get; }
476+
public ImmutableArray<string> Namespace { get; }
477477

478478
public string Name { get; }
479479

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

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

489489
public readonly struct QualifiedMember
@@ -510,7 +510,7 @@ public bool IsMatch(ISymbol? symbol)
510510
[DebuggerDisplay("{" + nameof(Method) + "} -> {" + nameof(AsyncAlternativeMethodName) + "}")]
511511
public readonly struct SyncBlockingMethod
512512
{
513-
public SyncBlockingMethod(QualifiedMember method, string? asyncAlternativeMethodName = null, IReadOnlyList<string>? extensionMethodNamespace = null)
513+
public SyncBlockingMethod(QualifiedMember method, string? asyncAlternativeMethodName = null, ImmutableArray<string>? extensionMethodNamespace = null)
514514
{
515515
this.Method = method;
516516
this.AsyncAlternativeMethodName = asyncAlternativeMethodName;
@@ -521,7 +521,7 @@ public SyncBlockingMethod(QualifiedMember method, string? asyncAlternativeMethod
521521

522522
public string? AsyncAlternativeMethodName { get; }
523523

524-
public IReadOnlyList<string>? ExtensionMethodNamespace { get; }
524+
public ImmutableArray<string>? ExtensionMethodNamespace { get; }
525525
}
526526

527527
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)