Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce helper extensions to make it easier to check LangVersions. #58896

Merged
merged 4 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

#if CODE_STYLE
Expand Down Expand Up @@ -48,7 +45,7 @@ public static bool CanOfferUseBlockScoped(OptionSet optionSet, BaseNamespaceDecl
}

internal static bool CanOfferUseFileScoped(OptionSet optionSet, CompilationUnitSyntax root, BaseNamespaceDeclarationSyntax declaration, bool forAnalyzer)
=> CanOfferUseFileScoped(optionSet, root, declaration, forAnalyzer, ((CSharpParseOptions)root.SyntaxTree.Options).LanguageVersion);
=> CanOfferUseFileScoped(optionSet, root, declaration, forAnalyzer, root.SyntaxTree.Options.LanguageVersion());

internal static bool CanOfferUseFileScoped(
OptionSet optionSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public static (SyntaxKind nodeToGenerate, VariableDeclaratorSyntax declaratorToR
SemanticModel semanticModel,
out bool shouldRemoveNextStatement)
{
var parseOptions = (CSharpParseOptions)semanticModel.SyntaxTree.Options;
var analyzer = new Analyzer(supportsOrPatterns: parseOptions.LanguageVersion.IsCSharp9OrAbove());
var analyzer = new Analyzer(supportsOrPatterns: semanticModel.SyntaxTree.Options.LanguageVersion() >= LanguageVersion.CSharp9);
var nodeToGenerate = analyzer.AnalyzeSwitchStatement(node, out shouldRemoveNextStatement);

if (nodeToGenerate == SyntaxKind.SimpleAssignmentExpression &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.PooledObjects;
Expand All @@ -31,10 +32,8 @@ public ConvertSwitchStatementToExpressionDiagnosticAnalyzer()
protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp8)
{
if (context.Compilation.LanguageVersion() < LanguageVersion.CSharp8)
return;
}

context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.SwitchStatement);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)

// ?. is only available in C# 6.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.
if (((CSharpParseOptions)ifStatement.SyntaxTree.Options).LanguageVersion < LanguageVersion.CSharp6)
if (ifStatement.SyntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp6)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

Expand All @@ -30,10 +30,8 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterCompilationStartAction(context =>
{
if (MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(((CSharpCompilation)context.Compilation).LanguageVersion))
{
if (MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(context.Compilation.LanguageVersion()))
context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.LocalFunctionStatement);
}
});

private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -32,10 +33,8 @@ protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp9)
{
if (context.Compilation.LanguageVersion() < LanguageVersion.CSharp9)
return;
}

context.RegisterSyntaxNodeAction(AnalyzeDiscardDesignation, SyntaxKind.DiscardDesignation);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
Expand All @@ -24,7 +25,7 @@ protected override bool IsRecordDeclaration(SyntaxNode node)
=> node is RecordDeclarationSyntax;

protected override bool SupportsDiscard(SyntaxTree tree)
=> ((CSharpParseOptions)tree.Options).LanguageVersion >= LanguageVersion.CSharp7;
=> tree.Options.LanguageVersion() >= LanguageVersion.CSharp7;

