Skip to content

Commit ad96ee4

Browse files
Merge pull request #59523 from Youssef1313/ide0056-ide0057-cleanup
IDE0056, IDE0057: Move language version check to compilation start, minor cleanup
2 parents 5193908 + 0636da3 commit ad96ee4

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/Analyzers/CSharp/Analyzers/UseIndexOrRangeOperator/CSharpUseIndexOperatorDiagnosticAnalyzer.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace Microsoft.CodeAnalysis.CSharp.UseIndexOrRangeOperator
4040
/// </summary>
4141
[DiagnosticAnalyzer(LanguageNames.CSharp)]
4242
[SuppressMessage("Documentation", "CA1200:Avoid using cref tags with a prefix", Justification = "Required to avoid ambiguous reference warnings.")]
43-
internal partial class CSharpUseIndexOperatorDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
43+
internal sealed partial class CSharpUseIndexOperatorDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
4444
{
4545
public CSharpUseIndexOperatorDiagnosticAnalyzer()
4646
: base(IDEDiagnosticIds.UseIndexOperatorDiagnosticId,
@@ -56,12 +56,17 @@ public CSharpUseIndexOperatorDiagnosticAnalyzer()
5656

5757
protected override void InitializeWorker(AnalysisContext context)
5858
{
59-
context.RegisterCompilationStartAction(startContext =>
59+
context.RegisterCompilationStartAction(context =>
6060
{
61+
var compilation = (CSharpCompilation)context.Compilation;
62+
63+
// Only supported on C# 8 and above.
64+
if (compilation.LanguageVersion < LanguageVersion.CSharp8)
65+
return;
66+
6167
// We're going to be checking every property-reference and invocation in the
6268
// compilation. Cache information we compute in this object so we don't have to
6369
// continually recompute it.
64-
var compilation = startContext.Compilation;
6570
if (!InfoCache.TryCreate(compilation, out var infoCache))
6671
return;
6772

@@ -171,14 +176,8 @@ private void AnalyzeInvokedMember(
171176
if (subtraction.Syntax is not BinaryExpressionSyntax binaryExpression)
172177
return;
173178

174-
// Only supported on C# 8 and above.
175-
var syntaxTree = binaryExpression.SyntaxTree;
176-
var parseOptions = (CSharpParseOptions)syntaxTree.Options;
177-
if (parseOptions.LanguageVersion < LanguageVersion.CSharp8)
178-
return;
179-
180179
// Don't bother analyzing if the user doesn't like using Index/Range operators.
181-
var option = context.Options.GetOption(CSharpCodeStyleOptions.PreferIndexOperator, syntaxTree, cancellationToken);
180+
var option = context.Options.GetOption(CSharpCodeStyleOptions.PreferIndexOperator, binaryExpression.SyntaxTree, cancellationToken);
182181
if (!option.Value)
183182
return;
184183

src/Analyzers/CSharp/Analyzers/UseIndexOrRangeOperator/CSharpUseRangeOperatorDiagnosticAnalyzer.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Microsoft.CodeAnalysis.CSharp.UseIndexOrRangeOperator
3535
/// </summary>
3636
[DiagnosticAnalyzer(LanguageNames.CSharp)]
3737
[SuppressMessage("Documentation", "CA1200:Avoid using cref tags with a prefix", Justification = "Required to avoid ambiguous reference warnings.")]
38-
internal partial class CSharpUseRangeOperatorDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
38+
internal sealed partial class CSharpUseRangeOperatorDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
3939
{
4040
// public const string UseIndexer = nameof(UseIndexer);
4141
public const string ComputedRange = nameof(ComputedRange);
@@ -55,14 +55,20 @@ public CSharpUseRangeOperatorDiagnosticAnalyzer()
5555

5656
protected override void InitializeWorker(AnalysisContext context)
5757
{
58-
context.RegisterCompilationStartAction(compilationContext =>
58+
context.RegisterCompilationStartAction(context =>
5959
{
60+
var compilation = (CSharpCompilation)context.Compilation;
61+
62+
// Check if we're at least on C# 8
63+
if (compilation.LanguageVersion < LanguageVersion.CSharp8)
64+
return;
65+
6066
// We're going to be checking every invocation in the compilation. Cache information
6167
// we compute in this object so we don't have to continually recompute it.
62-
if (!InfoCache.TryCreate(compilationContext.Compilation, out var infoCache))
68+
if (!InfoCache.TryCreate(context.Compilation, out var infoCache))
6369
return;
6470

65-
compilationContext.RegisterOperationAction(
71+
context.RegisterOperationAction(
6672
c => AnalyzeInvocation(c, infoCache),
6773
OperationKind.Invocation);
6874
});
@@ -74,6 +80,8 @@ private void AnalyzeInvocation(
7480
var operation = context.Operation;
7581
var syntaxTree = operation.SemanticModel!.SyntaxTree;
7682
var cancellationToken = context.CancellationToken;
83+
84+
// Check if the user wants these operators.
7785
var option = context.Options.GetOption(CSharpCodeStyleOptions.PreferRangeOperator, syntaxTree, cancellationToken);
7886
if (!option.Value)
7987
return;
@@ -98,12 +106,6 @@ private void AnalyzeInvocation(
98106
return null;
99107
}
100108

101-
// Check if we're at least on C# 8, and that the user wants these operators.
102-
var syntaxTree = invocationSyntax.SyntaxTree;
103-
var parseOptions = (CSharpParseOptions)syntaxTree.Options;
104-
if (parseOptions.LanguageVersion < LanguageVersion.CSharp8)
105-
return null;
106-
107109
// look for `s.Slice(e1, end - e2)` or `s.Slice(e1)`
108110
if (invocation.Instance is null)
109111
return null;

0 commit comments

Comments
 (0)