Skip to content

Commit 19e1393

Browse files
authored
Merge pull request #6639 from sharwell/pooled-collections
Allocation optimizations from feedback ticket
2 parents 7a06ea9 + c99db05 commit 19e1393

23 files changed

+442
-149
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ indent_style = space
1111
# Code files
1212
[*.{cs,csx,vb,vbx}]
1313
indent_size = 4
14+
charset = utf-8-bom
1415

1516
# Xml project files
1617
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/IdentifiersShouldHaveCorrectPrefix.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

33
using System;
44
using System.Collections.Immutable;
5-
using Microsoft.CodeAnalysis.Diagnostics;
6-
using Microsoft.CodeAnalysis;
5+
using System.Diagnostics;
76
using Analyzer.Utilities;
87
using Analyzer.Utilities.Extensions;
9-
using System.Diagnostics;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.Diagnostics;
1010

1111
namespace Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines
1212
{
@@ -72,34 +72,42 @@ public override void Initialize(AnalysisContext context)
7272
switch (context.Symbol.Kind)
7373
{
7474
case SymbolKind.NamedType:
75-
AnalyzeNamedTypeSymbol((INamedTypeSymbol)context.Symbol, allowSingleLetterTypeParameters, context.ReportDiagnostic);
75+
AnalyzeNamedTypeSymbol(
76+
(INamedTypeSymbol)context.Symbol,
77+
allowSingleLetterTypeParameters,
78+
static (context, diagnostic) => context.ReportDiagnostic(diagnostic),
79+
context);
7680
break;
7781

7882
case SymbolKind.Method:
79-
AnalyzeMethodSymbol((IMethodSymbol)context.Symbol, allowSingleLetterTypeParameters, context.ReportDiagnostic);
83+
AnalyzeMethodSymbol(
84+
(IMethodSymbol)context.Symbol,
85+
allowSingleLetterTypeParameters,
86+
static (context, diagnostic) => context.ReportDiagnostic(diagnostic),
87+
context);
8088
break;
8189
}
8290
},
8391
SymbolKind.Method,
8492
SymbolKind.NamedType);
8593
}
8694

87-
private static void AnalyzeNamedTypeSymbol(INamedTypeSymbol symbol, bool allowSingleLetterTypeParameters, Action<Diagnostic> addDiagnostic)
95+
private static void AnalyzeNamedTypeSymbol<TContext>(INamedTypeSymbol symbol, bool allowSingleLetterTypeParameters, Action<TContext, Diagnostic> addDiagnostic, TContext context)
8896
{
89-
AnalyzeTypeParameters(symbol.TypeParameters, allowSingleLetterTypeParameters, addDiagnostic);
97+
AnalyzeTypeParameters(symbol.TypeParameters, allowSingleLetterTypeParameters, addDiagnostic, context);
9098

9199
if (symbol.TypeKind == TypeKind.Interface &&
92100
symbol.IsPublic() &&
93101
!HasCorrectPrefix(symbol, 'I'))
94102
{
95-
addDiagnostic(symbol.CreateDiagnostic(InterfaceRule, symbol.Name));
103+
addDiagnostic(context, symbol.CreateDiagnostic(InterfaceRule, symbol.Name));
96104
}
97105
}
98106

99-
private static void AnalyzeMethodSymbol(IMethodSymbol symbol, bool allowSingleLetterTypeParameters, Action<Diagnostic> addDiagnostic)
100-
=> AnalyzeTypeParameters(symbol.TypeParameters, allowSingleLetterTypeParameters, addDiagnostic);
107+
private static void AnalyzeMethodSymbol<TContext>(IMethodSymbol symbol, bool allowSingleLetterTypeParameters, Action<TContext, Diagnostic> addDiagnostic, TContext context)
108+
=> AnalyzeTypeParameters(symbol.TypeParameters, allowSingleLetterTypeParameters, addDiagnostic, context);
101109

