Permalink
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (39 sloc) 1.79 KB
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Roslynator.CSharp.Analysis
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class UseEventArgsEmptyAnalyzer : BaseDiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(DiagnosticDescriptors.UseEventArgsEmpty); }
}
public override void Initialize(AnalysisContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
base.Initialize(context);
context.RegisterSyntaxNodeAction(AnalyzeObjectCreationExpression, SyntaxKind.ObjectCreationExpression);
}
public static void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext context)
{
if (context.Node.SpanContainsDirectives())
return;
var objectCreation = (ObjectCreationExpressionSyntax)context.Node;
if (objectCreation.ArgumentList?.Arguments.Count != 0)
return;
if (objectCreation.Initializer != null)
return;
ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(objectCreation, context.CancellationToken);
if (typeSymbol?.HasMetadataName(MetadataNames.System_EventArgs) != true)
return;
context.ReportDiagnostic(DiagnosticDescriptors.UseEventArgsEmpty, objectCreation);
}
}
}