Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -145,7 +145,7 @@ private static async Task<Document> ConvertToEqualsAsync(Document document, Sema
false =>
generator.InvocationExpression(
generator.MemberAccessExpression(
generator.TypeExpression(semanticModel.Compilation.GetSpecialType(SpecialType.System_Object)),
generator.TypeExpression(SpecialType.System_Object),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's a new API on roslyn side? I don't know why we would have gone through this more complex syntax otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Evangelink Both APIs were introduced in 3.0.0. I think this approach was taken since this is the first overload shown by IntelliSense?

nameof(object.Equals)),
binaryOperation.LeftOperand.Syntax.WithoutLeadingTrivia(),
binaryOperation.RightOperand.Syntax.WithoutTrailingTrivia())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ public override void Initialize(AnalysisContext context)

private static void OnCompilationStart(CompilationStartAnalysisContext context)
{
INamedTypeSymbol? objectType = context.Compilation.GetSpecialType(SpecialType.System_Object);
INamedTypeSymbol? equatableType = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemIEquatable1);
if (objectType != null && equatableType != null)
if (equatableType != null)
{
context.RegisterSymbolAction(c => AnalyzeSymbol(c, equatableType), SymbolKind.NamedType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static async Task<Document> AddConstructorsAsync(Document document, IEnu
containingTypeName: typeSymbol.Name,
parameters: new[]
{
generator.ParameterDeclaration("message", generator.TypeExpression(editor.SemanticModel.Compilation.GetSpecialType(SpecialType.System_String)))
generator.ParameterDeclaration("message", generator.TypeExpression(SpecialType.System_String))
},
accessibility: Accessibility.Public,
baseConstructorArguments: new[]
Expand All @@ -97,7 +97,7 @@ private static async Task<Document> AddConstructorsAsync(Document document, IEnu
containingTypeName: typeSymbol.Name,
parameters: new[]
{
generator.ParameterDeclaration("message", generator.TypeExpression(editor.SemanticModel.Compilation.GetSpecialType(SpecialType.System_String))),
generator.ParameterDeclaration("message", generator.TypeExpression(SpecialType.System_String)),
generator.ParameterDeclaration("innerException", generator.TypeExpression(editor.SemanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemException)))
},
accessibility: Accessibility.Public,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(c =>
{
INamedTypeSymbol? @string = c.Compilation.GetSpecialType(SpecialType.System_String);
INamedTypeSymbol? uri = c.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemUri);
if (@string == null || uri == null)
if (uri == null)
{
// we don't have required types
return;
}

var analyzer = new PerCompilationAnalyzer(c.Compilation, @string, uri, GetInvocationExpression);
var analyzer = new PerCompilationAnalyzer(c.Compilation, uri, GetInvocationExpression);
c.RegisterOperationAction(analyzer.Analyze, OperationKind.Invocation);
});
}
Expand All @@ -64,18 +63,15 @@ private sealed class PerCompilationAnalyzer
{
// this type will be created per compilation
private readonly Compilation _compilation;
private readonly INamedTypeSymbol _string;
private readonly INamedTypeSymbol _uri;
private readonly Func<SyntaxNode, SyntaxNode?> _expressionGetter;

public PerCompilationAnalyzer(
Compilation compilation,
INamedTypeSymbol @string,
INamedTypeSymbol uri,
Func<SyntaxNode, SyntaxNode?> expressionGetter)
{
_compilation = compilation;
_string = @string;
_uri = uri;
_expressionGetter = expressionGetter;
}
Expand Down Expand Up @@ -107,7 +103,7 @@ public void Analyze(OperationAnalysisContext context)
return;
}

var stringParameters = method.Parameters.GetParametersOfType(_string);
var stringParameters = method.Parameters.GetParametersOfType(SpecialType.System_String);
if (!stringParameters.Any())
{
// no string parameter. not interested.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,26 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(c =>
{
var @string = c.Compilation.GetSpecialType(SpecialType.System_String);
var uri = c.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemUri);
var attribute = c.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemAttribute);
if (@string == null || uri == null || attribute == null)
if (uri == null || attribute == null)
{
// we don't have required types
return;
}

var analyzer = new PerCompilationAnalyzer(@string, uri, attribute);
var analyzer = new PerCompilationAnalyzer(uri, attribute);
c.RegisterSymbolAction(analyzer.Analyze, SymbolKind.Method);
});
}

private class PerCompilationAnalyzer
{
private readonly INamedTypeSymbol _string;
private readonly INamedTypeSymbol _uri;
private readonly INamedTypeSymbol _attribute;

public PerCompilationAnalyzer(INamedTypeSymbol @string, INamedTypeSymbol uri, INamedTypeSymbol attribute)
public PerCompilationAnalyzer(INamedTypeSymbol uri, INamedTypeSymbol attribute)
{
_string = @string;
_uri = uri;
_attribute = attribute;
}
Expand All @@ -89,7 +86,7 @@ public void Analyze(SymbolAnalysisContext context)
return;
}

