diff --git a/src/Analyzers/CSharp/Analyzers/ConvertNamespace/ConvertNamespaceAnalysis.cs b/src/Analyzers/CSharp/Analyzers/ConvertNamespace/ConvertNamespaceAnalysis.cs index a605d1b98802a..7aa7dbbead3cd 100644 --- a/src/Analyzers/CSharp/Analyzers/ConvertNamespace/ConvertNamespaceAnalysis.cs +++ b/src/Analyzers/CSharp/Analyzers/ConvertNamespace/ConvertNamespaceAnalysis.cs @@ -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 @@ -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, diff --git a/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs b/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs index 8847203847c04..e4ef97b6340ee 100644 --- a/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.Analyzer.cs @@ -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 && diff --git a/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs index d593bf47c6d2e..c564bd3b73b80 100644 --- a/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs @@ -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; @@ -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); }); diff --git a/src/Analyzers/CSharp/Analyzers/InvokeDelegateWithConditionalAccess/InvokeDelegateWithConditionalAccessAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/InvokeDelegateWithConditionalAccess/InvokeDelegateWithConditionalAccessAnalyzer.cs index 2c8ccdf29994b..6a4ef5206002c 100644 --- a/src/Analyzers/CSharp/Analyzers/InvokeDelegateWithConditionalAccess/InvokeDelegateWithConditionalAccessAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/InvokeDelegateWithConditionalAccess/InvokeDelegateWithConditionalAccessAnalyzer.cs @@ -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; } diff --git a/src/Analyzers/CSharp/Analyzers/MakeLocalFunctionStatic/MakeLocalFunctionStaticDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/MakeLocalFunctionStatic/MakeLocalFunctionStaticDiagnosticAnalyzer.cs index 161b1405373c9..045a203d9e77e 100644 --- a/src/Analyzers/CSharp/Analyzers/MakeLocalFunctionStatic/MakeLocalFunctionStaticDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/MakeLocalFunctionStatic/MakeLocalFunctionStaticDiagnosticAnalyzer.cs @@ -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; @@ -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) diff --git a/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryDiscardDesignation/CSharpRemoveUnnecessaryDiscardDesignationDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryDiscardDesignation/CSharpRemoveUnnecessaryDiscardDesignationDiagnosticAnalyzer.cs index a95e482559fd8..3f0d137e55d28 100644 --- a/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryDiscardDesignation/CSharpRemoveUnnecessaryDiscardDesignationDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryDiscardDesignation/CSharpRemoveUnnecessaryDiscardDesignationDiagnosticAnalyzer.cs @@ -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; @@ -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); }); diff --git a/src/Analyzers/CSharp/Analyzers/RemoveUnusedParametersAndValues/CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/RemoveUnusedParametersAndValues/CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs index 18df6089dd27c..acca9e9d33600 100644 --- a/src/Analyzers/CSharp/Analyzers/RemoveUnusedParametersAndValues/CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/RemoveUnusedParametersAndValues/CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs @@ -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; @@ -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; diff --git a/src/Analyzers/CSharp/Analyzers/SimplifyPropertyPattern/CSharpSimplifyPropertyPatternDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/SimplifyPropertyPattern/CSharpSimplifyPropertyPatternDiagnosticAnalyzer.cs index 6378ce4725fb2..b8c23b4bae93e 100644 --- a/src/Analyzers/CSharp/Analyzers/SimplifyPropertyPattern/CSharpSimplifyPropertyPatternDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/SimplifyPropertyPattern/CSharpSimplifyPropertyPatternDiagnosticAnalyzer.cs @@ -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 { @@ -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); diff --git a/src/Analyzers/CSharp/Analyzers/UseAutoProperty/CSharpUseAutoPropertyAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseAutoProperty/CSharpUseAutoPropertyAnalyzer.cs index 57d96a81cdb84..94a273b10fe09 100644 --- a/src/Analyzers/CSharp/Analyzers/UseAutoProperty/CSharpUseAutoPropertyAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseAutoProperty/CSharpUseAutoPropertyAnalyzer.cs @@ -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; @@ -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; diff --git a/src/Analyzers/CSharp/Analyzers/UseCollectionInitializer/CSharpUseCollectionInitializerDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseCollectionInitializer/CSharpUseCollectionInitializerDiagnosticAnalyzer.cs index 7c799f9af62d9..afb9368568142 100644 --- a/src/Analyzers/CSharp/Analyzers/UseCollectionInitializer/CSharpUseCollectionInitializerDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseCollectionInitializer/CSharpUseCollectionInitializerDiagnosticAnalyzer.cs @@ -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; @@ -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; } diff --git a/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundAssignmentDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundAssignmentDiagnosticAnalyzer.cs index 5b33da057bf03..1b7576995a545 100644 --- a/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundAssignmentDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundAssignmentDiagnosticAnalyzer.cs @@ -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; @@ -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) { diff --git a/src/Analyzers/CSharp/Analyzers/UseExpressionBody/Helpers/UseExpressionBodyHelper`1.cs b/src/Analyzers/CSharp/Analyzers/UseExpressionBody/Helpers/UseExpressionBodyHelper`1.cs index d76971155b75b..b8f2049c95638 100644 --- a/src/Analyzers/CSharp/Analyzers/UseExpressionBody/Helpers/UseExpressionBodyHelper`1.cs +++ b/src/Analyzers/CSharp/Analyzers/UseExpressionBody/Helpers/UseExpressionBodyHelper`1.cs @@ -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, diff --git a/src/Analyzers/CSharp/Analyzers/UseImplicitObjectCreation/CSharpUseImplicitObjectCreationDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseImplicitObjectCreation/CSharpUseImplicitObjectCreationDiagnosticAnalyzer.cs index ada69073628dc..a57f154190838 100644 --- a/src/Analyzers/CSharp/Analyzers/UseImplicitObjectCreation/CSharpUseImplicitObjectCreationDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseImplicitObjectCreation/CSharpUseImplicitObjectCreationDiagnosticAnalyzer.cs @@ -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); diff --git a/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorDiagnosticAnalyzer.cs index 34fd781be78a6..18af25fb9d424 100644 --- a/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorDiagnosticAnalyzer.cs @@ -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; @@ -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); }); diff --git a/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsDiagnosticAnalyzer.cs index 44bd1fca2e535..0306693867510 100644 --- a/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsDiagnosticAnalyzer.cs @@ -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; @@ -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; diff --git a/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer.cs index 22e128c62353b..22b05951d2320 100644 --- a/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseIsNullCheck/CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer.cs @@ -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; @@ -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); diff --git a/src/Analyzers/CSharp/Analyzers/UseLocalFunction/CSharpUseLocalFunctionDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseLocalFunction/CSharpUseLocalFunctionDiagnosticAnalyzer.cs index 991eadaf65ebd..0a744ffc75192 100644 --- a/src/Analyzers/CSharp/Analyzers/UseLocalFunction/CSharpUseLocalFunctionDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseLocalFunction/CSharpUseLocalFunctionDiagnosticAnalyzer.cs @@ -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!); diff --git a/src/Analyzers/CSharp/Analyzers/UseNullPropagation/CSharpUseNullPropagationDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseNullPropagation/CSharpUseNullPropagationDiagnosticAnalyzer.cs index 596a97ddd10bb..d6a8edf851e20 100644 --- a/src/Analyzers/CSharp/Analyzers/UseNullPropagation/CSharpUseNullPropagationDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseNullPropagation/CSharpUseNullPropagationDiagnosticAnalyzer.cs @@ -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; @@ -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; diff --git a/src/Analyzers/CSharp/Analyzers/UseObjectInitializer/CSharpUseObjectInitializerDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseObjectInitializer/CSharpUseObjectInitializerDiagnosticAnalyzer.cs index 74fd38b60904b..f07931c0f49df 100644 --- a/src/Analyzers/CSharp/Analyzers/UseObjectInitializer/CSharpUseObjectInitializerDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseObjectInitializer/CSharpUseObjectInitializerDiagnosticAnalyzer.cs @@ -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; @@ -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; diff --git a/src/Analyzers/CSharp/Analyzers/UsePatternCombinators/CSharpUsePatternCombinatorsDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UsePatternCombinators/CSharpUsePatternCombinatorsDiagnosticAnalyzer.cs index c10714973ca04..ab35e699fba96 100644 --- a/src/Analyzers/CSharp/Analyzers/UsePatternCombinators/CSharpUsePatternCombinatorsDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UsePatternCombinators/CSharpUsePatternCombinatorsDiagnosticAnalyzer.cs @@ -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; diff --git a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs index 19d4f426e4295..882fabea89b1e 100644 --- a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs @@ -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; } diff --git a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs index b77e0d1955def..7a66739e9247c 100644 --- a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs @@ -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; } diff --git a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpUseNotPatternDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpUseNotPatternDiagnosticAnalyzer.cs index e9e1372f02e80..8573a3cc7dcf9 100644 --- a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpUseNotPatternDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpUseNotPatternDiagnosticAnalyzer.cs @@ -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); diff --git a/src/Analyzers/CSharp/Analyzers/UseThrowExpression/CSharpUseThrowExpressionDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseThrowExpression/CSharpUseThrowExpressionDiagnosticAnalyzer.cs index ccc8b56b7d758..83bf4a2a32768 100644 --- a/src/Analyzers/CSharp/Analyzers/UseThrowExpression/CSharpUseThrowExpressionDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseThrowExpression/CSharpUseThrowExpressionDiagnosticAnalyzer.cs @@ -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; @@ -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); diff --git a/src/Analyzers/CSharp/Analyzers/UseTupleSwap/CSharpUseTupleSwapDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseTupleSwap/CSharpUseTupleSwapDiagnosticAnalyzer.cs index b85b5bd837098..ab56c84b265c3 100644 --- a/src/Analyzers/CSharp/Analyzers/UseTupleSwap/CSharpUseTupleSwapDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseTupleSwap/CSharpUseTupleSwapDiagnosticAnalyzer.cs @@ -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( diff --git a/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs index 24f21e1674b39..a42d5202f4a8f 100644 --- a/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs @@ -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()) diff --git a/src/Analyzers/CSharp/CodeFixes/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsCodeFixProvider.cs index 81898868edee8..cd7d69f2f6ca7 100644 --- a/src/Analyzers/CSharp/CodeFixes/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/UseIsNullCheck/CSharpUseIsNullCheckForReferenceEqualsCodeFixProvider.cs @@ -46,8 +46,7 @@ private static SyntaxNode CreateIsNullCheck(ExpressionSyntax argument) private static SyntaxNode CreateIsNotNullCheck(ExpressionSyntax argument) { - var parseOptions = (CSharpParseOptions)argument.SyntaxTree.Options; - if (parseOptions.LanguageVersion.IsCSharp9OrAbove()) + if (argument.SyntaxTree.Options.LanguageVersion() >= LanguageVersion.CSharp9) { return IsPatternExpression( argument, diff --git a/src/Analyzers/CSharp/CodeFixes/UsePatternMatching/CSharpAsAndNullCheckCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/UsePatternMatching/CSharpAsAndNullCheckCodeFixProvider.cs index 0e83d3871d04d..8a18396dd7340 100644 --- a/src/Analyzers/CSharp/CodeFixes/UsePatternMatching/CSharpAsAndNullCheckCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/UsePatternMatching/CSharpAsAndNullCheckCodeFixProvider.cs @@ -53,7 +53,7 @@ protected override async Task FixAllAsync( using var _2 = PooledHashSet.GetInstance(out var statementParentScopes); var tree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); - var languageVersion = ((CSharpParseOptions)tree.Options).LanguageVersion; + var languageVersion = tree.Options.LanguageVersion(); foreach (var diagnostic in diagnostics) { diff --git a/src/Features/CSharp/Portable/AddDebuggerDisplay/CSharpAddDebuggerDisplayCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/AddDebuggerDisplay/CSharpAddDebuggerDisplayCodeRefactoringProvider.cs index 38875bc12b7ed..e7a01ce10b8f1 100644 --- a/src/Features/CSharp/Portable/AddDebuggerDisplay/CSharpAddDebuggerDisplayCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/AddDebuggerDisplay/CSharpAddDebuggerDisplayCodeRefactoringProvider.cs @@ -6,6 +6,7 @@ using System.Composition; using Microsoft.CodeAnalysis.AddDebuggerDisplay; using Microsoft.CodeAnalysis.CodeRefactorings; +using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Shared.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Host.Mef; @@ -27,6 +28,6 @@ public CSharpAddDebuggerDisplayCodeRefactoringProvider() protected override bool CanNameofAccessNonPublicMembersFromAttributeArgument => true; protected override bool SupportsConstantInterpolatedStrings(Document document) - => ((CSharpParseOptions)document.Project.ParseOptions!).LanguageVersion.HasConstantInterpolatedStrings(); + => document.Project.ParseOptions!.LanguageVersion().HasConstantInterpolatedStrings(); } } diff --git a/src/Features/CSharp/Portable/CodeRefactorings/ConvertLocalFunctionToMethod/CSharpConvertLocalFunctionToMethodCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/CodeRefactorings/ConvertLocalFunctionToMethod/CSharpConvertLocalFunctionToMethodCodeRefactoringProvider.cs index 1124b7323fc53..d0e16360066bf 100644 --- a/src/Features/CSharp/Portable/CodeRefactorings/ConvertLocalFunctionToMethod/CSharpConvertLocalFunctionToMethodCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/CodeRefactorings/ConvertLocalFunctionToMethod/CSharpConvertLocalFunctionToMethodCodeRefactoringProvider.cs @@ -248,7 +248,7 @@ private static async Task UpdateDocumentAsync( } private static bool SupportsNonTrailingNamedArguments(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp7_2; + => options.LanguageVersion() >= LanguageVersion.CSharp7_2; private static SyntaxNode GenerateArgument(IParameterSymbol p, string name, bool shouldUseNamedArguments = false) => s_generator.Argument(shouldUseNamedArguments ? name : null, p.RefKind, name.ToIdentifierName()); diff --git a/src/Features/CSharp/Portable/CodeRefactorings/UseRecursivePatterns/UseRecursivePatternsCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/CodeRefactorings/UseRecursivePatterns/UseRecursivePatternsCodeRefactoringProvider.cs index 19ef4ebe9d3eb..ee907050a65a0 100644 --- a/src/Features/CSharp/Portable/CodeRefactorings/UseRecursivePatterns/UseRecursivePatternsCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/CodeRefactorings/UseRecursivePatterns/UseRecursivePatternsCodeRefactoringProvider.cs @@ -57,7 +57,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte return; var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - if (((CSharpParseOptions)root.SyntaxTree.Options).LanguageVersion < LanguageVersion.CSharp9) + if (root.SyntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp9) return; var model = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/Features/CSharp/Portable/Completion/CompletionProviders/NamedParameterCompletionProvider.cs b/src/Features/CSharp/Portable/Completion/CompletionProviders/NamedParameterCompletionProvider.cs index 3c150e28420de..aff2db04346c4 100644 --- a/src/Features/CSharp/Portable/Completion/CompletionProviders/NamedParameterCompletionProvider.cs +++ b/src/Features/CSharp/Portable/Completion/CompletionProviders/NamedParameterCompletionProvider.cs @@ -100,7 +100,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context) // Consider refining this logic to mandate completion with an argument name, if preceded by an out-of-position name // See https://github.com/dotnet/roslyn/issues/20657 - var languageVersion = ((CSharpParseOptions)document.Project.ParseOptions!).LanguageVersion; + var languageVersion = document.Project.ParseOptions!.LanguageVersion(); if (languageVersion < LanguageVersion.CSharp7_2 && token.IsMandatoryNamedParameterPosition()) { context.IsExclusive = true; diff --git a/src/Features/CSharp/Portable/ConvertIfToSwitch/CSharpConvertIfToSwitchCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/ConvertIfToSwitch/CSharpConvertIfToSwitchCodeRefactoringProvider.cs index 3b9f09193ebc3..f28d11846f910 100644 --- a/src/Features/CSharp/Portable/ConvertIfToSwitch/CSharpConvertIfToSwitchCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/ConvertIfToSwitch/CSharpConvertIfToSwitchCodeRefactoringProvider.cs @@ -6,6 +6,7 @@ using System.Composition; using Microsoft.CodeAnalysis.CodeRefactorings; using Microsoft.CodeAnalysis.ConvertIfToSwitch; +using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Shared.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Host.Mef; @@ -30,11 +31,11 @@ public override string GetTitle(bool forSwitchExpression) public override Analyzer CreateAnalyzer(ISyntaxFacts syntaxFacts, ParseOptions options) { - var version = ((CSharpParseOptions)options).LanguageVersion; + var version = options.LanguageVersion(); var features = (version >= LanguageVersion.CSharp7 ? Feature.SourcePattern | Feature.IsTypePattern | Feature.CaseGuard : 0) | (version >= LanguageVersion.CSharp8 ? Feature.SwitchExpression : 0) | - (version.IsCSharp9OrAbove() ? Feature.RelationalPattern | Feature.OrPattern | Feature.AndPattern | Feature.TypePattern : 0); + (version >= LanguageVersion.CSharp9 ? Feature.RelationalPattern | Feature.OrPattern | Feature.AndPattern | Feature.TypePattern : 0); return new CSharpAnalyzer(syntaxFacts, features); } } diff --git a/src/Features/CSharp/Portable/ConvertToInterpolatedString/CSharpConvertConcatenationToInterpolatedStringRefactoringProvider.cs b/src/Features/CSharp/Portable/ConvertToInterpolatedString/CSharpConvertConcatenationToInterpolatedStringRefactoringProvider.cs index 5506f1908e093..50be3052cd778 100644 --- a/src/Features/CSharp/Portable/ConvertToInterpolatedString/CSharpConvertConcatenationToInterpolatedStringRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/ConvertToInterpolatedString/CSharpConvertConcatenationToInterpolatedStringRefactoringProvider.cs @@ -6,6 +6,7 @@ using System.Composition; using Microsoft.CodeAnalysis.CodeRefactorings; using Microsoft.CodeAnalysis.ConvertToInterpolatedString; +using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Shared.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Host.Mef; @@ -23,7 +24,7 @@ public CSharpConvertConcatenationToInterpolatedStringRefactoringProvider() } protected override bool SupportsConstantInterpolatedStrings(Document document) - => ((CSharpParseOptions)document.Project.ParseOptions!).LanguageVersion.HasConstantInterpolatedStrings(); + => document.Project.ParseOptions!.LanguageVersion().HasConstantInterpolatedStrings(); protected override string GetTextWithoutQuotes(string text, bool isVerbatim, bool isCharacterLiteral) => isVerbatim diff --git a/src/Features/CSharp/Portable/ExtractMethod/CSharpMethodExtractor.CSharpCodeGenerator.cs b/src/Features/CSharp/Portable/ExtractMethod/CSharpMethodExtractor.CSharpCodeGenerator.cs index e64342081ccbe..0d4390d3bb316 100644 --- a/src/Features/CSharp/Portable/ExtractMethod/CSharpMethodExtractor.CSharpCodeGenerator.cs +++ b/src/Features/CSharp/Portable/ExtractMethod/CSharpMethodExtractor.CSharpCodeGenerator.cs @@ -177,7 +177,7 @@ selectedNode is FieldDeclarationSyntax || } protected override bool ShouldLocalFunctionCaptureParameter(SyntaxNode node) - => ((CSharpParseOptions)node.SyntaxTree.Options).LanguageVersion < LanguageVersion.CSharp8; + => node.SyntaxTree.Options.LanguageVersion() < LanguageVersion.CSharp8; private static bool IsExpressionBodiedMember(SyntaxNode node) => node is MemberDeclarationSyntax member && member.GetExpressionBody() != null; @@ -230,7 +230,7 @@ private DeclarationModifiers CreateMethodModifiers() var isReadOnly = AnalyzerResult.ShouldBeReadOnly; // Static local functions are only supported in C# 8.0 and later - var languageVersion = ((CSharpParseOptions)SemanticDocument.SyntaxTree.Options).LanguageVersion; + var languageVersion = SemanticDocument.SyntaxTree.Options.LanguageVersion(); if (LocalFunction && (!Options.GetOption(CSharpCodeStyleOptions.PreferStaticLocalFunction).Value || languageVersion < LanguageVersion.CSharp8)) { diff --git a/src/Features/CSharp/Portable/MakeLocalFunctionStatic/MakeLocalFunctionStaticCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/MakeLocalFunctionStatic/MakeLocalFunctionStaticCodeRefactoringProvider.cs index 3a80dc9a83628..40b5e172323c3 100644 --- a/src/Features/CSharp/Portable/MakeLocalFunctionStatic/MakeLocalFunctionStaticCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/MakeLocalFunctionStatic/MakeLocalFunctionStaticCodeRefactoringProvider.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeRefactorings; +using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Microsoft.CodeAnalysis.CSharp.MakeLocalFunctionStatic @@ -27,7 +28,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte var (document, textSpan, cancellationToken) = context; var syntaxTree = (await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false))!; - if (!MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(((CSharpParseOptions)syntaxTree.Options).LanguageVersion)) + if (!MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(syntaxTree.Options.LanguageVersion())) { return; } diff --git a/src/Features/CSharp/Portable/MakeLocalFunctionStatic/PassInCapturedVariablesAsArgumentsCodeFixProvider.cs b/src/Features/CSharp/Portable/MakeLocalFunctionStatic/PassInCapturedVariablesAsArgumentsCodeFixProvider.cs index d2d9b23211f3c..e82692de0fcc7 100644 --- a/src/Features/CSharp/Portable/MakeLocalFunctionStatic/PassInCapturedVariablesAsArgumentsCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/MakeLocalFunctionStatic/PassInCapturedVariablesAsArgumentsCodeFixProvider.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Editing; using Roslyn.Utilities; @@ -78,10 +79,8 @@ private static async Task WrapFixAsync( // Even when the language version doesn't support staic local function, the compiler will still // generate this error. So we need to check to make sure we don't provide incorrect fix. - if (!MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(((CSharpParseOptions)root.SyntaxTree.Options).LanguageVersion)) - { + if (!MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(root.SyntaxTree.Options.LanguageVersion())) return; - } // Find all unique local functions that contain the error. var localFunctions = diagnostics diff --git a/src/Features/CSharp/Portable/ReplaceMethodWithProperty/CSharpReplaceMethodWithPropertyService.cs b/src/Features/CSharp/Portable/ReplaceMethodWithProperty/CSharpReplaceMethodWithPropertyService.cs index da26f70bf49cf..ecf15b6b8d3ed 100644 --- a/src/Features/CSharp/Portable/ReplaceMethodWithProperty/CSharpReplaceMethodWithPropertyService.cs +++ b/src/Features/CSharp/Portable/ReplaceMethodWithProperty/CSharpReplaceMethodWithPropertyService.cs @@ -45,7 +45,7 @@ public void ReplaceGetMethodWithProperty( return; } - var languageVersion = ((CSharpParseOptions)parseOptions).LanguageVersion; + var languageVersion = parseOptions.LanguageVersion(); var newProperty = ConvertMethodsToProperty( documentOptions, languageVersion, semanticModel, editor.Generator, diff --git a/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs b/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs index c28050f74a896..9d9c92bf78400 100644 --- a/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs +++ b/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs @@ -47,7 +47,7 @@ public override async Task> GetReplacementMembersAsyn var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); - var languageVersion = ((CSharpParseOptions)syntaxTree.Options).LanguageVersion; + var languageVersion = syntaxTree.Options.LanguageVersion(); return ConvertPropertyToMembers( documentOptions, languageVersion, diff --git a/src/Features/CSharp/Portable/UseAutoProperty/CSharpUseAutoPropertyCodeFixProvider.cs b/src/Features/CSharp/Portable/UseAutoProperty/CSharpUseAutoPropertyCodeFixProvider.cs index e380eb18e88a3..a94b7f7d55509 100644 --- a/src/Features/CSharp/Portable/UseAutoProperty/CSharpUseAutoPropertyCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/UseAutoProperty/CSharpUseAutoPropertyCodeFixProvider.cs @@ -11,6 +11,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Formatting; @@ -157,7 +158,7 @@ private static bool NeedsSetter(Compilation compilation, PropertyDeclarationSynt } private static bool SupportsReadOnlyProperties(Compilation compilation) - => ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp6; + => compilation.LanguageVersion() >= LanguageVersion.CSharp6; private static AccessorListSyntax UpdateAccessorList(AccessorListSyntax accessorList) { diff --git a/src/Features/CSharp/Portable/UseExpressionBodyForLambda/UseExpressionBodyForLambdaCodeStyleProvider.cs b/src/Features/CSharp/Portable/UseExpressionBodyForLambda/UseExpressionBodyForLambdaCodeStyleProvider.cs index 7fab4e7d0636c..0357333fdb241 100644 --- a/src/Features/CSharp/Portable/UseExpressionBodyForLambda/UseExpressionBodyForLambdaCodeStyleProvider.cs +++ b/src/Features/CSharp/Portable/UseExpressionBodyForLambda/UseExpressionBodyForLambdaCodeStyleProvider.cs @@ -114,7 +114,7 @@ private static bool CanOfferUseBlockBody( return false; } - var languageVersion = ((CSharpParseOptions)declaration.SyntaxTree.Options).LanguageVersion; + var languageVersion = declaration.SyntaxTree.Options.LanguageVersion(); if (expressionBodyOpt.IsKind(SyntaxKind.ThrowExpression) && languageVersion < LanguageVersion.CSharp7) { diff --git a/src/Features/CSharp/Portable/UseLocalFunction/CSharpUseLocalFunctionCodeFixProvider.cs b/src/Features/CSharp/Portable/UseLocalFunction/CSharpUseLocalFunctionCodeFixProvider.cs index a18aab329000c..19dbb99b310eb 100644 --- a/src/Features/CSharp/Portable/UseLocalFunction/CSharpUseLocalFunctionCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/UseLocalFunction/CSharpUseLocalFunctionCodeFixProvider.cs @@ -90,7 +90,7 @@ protected override async Task FixAllAsync( var currentRoot = root.TrackNodes(nodesToTrack); var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); - var languageVersion = ((CSharpParseOptions)semanticModel.SyntaxTree.Options).LanguageVersion; + var languageVersion = semanticModel.SyntaxTree.Options.LanguageVersion(); var makeStaticIfPossible = languageVersion >= LanguageVersion.CSharp8 && options.GetOption(CSharpCodeStyleOptions.PreferStaticLocalFunction).Value; diff --git a/src/Features/CSharp/Portable/UseNamedArguments/CSharpUseNamedArgumentsCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/UseNamedArguments/CSharpUseNamedArgumentsCodeRefactoringProvider.cs index 307fe5c884f04..6f65b384fd1f9 100644 --- a/src/Features/CSharp/Portable/UseNamedArguments/CSharpUseNamedArgumentsCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/UseNamedArguments/CSharpUseNamedArgumentsCodeRefactoringProvider.cs @@ -31,7 +31,7 @@ protected sealed override bool IsLegalToAddNamedArguments(ImmutableArray !parameters.Last().IsParams || parameters.Length >= argumentCount; protected override bool SupportsNonTrailingNamedArguments(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp7_2; + => options.LanguageVersion() >= LanguageVersion.CSharp7_2; protected override bool IsImplicitIndexOrRangeIndexer(ImmutableArray parameters, TSyntax argument, SemanticModel semanticModel) { diff --git a/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs index 8fd22d2e3a2db..650cf7cd252d3 100644 --- a/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs @@ -71,7 +71,7 @@ private void SyntaxNodeAction( // "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; } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CSharpCompilerExtensions.projitems b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CSharpCompilerExtensions.projitems index 7a9bd58376191..5dcf9a116468c 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CSharpCompilerExtensions.projitems +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CSharpCompilerExtensions.projitems @@ -20,6 +20,7 @@ + @@ -38,6 +39,7 @@ + diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/CompilationExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/CompilationExtensions.cs new file mode 100644 index 0000000000000..a0ed19e53f383 --- /dev/null +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/CompilationExtensions.cs @@ -0,0 +1,12 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CodeAnalysis.CSharp.Extensions +{ + internal static class CompilationExtensions + { + public static LanguageVersion LanguageVersion(this Compilation compilation) + => ((CSharpCompilation)compilation).LanguageVersion; + } +} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/LanguageVersionExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/LanguageVersionExtensions.cs index 67faee49d91e6..b84c4abd638c9 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/LanguageVersionExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/LanguageVersionExtensions.cs @@ -6,14 +6,8 @@ namespace Microsoft.CodeAnalysis.CSharp.Shared.Extensions { internal static class LanguageVersionExtensions { - public static bool IsCSharp9OrAbove(this LanguageVersion languageVersion) - => languageVersion >= LanguageVersion.CSharp9; - - public static bool IsCSharp10OrAbove(this LanguageVersion languageVersion) - => languageVersion >= LanguageVersion.CSharp10; - public static bool HasConstantInterpolatedStrings(this LanguageVersion languageVersion) - => languageVersion.IsCSharp10OrAbove(); + => languageVersion >= LanguageVersion.CSharp10; /// /// Corresponds to Microsoft.CodeAnalysis.CSharp.LanguageVersionFacts.CSharpNext. diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParseOptionsExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParseOptionsExtensions.cs new file mode 100644 index 0000000000000..01c8ef4d8937f --- /dev/null +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParseOptionsExtensions.cs @@ -0,0 +1,12 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CodeAnalysis.CSharp.Extensions +{ + internal static class ParseOptionsExtensions + { + public static LanguageVersion LanguageVersion(this ParseOptions options) + => ((CSharpParseOptions)options).LanguageVersion; + } +} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs index c84b60696c5cd..e9cc29d9fe2f7 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs @@ -50,22 +50,22 @@ public SyntaxTrivia ElasticCarriageReturnLineFeed public ISyntaxKinds SyntaxKinds { get; } = CSharpSyntaxKinds.Instance; public bool SupportsIndexingInitializer(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp6; + => options.LanguageVersion() >= LanguageVersion.CSharp6; public bool SupportsThrowExpression(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp7; + => options.LanguageVersion() >= LanguageVersion.CSharp7; public bool SupportsLocalFunctionDeclaration(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp7; + => options.LanguageVersion() >= LanguageVersion.CSharp7; public bool SupportsRecord(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp9; + => options.LanguageVersion() >= LanguageVersion.CSharp9; public bool SupportsRecordStruct(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion.IsCSharp10OrAbove(); + => options.LanguageVersion() >= LanguageVersion.CSharp10; public bool SupportsTargetTypedConditionalExpression(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp9; + => options.LanguageVersion() >= LanguageVersion.CSharp9; public SyntaxToken ParseToken(string text) => SyntaxFactory.ParseToken(text); @@ -1492,7 +1492,7 @@ public void GetPartsOfRecursivePattern(SyntaxNode node, out SyntaxNode? type, ou } public bool SupportsNotPattern(ParseOptions options) - => ((CSharpParseOptions)options).LanguageVersion.IsCSharp9OrAbove(); + => options.LanguageVersion() >= LanguageVersion.CSharp9; public bool IsAndPattern([NotNullWhen(true)] SyntaxNode? node) => node.IsKind(SyntaxKind.AndPattern); diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs index 0e6f3b05c323a..a8ad0309f17b4 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs @@ -1248,8 +1248,7 @@ private static (SemanticModel? rewrittenSemanticModel, ExpressionSyntax? rewritt // Removing a cast may cause a conditional-expression conversion to come into existence. This is // fine as long as we're in C# 9 or above. - var languageVersion = ((CSharpCompilation)originalSemanticModel.Compilation).LanguageVersion; - if (languageVersion < LanguageVersion.CSharp9 && + if (originalSemanticModel.Compilation.LanguageVersion() < LanguageVersion.CSharp9 && IntroducedConditionalExpressionConversion(rewrittenExpression, rewrittenSemanticModel, cancellationToken)) { return default; diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpSemanticFactsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpSemanticFactsService.cs index d208cbee118d2..0665a2f2b99eb 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpSemanticFactsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpSemanticFactsService.cs @@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery; using Microsoft.CodeAnalysis.CSharp.LanguageServices; +using Microsoft.CodeAnalysis.CSharp.Shared.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.LanguageServices; using Microsoft.CodeAnalysis.Operations; @@ -39,7 +40,7 @@ protected override IEnumerable GetCollidableSymbols(SemanticModel seman var visibleSymbols = semanticModel.LookupSymbols(location.SpanStart); // Local function parameter is allowed to shadow variables since C# 8. - if (((CSharpCompilation)semanticModel.Compilation).LanguageVersion.MapSpecifiedToEffectiveVersion() >= LanguageVersion.CSharp8) + if (semanticModel.Compilation.LanguageVersion().MapSpecifiedToEffectiveVersion() >= LanguageVersion.CSharp8) { if (SyntaxFacts.IsParameterList(container) && SyntaxFacts.IsLocalFunctionStatement(container.Parent)) {