Skip to content

Commit

Permalink
Merge pull request #54358 from dotnet/merges/main-to-main-vs-deps
Browse files Browse the repository at this point in the history
Merge main to main-vs-deps
  • Loading branch information
msftbot[bot] committed Jun 25, 2021
2 parents 9d5ad74 + b796152 commit 445e572
Show file tree
Hide file tree
Showing 53 changed files with 2,690 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ public ConvertSwitchStatementToExpressionDiagnosticAnalyzer()
}

protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.SwitchStatement);
=> context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp8)
{
return;
}
context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.SwitchStatement);
});

private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
{
var switchStatement = context.Node;
var syntaxTree = switchStatement.SyntaxTree;

if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp8)
{
return;
}

var options = context.Options;
var cancellationToken = context.CancellationToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterCompilationStartAction(context =>
{
// All trees are expected to have the same language version. So make the check only once in compilation start .
if (context.Compilation.SyntaxTrees.FirstOrDefault() is SyntaxTree tree && MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(tree))
if (MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(((CSharpCompilation)context.Compilation).LanguageVersion))
{
context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.LocalFunctionStatement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Microsoft.CodeAnalysis.CSharp.MakeLocalFunctionStatic
{
internal static class MakeLocalFunctionStaticHelper
{
public static bool IsStaticLocalFunctionSupported(SyntaxTree tree)
=> tree.Options is CSharpParseOptions csharpOption && csharpOption.LanguageVersion >= LanguageVersion.CSharp8;
public static bool IsStaticLocalFunctionSupported(LanguageVersion languageVersion)
=> languageVersion >= LanguageVersion.CSharp8;

private static bool TryGetDataFlowAnalysis(LocalFunctionStatementSyntax localFunction, SemanticModel semanticModel, [NotNullWhen(returnValue: true)] out DataFlowAnalysis? dataFlow)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryDiscardDesignation
{
Expand All @@ -32,15 +30,19 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeDiscardDesignation, SyntaxKind.DiscardDesignation);
context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp9)
{
return;
}
context.RegisterSyntaxNodeAction(AnalyzeDiscardDesignation, SyntaxKind.DiscardDesignation);
});
}

private void AnalyzeDiscardDesignation(SyntaxNodeAnalysisContext context)
{
var syntaxTree = context.SemanticModel.SyntaxTree;
if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp9)
return;

var discard = (DiscardDesignationSyntax)context.Node;
if (discard.Parent is DeclarationPatternSyntax)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal class CSharpUseCollectionInitializerDiagnosticAnalyzer :
ExpressionStatementSyntax,
VariableDeclaratorSyntax>
{
protected override bool AreCollectionInitializersSupported(SyntaxNodeAnalysisContext context)
=> ((CSharpParseOptions)context.Node.SyntaxTree.Options).LanguageVersion >= LanguageVersion.CSharp3;
protected override bool AreCollectionInitializersSupported(Compilation compilation)
=> ((CSharpCompilation)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,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -32,7 +30,15 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression);
=> context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp7)
{
return;
}
context.RegisterSyntaxNodeAction(n => AnalyzeSyntax(n), SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression);
});

private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
{
Expand All @@ -41,11 +47,6 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
var semanticModel = context.SemanticModel;
var syntaxTree = semanticModel.SyntaxTree;

if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp7)
{
return;
}

var option = context.Options.GetOption(CodeStyleOptions2.PreferIsNullCheckOverReferenceEqualityMethod, semanticModel.Language, syntaxTree, cancellationToken);
if (!option.Value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public CSharpUseIsNullCheckForReferenceEqualsDiagnosticAnalyzer()
{
}

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

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

protected override ISyntaxFacts GetSyntaxFacts()
=> CSharpSyntaxFacts.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ internal class CSharpUseNullPropagationDiagnosticAnalyzer :
ConditionalAccessExpressionSyntax,
ElementAccessExpressionSyntax>
{
protected override bool ShouldAnalyze(ParseOptions options)
=> ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp6;
protected override bool ShouldAnalyze(Compilation compilation)
=> ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp6;

protected override ISyntaxFacts GetSyntaxFacts()
=> CSharpSyntaxFacts.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ internal class CSharpUseObjectInitializerDiagnosticAnalyzer :
{
protected override bool FadeOutOperatorToken => true;

protected override bool AreObjectInitializersSupported(SyntaxNodeAnalysisContext context)
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 ((CSharpParseOptions)context.Node.SyntaxTree.Options).LanguageVersion >= LanguageVersion.CSharp3;
return ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp3;
}

protected override ISyntaxFacts GetSyntaxFacts() => CSharpSyntaxFacts.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(SyntaxNodeAction, SyntaxKind.LogicalNotExpression);
context.RegisterCompilationStartAction(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())
return;
context.RegisterSyntaxNodeAction(n => SyntaxNodeAction(n), SyntaxKind.LogicalNotExpression);
});
}

