Skip to content

Commit

Permalink
Rule S3908: Generic event handlers should be used (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaury Levé authored Jun 12, 2017
1 parent ab500f6 commit a6caf6f
Show file tree
Hide file tree
Showing 13 changed files with 476 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"issues": [
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "Ember-MM\Ember.Plugins\PluginManager.cs",
"region": {
"startLine": 20,
"startColumn": 22,
"endLine": 20,
"endColumn": 54
}
}
},
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "Ember-MM\Ember.Plugins\Scraper\MovieScraperManager.cs",
"region": {
"startLine": 20,
"startColumn": 22,
"endLine": 20,
"endColumn": 61
}
}
},
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "Ember-MM\Ember.Plugins\Scraper\MovieScraperManager.cs",
"region": {
"startLine": 25,
"startColumn": 22,
"endLine": 25,
"endColumn": 62
}
}
},
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "Ember-MM\Ember.Plugins\Scraper\MovieScraperManager.cs",
"region": {
"startLine": 30,
"startColumn": 22,
"endLine": 30,
"endColumn": 62
}
}
},
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "Ember-MM\Ember.Plugins\Scraper\MovieScraperManager.cs",
"region": {
"startLine": 35,
"startColumn": 22,
"endLine": 35,
"endColumn": 63
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"issues": [
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "akka.net\src\core\Akka.Remote\Transport\Helios\HeliosHelpers.cs",
"region": {
"startLine": 166,
"startColumn": 22,
"endLine": 166,
"endColumn": 42
}
}
},
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "akka.net\src\core\Akka.Remote\Transport\Helios\HeliosHelpers.cs",
"region": {
"startLine": 167,
"startColumn": 22,
"endLine": 167,
"endColumn": 51
}
}
},
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "akka.net\src\core\Akka.Remote\Transport\Helios\HeliosHelpers.cs",
"region": {
"startLine": 168,
"startColumn": 22,
"endLine": 168,
"endColumn": 50
}
}
},
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "akka.net\src\core\Akka.Remote\Transport\Helios\HeliosHelpers.cs",
"region": {
"startLine": 169,
"startColumn": 22,
"endLine": 169,
"endColumn": 39
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"issues": [
{
"id": "S3908",
"message": "Refactor this delegate to use 'System.EventHandler<TEventArgs>'.",
"location": {
"uri": "akka.net\src\core\Akka.TestKit\EventFilter\Internal\EventFilterBase.cs",
"region": {
"startLine": 33,
"startColumn": 22,
"endLine": 33,
"endColumn": 34
}
}
}
]
}
71 changes: 71 additions & 0 deletions sonaranalyzer-dotnet/rspec/cs/S3908_c#.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<p>Since .Net Framework version 2.0 it is not necessary to declare a delegate that specifies a class derived from <code>System.EventArgs</code>. The
<code>System.EventHandler&lt;TEventArgs&gt;</code> delegate mechanism should be used instead as it allows any class derived from
<code>EventArgs</code> to be used with that handler.</p>
<p>This rule raises an issue when an old style delegate is used as an event handler.</p>
<h2>Noncompliant Code Example</h2>
<pre>
public class MyEventArgs : EventArgs
{
}

public delegate void MyEventHandler(object sender, MyEventArgs e); // Noncompliant

public class EventProducer
{
public event MyEventHandler MyEvent;

protected virtual void OnMyEvent(MyEventArgs e)
{
if (MyEvent != null)
{
MyEvent(e);
}
}
}

public class EventConsumer
{
public EventConsumer(EventProducer producer)
{
producer.MyEvent += HandleEvent;
}

private void HandleEvent(object sender, MyEventArgs e)
{
// Do something...
}
}
</pre>
<h2>Compliant Solution</h2>
<pre>
public class MyEventArgs : EventArgs
{
}

public class EventProducer
{
public event EventHandler&lt;MyEventArgs&gt; MyEvent;

protected virtual void OnMyEvent(MyEventArgs e)
{
if (MyEvent != null)
{
MyEvent(e);
}
}
}

public class EventConsumer
{
public EventConsumer(EventProducer producer)
{
producer.MyEvent += HandleEvent;
}

private void HandleEvent(object sender, MyEventArgs e)
{
// Do something...
}
}
</pre>

13 changes: 13 additions & 0 deletions sonaranalyzer-dotnet/rspec/cs/S3908_c#.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"title": "Generic event handlers should be used",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "15min"
},
"tags": [

],
"defaultSeverity": "Major"
}
81 changes: 54 additions & 27 deletions sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/RspecStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -5964,6 +5964,33 @@
<data name="S3906_Type" xml:space="preserve">
<value>CODE_SMELL</value>
</data>
<data name="S3908_Category" xml:space="preserve">
<value>Sonar Code Smell</value>
</data>
<data name="S3908_Description" xml:space="preserve">
<value>Since .Net Framework version 2.0 it is not necessary to declare a delegate that specifies a class derived from System.EventArgs. The System.EventHandler&lt;TEventArgs&gt; delegate mechanism should be used instead as it allows any class derived from EventArgs to be used with that handler.</value>
</data>
<data name="S3908_IsActivatedByDefault" xml:space="preserve">
<value>False</value>
</data>
<data name="S3908_Remediation" xml:space="preserve">
<value>Constant/Issue</value>
</data>
<data name="S3908_RemediationCost" xml:space="preserve">
<value>15min</value>
</data>
<data name="S3908_Severity" xml:space="preserve">
<value>Major</value>
</data>
<data name="S3908_Tags" xml:space="preserve">
<value />
</data>
<data name="S3908_Title" xml:space="preserve">
<value>Generic event handlers should be used</value>
</data>
<data name="S3908_Type" xml:space="preserve">
<value>CODE_SMELL</value>
</data>
<data name="S3909_Category" xml:space="preserve">
<value>Sonar Code Smell</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ protected override void Initialize(SonarAnalysisContext context)

private void AnalyzeEventType(SyntaxNodeAnalysisContext analysisContext, TypeSyntax typeSyntax)
{
var namedTypeSymbol = analysisContext.SemanticModel.GetSymbolInfo(typeSyntax).Symbol
var eventHandlerType = analysisContext.SemanticModel.GetSymbolInfo(typeSyntax).Symbol
as INamedTypeSymbol;
var methodSymbol = namedTypeSymbol?.DelegateInvokeMethod;
var methodSymbol = eventHandlerType?.DelegateInvokeMethod;
if (methodSymbol == null)
{
return;
Expand Down
Loading

0 comments on commit a6caf6f

Please sign in to comment.