var stringParameters = method.Parameters.GetParametersOfType(_string);
var stringParameters = method.Parameters.GetParametersOfType(SpecialType.System_String);
if (!stringParameters.Any())
{
// no string parameter. not interested.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,24 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(c =>
{
var @string = c.Compilation.GetSpecialType(SpecialType.System_String);
var attribute = c.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemAttribute);
if (@string == null || attribute == null)
if (attribute == null)
{
// we don't have required types
return;
}

var analyzer = new PerCompilationAnalyzer(@string, attribute);
var analyzer = new PerCompilationAnalyzer(attribute);
c.RegisterSymbolAction(analyzer.Analyze, SymbolKind.Property);
});
}

private class PerCompilationAnalyzer
{
private readonly INamedTypeSymbol _string;
private readonly INamedTypeSymbol _attribute;

public PerCompilationAnalyzer(INamedTypeSymbol @string, INamedTypeSymbol attribute)
public PerCompilationAnalyzer(INamedTypeSymbol attribute)
{
_string = @string;
_attribute = attribute;
}

Expand All @@ -85,7 +82,7 @@ public void Analyze(SymbolAnalysisContext context)
return;
}

if (property.Type?.Equals(_string) != true)
if (property.Type?.SpecialType != SpecialType.System_String)
{
// not expected type
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,24 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(c =>
{
var @string = c.Compilation.GetSpecialType(SpecialType.System_String);
var uri = c.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemUri);
if (@string == null || uri == null)
if (uri == null)
{
// we don't have required types
return;
}

var analyzer = new PerCompilationAnalyzer(@string, uri);
var analyzer = new PerCompilationAnalyzer(uri);
c.RegisterSymbolAction(analyzer.Analyze, SymbolKind.Method);
});
}

private class PerCompilationAnalyzer
{
private readonly INamedTypeSymbol _string;
private readonly INamedTypeSymbol _uri;

public PerCompilationAnalyzer(INamedTypeSymbol @string, INamedTypeSymbol uri)
public PerCompilationAnalyzer(INamedTypeSymbol uri)
{
_string = @string;
_uri = uri;
}

Expand All @@ -85,7 +82,7 @@ public void Analyze(SymbolAnalysisContext context)
return;
}

