Skip to content

Commit c462cc5

Browse files
committed
Simplify tuple support checks
1 parent bb6fbc9 commit c462cc5

File tree

2 files changed

+20
-53
lines changed

2 files changed

+20
-53
lines changed

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/LanguageFeatureHelpers.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace StyleCop.Analyzers.Helpers
77
{
8+
using System.Diagnostics.CodeAnalysis;
89
using Microsoft.CodeAnalysis.CSharp;
910
using Microsoft.CodeAnalysis.Diagnostics;
1011
using StyleCop.Analyzers.Lightup;
@@ -14,6 +15,17 @@ namespace StyleCop.Analyzers.Helpers
1415
/// </summary>
1516
internal static class LanguageFeatureHelpers
1617
{
18+
/// <summary>
19+
/// Checks if the tuple language feature is supported.
20+
/// </summary>
21+
/// <param name="context">The analysis context that will be checked.</param>
22+
/// <returns>True if tuples are supported by the compiler.</returns>
23+
[SuppressMessage("MicrosoftCodeAnalysisPerformance", "RS1012:Start action has no registered actions", Justification = "This is not a start action method.")]
24+
internal static bool SupportsTuples(this CompilationStartAnalysisContext context)
25+
{
26+
return context.Compilation is CSharpCompilation { LanguageVersion: >= LanguageVersionEx.CSharp7 };
27+
}
28+
1729
/// <summary>
1830
/// Checks if the tuple language feature is supported.
1931
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1141UseTupleSyntax.cs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public override void Initialize(AnalysisContext context)
5050
context.EnableConcurrentExecution();
5151

5252
context.RegisterCompilationStartAction(CompilationStartAction);
53+
}
54+
55+
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
56+
{
57+
if (!context.SupportsTuples())
58+
{
59+
return;
60+
}
5361

5462
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
5563
context.RegisterSyntaxNodeAction(ConversionOperatorAction, SyntaxKind.ConversionOperatorDeclaration);
@@ -59,10 +67,7 @@ public override void Initialize(AnalysisContext context)
5967

6068
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
6169
context.RegisterSyntaxNodeAction(LambdaExpressionAction, SyntaxKinds.LambdaExpression);
62-
}
6370

64-
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
65-
{
6671
var expressionType = context.Compilation.GetTypeByMetadataName("System.Linq.Expressions.Expression`1");
6772

6873
context.RegisterSyntaxNodeAction(context => HandleObjectCreationExpression(context, expressionType), SyntaxKind.ObjectCreationExpression);
@@ -75,11 +80,6 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
7580

7681
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
7782
{
78-
if (!context.SupportsTuples())
79-
{
80-
return;
81-
}
82-
8383
var methodDeclaration = (MethodDeclarationSyntax)context.Node;
8484

8585
CheckType(context, expressionType: null, methodDeclaration.ReturnType);
@@ -88,11 +88,6 @@ private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
8888

8989
private static void HandleConversionOperator(SyntaxNodeAnalysisContext context)
9090
{
91-
if (!context.SupportsTuples())
92-
{
93-
return;
94-
}
95-
9691
var conversionOperatorDeclaration = (ConversionOperatorDeclarationSyntax)context.Node;
9792

9893
CheckType(context, expressionType: null, conversionOperatorDeclaration.Type);
@@ -101,33 +96,18 @@ private static void HandleConversionOperator(SyntaxNodeAnalysisContext context)
10196

10297
private static void HandleBasePropertyDeclaration(SyntaxNodeAnalysisContext context)
10398
{
104-
if (!context.SupportsTuples())
105-
{
106-
return;
107-
}
108-
10999
var propertyDeclaration = (BasePropertyDeclarationSyntax)context.Node;
110100
CheckType(context, expressionType: null, propertyDeclaration.Type);
111101
}
112102

113103
private static void HandleFieldDeclaration(SyntaxNodeAnalysisContext context)
114104
{
115-
if (!context.SupportsTuples())
116-
{
117-
return;
118-
}
119-
120105
var fieldDeclaration = (BaseFieldDeclarationSyntax)context.Node;
121106
CheckType(context, expressionType: null, fieldDeclaration.Declaration.Type);
122107
}
123108

124109
private static void HandleLambdaExpression(SyntaxNodeAnalysisContext context)
125110
{
126-
if (!context.SupportsTuples())
127-
{
128-
return;
129-
}
130-
131111
var lambdaExpression = (LambdaExpressionSyntax)context.Node;
132112
if (lambdaExpression is ParenthesizedLambdaExpressionSyntax parenthesizedLambdaExpression)
133113
{
@@ -137,22 +117,12 @@ private static void HandleLambdaExpression(SyntaxNodeAnalysisContext context)
137117

138118
private static void HandleObjectCreationExpression(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionType)
139119
{
140-
if (!context.SupportsTuples())
141-
{
142-
return;
143-
}
144-
145120
var objectCreationExpression = (ObjectCreationExpressionSyntax)context.Node;
146121
CheckType(context, expressionType, objectCreationExpression.Type, objectCreationExpression.GetLocation());
147122
}
148123

149124
private static void HandleInvocationExpression(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionType)
150125
{
151-
if (!context.SupportsTuples())
152-
{
153-
return;
154-
}
155-
156126
var invocationExpression = (InvocationExpressionSyntax)context.Node;
157127
if (invocationExpression.ArgumentList.Arguments.Count < 2)
158128
{
@@ -181,34 +151,19 @@ private static void HandleInvocationExpression(SyntaxNodeAnalysisContext context
181151

182152
private static void HandleDefaultExpression(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionType)
183153
{
184-
if (!context.SupportsTuples())
185-
{
186-
return;
187-
}
188-
189154
var defaultExpression = (DefaultExpressionSyntax)context.Node;
190155
CheckType(context, expressionType, defaultExpression.Type);
191156
}
192157

193158
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context)
194159
{
195-
if (!context.SupportsTuples())
196-
{
197-
return;
198-
}
199-
200160
var delegateDeclaration = (DelegateDeclarationSyntax)context.Node;
201161
CheckType(context, expressionType: null, delegateDeclaration.ReturnType);
202162
CheckParameterList(context, delegateDeclaration.ParameterList);
203163
}
204164

205165
private static void HandleCastExpression(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionType)
206166
{
207-
if (!context.SupportsTuples())
208-
{
209-
return;
210-
}
211-
212167
var castExpression = (CastExpressionSyntax)context.Node;
213168
CheckType(context, expressionType, castExpression.Type);
214169
}

0 commit comments

Comments
 (0)