private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
{
var node = syntaxContext.Node;
var syntaxTree = node.SyntaxTree;

// "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 (!((CSharpParseOptions)syntaxTree.Options).LanguageVersion.IsCSharp9OrAbove())
return;

var options = syntaxContext.Options;
var cancellationToken = syntaxContext.CancellationToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ public CSharpUseThrowExpressionDiagnosticAnalyzer()
{
}

protected override bool IsSupported(ParseOptions options)
protected override bool IsSupported(Compilation compilation)
{
var csOptions = (CSharpParseOptions)options;
return csOptions.LanguageVersion >= LanguageVersion.CSharp7;
return ((CSharpCompilation)compilation).LanguageVersion >= LanguageVersion.CSharp7;
}

protected override bool IsInExpressionTree(SemanticModel semanticModel, SyntaxNode node, INamedTypeSymbol expressionTypeOpt, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ protected override void InitializeWorker(AnalysisContext context)

private void OnCompilationStart(CompilationStartAnalysisContext context)
{
if (!AreCollectionInitializersSupported(context.Compilation))
{
return;
}

var ienumerableType = context.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName!);
if (ienumerableType != null)
{
Expand All @@ -57,15 +62,10 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
}
}

protected abstract bool AreCollectionInitializersSupported(SyntaxNodeAnalysisContext context);
protected abstract bool AreCollectionInitializersSupported(Compilation compilation);

private void AnalyzeNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol ienumerableType)
{
if (!AreCollectionInitializersSupported(context))
{
return;
}

var semanticModel = context.SemanticModel;
var objectCreationExpression = (TObjectCreationExpressionSyntax)context.Node;
var language = objectCreationExpression.Language;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterCompilationStartAction(compilationContext =>
=> context.RegisterCompilationStartAction(context =>
{
var objectType = compilationContext.Compilation.GetSpecialType(SpecialType.System_Object);
if (objectType != null)
var objectType = context.Compilation.GetSpecialType(SpecialType.System_Object);
if (objectType != null && IsLanguageVersionSupported(context.Compilation))
{
var referenceEqualsMethod = objectType.GetMembers(nameof(ReferenceEquals))
.OfType<IMethodSymbol>()
Expand All @@ -41,27 +41,23 @@ protected override void InitializeWorker(AnalysisContext context)
if (referenceEqualsMethod != null)
{
var syntaxKinds = GetSyntaxFacts().SyntaxKinds;
var unconstraintedGenericSupported = IsUnconstrainedGenericSupported(context.Compilation);
context.RegisterSyntaxNodeAction(
c => AnalyzeSyntax(c, referenceEqualsMethod),
c => AnalyzeSyntax(c, referenceEqualsMethod, unconstraintedGenericSupported),
syntaxKinds.Convert<TLanguageKindEnum>(syntaxKinds.InvocationExpression));
}
}
});

protected abstract bool IsLanguageVersionSupported(ParseOptions options);
protected abstract bool IsUnconstrainedGenericSupported(ParseOptions options);
protected abstract bool IsLanguageVersionSupported(Compilation compilation);
protected abstract bool IsUnconstrainedGenericSupported(Compilation compilation);
protected abstract ISyntaxFacts GetSyntaxFacts();

private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, IMethodSymbol referenceEqualsMethod)
private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, IMethodSymbol referenceEqualsMethod, bool unconstraintedGenericSupported)
{
var cancellationToken = context.CancellationToken;

var semanticModel = context.SemanticModel;
var syntaxTree = semanticModel.SyntaxTree;
if (!IsLanguageVersionSupported(syntaxTree.Options))
{
return;
}

var option = context.GetOption(CodeStyleOptions2.PreferIsNullCheckOverReferenceEqualityMethod, semanticModel.Language);
if (!option.Value)
Expand Down Expand Up @@ -125,7 +121,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, IMethodSymbol refe
// HasReferenceTypeConstraint returns false for base type constraint.
// IsReferenceType returns true.

if (!genericParameterSymbol.IsReferenceType && !IsUnconstrainedGenericSupported(syntaxTree.Options))
if (!genericParameterSymbol.IsReferenceType && !unconstraintedGenericSupported)
{
// Needs special casing for C# as long as
// 'is null' over unconstrained generic is implemented in C# 8.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected AbstractUseNullPropagationDiagnosticAnalyzer()
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

protected abstract bool ShouldAnalyze(ParseOptions options);
protected abstract bool ShouldAnalyze(Compilation compilation);

protected abstract ISyntaxFacts GetSyntaxFacts();
protected abstract bool IsInExpressionTree(SemanticModel semanticModel, SyntaxNode node, INamedTypeSymbol? expressionTypeOpt, CancellationToken cancellationToken);
Expand All @@ -60,6 +60,11 @@ protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterCompilationStartAction(startContext =>
{
if (!ShouldAnalyze(startContext.Compilation))
{
return;
}
var expressionTypeOpt = startContext.Compilation.GetTypeByMetadataName("System.Linq.Expressions.Expression`1");
var objectType = startContext.Compilation.GetSpecialType(SpecialType.System_Object);
Expand All @@ -82,10 +87,6 @@ private void AnalyzeSyntax(
IMethodSymbol? referenceEqualsMethodOpt)
{
var conditionalExpression = (TConditionalExpressionSyntax)context.Node;
if (!ShouldAnalyze(conditionalExpression.SyntaxTree.Options))
{
return;
}

var option = context.GetOption(CodeStyleOptions2.PreferNullPropagation, conditionalExpression.Language);
if (!option.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,24 @@ protected AbstractUseObjectInitializerDiagnosticAnalyzer()
protected override void InitializeWorker(AnalysisContext context)
{
var syntaxKinds = GetSyntaxFacts().SyntaxKinds;
context.RegisterSyntaxNodeAction(
AnalyzeNode, syntaxKinds.Convert<TSyntaxKind>(syntaxKinds.ObjectCreationExpression));
context.RegisterCompilationStartAction(context =>
{
if (!AreObjectInitializersSupported(context.Compilation))
{
return;
}
context.RegisterSyntaxNodeAction(
AnalyzeNode, syntaxKinds.Convert<TSyntaxKind>(syntaxKinds.ObjectCreationExpression));
});
}

protected abstract bool AreObjectInitializersSupported(SyntaxNodeAnalysisContext context);
protected abstract bool AreObjectInitializersSupported(Compilation compilation);

protected abstract bool IsValidContainingStatement(TStatementSyntax node);

private void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (!AreObjectInitializersSupported(context))
{
return;
}

var objectCreationExpression = (TObjectCreationExpressionSyntax)context.Node;
var language = objectCreationExpression.Language;
var option = context.GetOption(CodeStyleOptions2.PreferObjectInitializer, language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,24 @@ protected AbstractUseThrowExpressionDiagnosticAnalyzer(Option2<CodeStyleOption2<
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

protected abstract bool IsSupported(ParseOptions options);
protected abstract bool IsSupported(Compilation compilation);

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterCompilationStartAction(startContext =>
{
if (!IsSupported(startContext.Compilation))
{
return;
}
var expressionTypeOpt = startContext.Compilation.GetTypeByMetadataName("System.Linq.Expressions.Expression`1");
startContext.RegisterOperationAction(operationContext => AnalyzeOperation(operationContext, expressionTypeOpt), OperationKind.Throw);
});
}

private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol expressionTypeOpt)
{
var syntaxTree = context.Operation.Syntax.SyntaxTree;
if (!IsSupported(syntaxTree.Options))
{
return;
}

var cancellationToken = context.CancellationToken;

var throwOperation = (IThrowOperation)context.Operation;
Expand Down
Loading

0 comments on commit 445e572

Please sign in to comment.