if (method.IsAccessorMethod() || method.ReturnType?.Equals(_string) != true)
if (method.IsAccessorMethod() || method.ReturnType?.SpecialType != SpecialType.System_String)
{
// return type must be string and it must be not an accessor method
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(context =>
{
var builder = ImmutableHashSet.CreateBuilder<ITypeSymbol>();
builder.Add(context.Compilation.GetSpecialType(SpecialType.System_IntPtr));
builder.Add(context.Compilation.GetSpecialType(SpecialType.System_UIntPtr));
var builder = ImmutableHashSet.CreateBuilder<SpecialType?>();
builder.Add(SpecialType.System_IntPtr);
builder.Add(SpecialType.System_UIntPtr);

var constantIncompatibleTypes = builder.ToImmutable();

Expand Down Expand Up @@ -85,7 +85,7 @@ public override void Initialize(AnalysisContext context)

// Though null is const we don't fire the diagnostic to be FxCop Compact
if (initializerValue != null &&
!constantIncompatibleTypes.Contains(fieldInitializerValue.Type))
!constantIncompatibleTypes.Contains(fieldInitializerValue.Type?.SpecialType))
{
if (fieldInitializerValue.Type?.SpecialType == SpecialType.System_String &&
((string)initializerValue).Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
Expand Down Expand Up @@ -51,7 +50,7 @@ private static async Task<Document> ConvertToArrayEmpty(Document document, Synta
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
SyntaxGenerator generator = editor.Generator;

INamedTypeSymbol? arrayTypeSymbol = semanticModel.Compilation.GetOrCreateTypeByMetadataName(AvoidZeroLengthArrayAllocationsAnalyzer.ArrayTypeName);
INamedTypeSymbol? arrayTypeSymbol = semanticModel.Compilation.GetSpecialType(SpecialType.System_Array);
if (arrayTypeSymbol == null)
{
return document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public abstract class AvoidZeroLengthArrayAllocationsAnalyzer : DiagnosticAnalyz
{
internal const string RuleId = "CA1825";

/// <summary>The name of the array type.</summary>
internal const string ArrayTypeName = "System.Array"; // using instead of GetSpecialType to make more testable

/// <summary>The name of the Empty method on System.Array.</summary>
internal const string ArrayEmptyMethodName = "Empty";

Expand Down Expand Up @@ -53,24 +50,24 @@ public sealed override void Initialize(AnalysisContext context)
// Only if it is, register the syntax node action provided by the derived implementations.
context.RegisterCompilationStartAction(ctx =>
{
INamedTypeSymbol? typeSymbol = ctx.Compilation.GetOrCreateTypeByMetadataName(ArrayTypeName);
if (typeSymbol != null && typeSymbol.DeclaredAccessibility == Accessibility.Public)
INamedTypeSymbol typeSymbol = ctx.Compilation.GetSpecialType(SpecialType.System_Array);
if (typeSymbol.DeclaredAccessibility == Accessibility.Public)
{
if (typeSymbol.GetMembers(ArrayEmptyMethodName).FirstOrDefault() is IMethodSymbol methodSymbol && methodSymbol.DeclaredAccessibility == Accessibility.Public &&
methodSymbol.IsStatic && methodSymbol.Arity == 1 && methodSymbol.Parameters.IsEmpty)
methodSymbol.IsStatic && methodSymbol.Arity == 1 && methodSymbol.Parameters.IsEmpty)
{
ctx.RegisterOperationAction(AnalyzeOperation, OperationKind.ArrayCreation);
ctx.RegisterOperationAction(c => AnalyzeOperation(c, methodSymbol), OperationKind.ArrayCreation);
}
}
});
}

private void AnalyzeOperation(OperationAnalysisContext context)
private void AnalyzeOperation(OperationAnalysisContext context, IMethodSymbol arrayEmptyMethodSymbol)
{
AnalyzeOperation(context, IsAttributeSyntax);
AnalyzeOperation(context, arrayEmptyMethodSymbol, IsAttributeSyntax);
}

private static void AnalyzeOperation(OperationAnalysisContext context, Func<SyntaxNode, bool> isAttributeSytnax)
private static void AnalyzeOperation(OperationAnalysisContext context, IMethodSymbol arrayEmptyMethodSymbol, Func<SyntaxNode, bool> isAttributeSytnax)
{
IArrayCreationOperation arrayCreationExpression = (IArrayCreationOperation)context.Operation;

Expand Down Expand Up @@ -109,14 +106,7 @@ private static void AnalyzeOperation(OperationAnalysisContext context, Func<Synt

if (elementType.TypeKind != TypeKind.Pointer)
{
var arrayType = context.Compilation.GetOrCreateTypeByMetadataName(ArrayTypeName);
if (arrayType == null)
{
return;
}

IMethodSymbol emptyMethod = (IMethodSymbol)arrayType.GetMembers(ArrayEmptyMethodName).First();
var constructed = emptyMethod.Construct(elementType);
var constructed = arrayEmptyMethodSymbol.Construct(elementType);

string typeName = constructed.ToDisplayString(ReportFormat);
context.ReportDiagnostic(arrayCreationExpression.Syntax.CreateDiagnostic(UseArrayEmptyDescriptor, typeName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public override void Initialize(AnalysisContext context)
{
var objectType = compilationStartContext.Compilation.GetSpecialType(SpecialType.System_Object);

if (objectType == null)
{
return;
}

var objectObjectParameters = new[]
{
ParameterInfo.GetParameterInfo(objectType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public sealed class NormalizeStringsToUppercaseAnalyzer : AbstractGlobalizationD
protected override void InitializeWorker(CompilationStartAnalysisContext context)
{
var stringType = context.Compilation.GetSpecialType(SpecialType.System_String);
if (stringType == null)
{
return;
}

var cultureInfo = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemGlobalizationCultureInfo);
var invariantCulture = cultureInfo?.GetMembers("InvariantCulture").OfType<IPropertySymbol>().FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public override void Initialize(AnalysisContext context)
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction(context =>
{
if (!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemString, out INamedTypeSymbol? stringType) ||
!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemChar, out INamedTypeSymbol? charType) ||
if (context.Compilation.GetSpecialType(SpecialType.System_String) is not INamedTypeSymbol stringType ||
context.Compilation.GetSpecialType(SpecialType.System_Char) is not INamedTypeSymbol charType ||
!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemStringComparison, out INamedTypeSymbol? stringComparisonType))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context

var objectType = context.Compilation.GetSpecialType(SpecialType.System_Object);
var stringType = context.Compilation.GetSpecialType(SpecialType.System_String);
if (objectType == null || stringType == null)
{
return;
}

var charType = context.Compilation.GetSpecialType(SpecialType.System_Char);
var boolType = context.Compilation.GetSpecialType(SpecialType.System_Boolean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context
var stringType = context.Compilation.GetSpecialType(SpecialType.System_String);

// Without these symbols the rule cannot run
if (stringComparisonType == null || stringType == null)
if (stringComparisonType == null)
{
return;
}
Expand Down
Loading