protected override bool MethodHasHandlesClause(IMethodSymbol method)
=> false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.SimplifyPropertyPattern
{
Expand Down Expand Up @@ -43,8 +43,7 @@ protected override void InitializeWorker(AnalysisContext context)
// Dotted property patterns are only available in C# 10.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.

var compilation = compilationContext.Compilation;
if (((CSharpCompilation)compilation).LanguageVersion < LanguageVersion.CSharp10)
if (compilationContext.Compilation.LanguageVersion() < LanguageVersion.CSharp10)
return;

context.RegisterSyntaxNodeAction(AnalyzeSubpattern, SyntaxKind.Subpattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.UseAutoProperty;
Expand All @@ -18,10 +19,10 @@ internal class CSharpUseAutoPropertyAnalyzer : AbstractUseAutoPropertyAnalyzer<
PropertyDeclarationSyntax, FieldDeclarationSyntax, VariableDeclaratorSyntax, ExpressionSyntax>
{
protected override bool SupportsReadOnlyProperties(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp6;
=> compilation.LanguageVersion() >= LanguageVersion.CSharp6;

protected override bool SupportsPropertyInitializer(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp6;
=> compilation.LanguageVersion() >= LanguageVersion.CSharp6;

protected override bool CanExplicitInterfaceImplementationsBeFixed()
=> false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -23,7 +24,7 @@ internal class CSharpUseCollectionInitializerDiagnosticAnalyzer :
VariableDeclaratorSyntax>
{
protected override bool AreCollectionInitializersSupported(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp3;
=> compilation.LanguageVersion() >= LanguageVersion.CSharp3;

protected override ISyntaxFacts GetSyntaxFacts() => CSharpSyntaxFacts.Instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -23,7 +24,7 @@ protected override SyntaxKind GetAnalysisKind()

protected override bool IsSupported(SyntaxKind assignmentKind, ParseOptions options)
=> assignmentKind != SyntaxKind.CoalesceExpression ||
((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp8;
options.LanguageVersion() >= LanguageVersion.CSharp8;

protected override int TryGetIncrementOrDecrement(SyntaxKind opKind, object constantValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private bool TryConvertToExpressionBodyWorker(
return false;
}

var languageVersion = ((CSharpParseOptions)body.SyntaxTree.Options).LanguageVersion;
var languageVersion = body.SyntaxTree.Options.LanguageVersion();

return body.TryConvertToArrowExpressionBody(
declaration.Kind(), languageVersion, conversionPreference,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
var cancellationToken = context.CancellationToken;

// Not available prior to C# 9.
if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp9)
if (syntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp9)
return;

var optionSet = options.GetAnalyzerOptionSet(syntaxTree, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.UseIsNullCheck;
Expand Down Expand Up @@ -32,10 +33,8 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp7)
{
if (context.Compilation.LanguageVersion() < LanguageVersion.CSharp7)
return;
}

context.RegisterSyntaxNodeAction(n => AnalyzeSyntax(n), SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServices;
Expand All @@ -18,10 +19,10 @@ public CSharpUseIsNullCheckForReferenceEqualsDiagnosticAnalyzer()
}

protected override bool IsLanguageVersionSupported(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp7;
=> compilation.LanguageVersion() >= LanguageVersion.CSharp7;

protected override bool IsUnconstrainedGenericSupported(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp8;
=> compilation.LanguageVersion() >= LanguageVersion.CSharp8;

protected override ISyntaxFacts GetSyntaxFacts()
=> CSharpSyntaxFacts.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
Expand Down Expand Up @@ -31,10 +32,8 @@ protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp9)
{
if (context.Compilation.LanguageVersion() < LanguageVersion.CSharp9)
return;
}

context.RegisterOperationAction(c => AnalyzeIsTypeOperation(c), OperationKind.IsType);
context.RegisterOperationAction(c => AnalyzeNegatedPatternOperation(c), OperationKind.NegatedPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override void InitializeWorker(AnalysisContext context)

// Local functions are only available in C# 7.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.
if (((CSharpCompilation)compilation).LanguageVersion < LanguageVersion.CSharp7)
if (compilation.LanguageVersion() < LanguageVersion.CSharp7)
return;

var expressionType = compilation.GetTypeByMetadataName(typeof(Expression<>).FullName!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServices;
Expand All @@ -26,7 +27,7 @@ internal class CSharpUseNullPropagationDiagnosticAnalyzer :
ElementAccessExpressionSyntax>
{
protected override bool ShouldAnalyze(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp6;
=> compilation.LanguageVersion() >= LanguageVersion.CSharp6;

protected override ISyntaxFacts GetSyntaxFacts()
=> CSharpSyntaxFacts.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -27,7 +28,7 @@ protected override bool AreObjectInitializersSupported(Compilation compilation)
{
// object initializers are only available in C# 3.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.
return ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp3;
return compilation.LanguageVersion() >= LanguageVersion.CSharp3;
}

protected override ISyntaxFacts GetSyntaxFacts() => CSharpSyntaxFacts.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context)
return;

var syntaxTree = expression.SyntaxTree;
if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp9)
if (syntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp9)
return;

var cancellationToken = context.CancellationToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)

// "x is Type y" is only available in C# 7.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.
if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp7)
if (syntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp7)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)

// "x is Type y" is only available in C# 7.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.
if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp7)
if (syntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp7)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void InitializeWorker(AnalysisContext context)
{
// "x is not Type y" is only available in C# 9.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.
if (!((CSharpCompilation)context.Compilation).LanguageVersion.IsCSharp9OrAbove())
if (context.Compilation.LanguageVersion() < LanguageVersion.CSharp9)
return;

context.RegisterSyntaxNodeAction(n => SyntaxNodeAction(n), SyntaxKind.LogicalNotExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.UseThrowExpression;

Expand All @@ -19,9 +20,7 @@ public CSharpUseThrowExpressionDiagnosticAnalyzer()
}

protected override bool IsSupported(Compilation compilation)
{
return ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp7;
}
=> compilation.LanguageVersion() >= LanguageVersion.CSharp7;

protected override bool IsInExpressionTree(SemanticModel semanticModel, SyntaxNode node, INamedTypeSymbol? expressionTypeOpt, CancellationToken cancellationToken)
=> node.IsInExpressionTree(semanticModel, expressionTypeOpt, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void InitializeWorker(AnalysisContext context)
{
// Tuples are only available in C# 7 and above.
var compilation = context.Compilation;
if (((CSharpCompilation)compilation).LanguageVersion < LanguageVersion.CSharp7)
if (compilation.LanguageVersion() < LanguageVersion.CSharp7)
return;

context.RegisterSyntaxNodeAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected override SyntaxNode TryUpdateParentOfUpdatedNode(SyntaxNode parent, Sy
{
if (newNameNode.IsKind(SyntaxKind.DiscardDesignation)
&& parent.IsKind(SyntaxKind.DeclarationPattern, out DeclarationPatternSyntax declarationPattern)
&& ((CSharpParseOptions)parent.SyntaxTree.Options).LanguageVersion.IsCSharp9OrAbove())
&& parent.SyntaxTree.Options.LanguageVersion() >= LanguageVersion.CSharp9)
{
var trailingTrivia = declarationPattern.Type.GetTrailingTrivia()
.AddRange(newNameNode.GetLeadingTrivia())
Expand Down
Loading