Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CA1710 (IdentifiersShouldHaveCorrectSuffix) for event symbol type #1980

Merged
merged 2 commits into from Jan 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -147,16 +147,23 @@ private static void AnalyzeCompilationStart(CompilationStartAnalysisContext cont
}
, SymbolKind.NamedType);

context.RegisterSymbolAction((saContext) =>
var eventArgsType = WellKnownTypes.EventArgs(context.Compilation);
if (eventArgsType != null)
{
const string eventHandlerString = "EventHandler";
var eventSymbol = saContext.Symbol as IEventSymbol;
if (!eventSymbol.Type.Name.EndsWith(eventHandlerString, StringComparison.Ordinal))
context.RegisterSymbolAction((saContext) =>
{
saContext.ReportDiagnostic(eventSymbol.CreateDiagnostic(DefaultRule, eventSymbol.Type.Name, eventHandlerString));
}
},
SymbolKind.Event);
const string eventHandlerString = "EventHandler";
var eventSymbol = (IEventSymbol)saContext.Symbol;
if (!eventSymbol.Type.Name.EndsWith(eventHandlerString, StringComparison.Ordinal) &&
eventSymbol.Type.IsInSource() &&
eventSymbol.Type.TypeKind == TypeKind.Delegate &&
((INamedTypeSymbol)eventSymbol.Type).DelegateInvokeMethod?.HasEventHandlerSignature(eventArgsType) == true)
mavasani marked this conversation as resolved.
Show resolved Hide resolved
{
saContext.ReportDiagnostic(eventSymbol.CreateDiagnostic(DefaultRule, eventSymbol.Type.Name, eventHandlerString));
}
},
SymbolKind.Event);
}
}
}
}
Expand Down
Expand Up @@ -1079,6 +1079,31 @@ Inherits Attribute
End Class");
}

[Fact, WorkItem(1822, "https://github.com/dotnet/roslyn-analyzers/issues/1822")]
public void CA1710_SystemAction_CSharp()
{
VerifyCSharp(@"
using System;

public class C
{
public event Action MyEvent;
}");
}

[Fact, WorkItem(1822, "https://github.com/dotnet/roslyn-analyzers/issues/1822")]
public void CA1710_CustomDelegate_CSharp()
{
VerifyCSharp(@"
using System;

public class C
{
public delegate void MyDelegate(int param);
public event MyDelegate MyEvent;
}");
}

private static DiagnosticResult GetCA1710BasicResultAt(int line, int column, string symbolName, string replacementName, bool isSpecial = false)
{
return GetBasicResultAt(
Expand Down
11 changes: 11 additions & 0 deletions src/Utilities/Extensions/IMethodSymbolExtensions.cs
Expand Up @@ -382,5 +382,16 @@ public static int GetParameterIndex(this IMethodSymbol methodSymbol, IParameterS

throw new ArgumentException("Invalid paramater", nameof(parameterSymbol));
}

/// <summary>
/// Returns true for void returning methods with two parameters, where
/// the first parameter is of <see cref="object"/> type and the second
/// parameter inherits from or equals <see cref="EventArgs"/> type.
/// </summary>
public static bool HasEventHandlerSignature(this IMethodSymbol method, INamedTypeSymbol eventArgsType)
=> eventArgsType != null &&
method.Parameters.Length == 2 &&
method.Parameters[0].Type.SpecialType == SpecialType.System_Object &&
method.Parameters[1].Type.DerivesFrom(eventArgsType, baseTypesOnly: true);
}
}
8 changes: 8 additions & 0 deletions src/Utilities/Extensions/ISymbolExtensions.cs
Expand Up @@ -552,5 +552,13 @@ public static bool HasAttribute(this ISymbol symbol, INamedTypeSymbol attribute)
{
return symbol.GetAttributes().Any(attr => attr.AttributeClass.Equals(attribute));
}

/// <summary>
/// Indicates if a symbol has at least one location in source.
/// </summary>
public static bool IsInSource(this ISymbol symbol)
{
return symbol.Locations.Any(l => l.IsInSource);
}
}
}