Skip to content
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 @@ -54,7 +54,8 @@ public sealed override void Initialize(AnalysisContext context)

void visitEnumSymbol(SymbolStartAnalysisContext context)
{
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Enum } enumSymbol)
var enumSymbol = (INamedTypeSymbol)context.Symbol;
if (enumSymbol.TypeKind != TypeKind.Enum)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ private static void OnCompilationStart(CompilationStartAnalysisContext context)

private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol equatableType)
{
if (context.Symbol is not INamedTypeSymbol namedType
|| (namedType.TypeKind != TypeKind.Struct && namedType.TypeKind != TypeKind.Class)
var namedType = (INamedTypeSymbol)context.Symbol;
if ((namedType.TypeKind != TypeKind.Struct && namedType.TypeKind != TypeKind.Class)
|| (namedType.TypeKind == TypeKind.Struct && namedType.IsRefLikeType))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ public void Initialize(CompilationStartAnalysisContext context)

private void AnalyzeNamedTypeSymbol(SymbolAnalysisContext context)
{
var type = (INamedTypeSymbol)context.Symbol;
// Note all the descriptors/rules for this analyzer have the same ID and category and hence
// will always have identical configured visibility.
if (context.Symbol is INamedTypeSymbol type &&
type.TypeKind == TypeKind.Class &&
if (type.TypeKind == TypeKind.Class &&
context.Options.MatchesConfiguredVisibility(IDisposableReimplementationRule, type, context.Compilation))
{
bool implementsDisposableInBaseType = ImplementsDisposableInBaseType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void OnOperationBlockStart(OperationBlockStartAnalysisContext context)

context.RegisterOperationAction(context =>
{
if (context.Operation is IInstanceReferenceOperation { ReferenceKind: InstanceReferenceKind.ContainingTypeInstance }
var operation = (IInstanceReferenceOperation)context.Operation;
if (operation.ReferenceKind == InstanceReferenceKind.ContainingTypeInstance
&& (context.Operation.Parent is not IInvocationOperation invocation || !invocation.TargetMethod.Equals(methodSymbol, SymbolEqualityComparer.Default)))
{
isInstanceReferenced = true;
Expand All @@ -121,7 +122,8 @@ void OnOperationBlockStart(OperationBlockStartAnalysisContext context)

context.RegisterOperationAction(context =>
{
if (context.Operation is IParameterReferenceOperation { Parameter.ContainingSymbol: IMethodSymbol { MethodKind: MethodKind.Constructor } })
var operation = (IParameterReferenceOperation)context.Operation;
if (operation.Parameter.ContainingSymbol is IMethodSymbol { MethodKind: MethodKind.Constructor })
{
// we're referencing a parameter not from our actual method, but from a type constructor.
// This must be a primary constructor scenario, and we're capturing the parameter here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,26 @@ public override void Initialize(AnalysisContext context)

context.RegisterSymbolAction(context =>
{
if (context.Symbol is IMethodSymbol method)
var method = (IMethodSymbol)context.Symbol;

// Eliminate methods that would fail the CS8814, CS8815, and CS8816 checks
// for what can have [ModuleInitializer] applied
if (method.GetResultantVisibility() == SymbolVisibility.Private ||
method.Parameters.Length > 0 ||
method.IsGenericMethod ||
method.ContainingType.IsGenericType ||
!method.IsStatic ||
!method.ReturnsVoid)
{
// Eliminate methods that would fail the CS8814, CS8815, and CS8816 checks
// for what can have [ModuleInitializer] applied
if (method.GetResultantVisibility() == SymbolVisibility.Private ||
method.Parameters.Length > 0 ||
method.IsGenericMethod ||
method.ContainingType.IsGenericType ||
!method.IsStatic ||
!method.ReturnsVoid)
{
return;
}
return;
}

AttributeData? initializerAttribute = context.Symbol.GetAttribute(moduleInitializerAttribute);
SyntaxReference? attributeReference = initializerAttribute?.ApplicationSyntaxReference;
AttributeData? initializerAttribute = context.Symbol.GetAttribute(moduleInitializerAttribute);
SyntaxReference? attributeReference = initializerAttribute?.ApplicationSyntaxReference;

if (attributeReference is not null)
{
context.ReportDiagnostic(attributeReference.GetSyntax(context.CancellationToken).CreateDiagnostic(Rule));
}
if (attributeReference is not null)
{
context.ReportDiagnostic(attributeReference.GetSyntax(context.CancellationToken).CreateDiagnostic(Rule));
}
},
SymbolKind.Method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ public override void Initialize(AnalysisContext context)

context.RegisterSymbolAction(context =>
{
if (context.Symbol is INamedTypeSymbol ntSymbol)
{
AnalyzeSymbol(context, ntSymbol, iParsableInterface.ContainingNamespace, iNumberInterface.ContainingNamespace);
}
var symbol = (INamedTypeSymbol)context.Symbol;
AnalyzeSymbol(context, symbol, iParsableInterface.ContainingNamespace, iNumberInterface.ContainingNamespace);
}, SymbolKind.NamedType);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Immutable;
using Analyzer.Utilities;
Expand Down Expand Up @@ -96,9 +96,10 @@ public override void Initialize(AnalysisContext context)
compilationStartContext.RegisterSymbolAction(
(SymbolAnalysisContext symbolContext) =>
{
var methodSymbol = (IMethodSymbol)symbolContext.Symbol;

// TODO enhancements: Consider looking at IAsyncResult-based action methods.
if (symbolContext.Symbol is not IMethodSymbol methodSymbol
|| methodSymbol.MethodKind != MethodKind.Ordinary
if (methodSymbol.MethodKind != MethodKind.Ordinary
|| methodSymbol.IsStatic
|| !methodSymbol.IsPublic()
|| !(methodSymbol.ReturnType.Inherits(actionResultSymbol) // FxCop implementation only looked at ActionResult-derived return types.
Expand Down