102-
private static void AnalyzeTypeParameters(ImmutableArray<ITypeParameterSymbol> typeParameters, bool allowSingleLetterTypeParameters, Action<Diagnostic> addDiagnostic)
110+
private static void AnalyzeTypeParameters<TContext>(ImmutableArray<ITypeParameterSymbol> typeParameters, bool allowSingleLetterTypeParameters, Action<TContext, Diagnostic> addDiagnostic, TContext context)
103111
{
104112
foreach (var typeParameter in typeParameters)
105113
{
@@ -111,7 +119,7 @@ private static void AnalyzeTypeParameters(ImmutableArray<ITypeParameterSymbol> t
111119
continue;
112120
}
113121

114-
addDiagnostic(typeParameter.CreateDiagnostic(TypeParameterRule, typeParameter.Name));
122+
addDiagnostic(context, typeParameter.CreateDiagnostic(TypeParameterRule, typeParameter.Name));
115123
}
116124
}
117125
}

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/MarkAttributesWithAttributeUsage.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

33
using System;
44
using System.Collections.Immutable;
@@ -47,13 +47,18 @@ public override void Initialize(AnalysisContext context)
4747

4848
compilationContext.RegisterSymbolAction(context =>
4949
{
50-
AnalyzeSymbol((INamedTypeSymbol)context.Symbol, attributeType, attributeUsageAttributeType, context.ReportDiagnostic);
50+
AnalyzeSymbol(
51+
(INamedTypeSymbol)context.Symbol,
52+
attributeType,
53+
attributeUsageAttributeType,
54+
static (context, diagnostic) => context.ReportDiagnostic(diagnostic),
55+
context);
5156
},
5257
SymbolKind.NamedType);
5358
});
5459
}
5560

56-
private static void AnalyzeSymbol(INamedTypeSymbol symbol, INamedTypeSymbol attributeType, INamedTypeSymbol attributeUsageAttributeType, Action<Diagnostic> addDiagnostic)
61+
private static void AnalyzeSymbol<TContext>(INamedTypeSymbol symbol, INamedTypeSymbol attributeType, INamedTypeSymbol attributeUsageAttributeType, Action<TContext, Diagnostic> addDiagnostic, TContext context)
5762
{
5863
if (symbol.IsAbstract || symbol.BaseType == null || !symbol.BaseType.Equals(attributeType))
5964
{
@@ -62,7 +67,7 @@ private static void AnalyzeSymbol(INamedTypeSymbol symbol, INamedTypeSymbol attr
6267

6368
if (!symbol.HasAttribute(attributeUsageAttributeType))
6469
{
65-
addDiagnostic(symbol.CreateDiagnostic(Rule, symbol.Name));
70+
addDiagnostic(context, symbol.CreateDiagnostic(Rule, symbol.Name));
6671
}
6772
}
6873
}

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/MovePInvokesToNativeMethodsClass.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

33
using System;
44
using System.Collections.Immutable;
@@ -43,15 +43,18 @@ public override void Initialize(AnalysisContext context)
4343

4444
context.RegisterSymbolAction(symbolContext =>
4545
{
46-
AnalyzeSymbol((INamedTypeSymbol)symbolContext.Symbol, symbolContext.ReportDiagnostic);
46+
AnalyzeSymbol(
47+
(INamedTypeSymbol)symbolContext.Symbol,
48+
static (context, diagnostic) => context.ReportDiagnostic(diagnostic),
49+
symbolContext);
4750
}, SymbolKind.NamedType);
4851
}
4952

50-
private static void AnalyzeSymbol(INamedTypeSymbol symbol, Action<Diagnostic> addDiagnostic)
53+
private static void AnalyzeSymbol<TContext>(INamedTypeSymbol symbol, Action<TContext, Diagnostic> addDiagnostic, TContext context)
5154
{
5255
if (symbol.GetMembers().Any(IsDllImport) && !IsTypeNamedCorrectly(symbol.Name))
5356
{
54-
addDiagnostic(symbol.CreateDiagnostic(Rule));
57+
addDiagnostic(context, symbol.CreateDiagnostic(Rule));
5558
}
5659
}
5760

0 commit comments

Comments
 (0)